|
1 | | -name: Build and Push Docker Image |
| 1 | +name: Build and Publish Docker Image on Tag |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
5 | | - branches: |
6 | | - - '**' # all branches |
7 | 5 | tags: |
8 | | - - 'v*' # tags |
| 6 | + - 'v*' # Trigger the workflow when a new tag starting with 'v' is pushed |
9 | 7 |
|
10 | 8 | jobs: |
11 | | - docker: |
| 9 | + build: |
12 | 10 | runs-on: ubuntu-latest |
13 | 11 |
|
| 12 | + permissions: |
| 13 | + actions: read |
| 14 | + contents: read |
| 15 | + packages: write |
| 16 | + |
14 | 17 | steps: |
15 | | - - name: Checkout source |
16 | | - uses: actions/checkout@v4 |
17 | | - |
18 | | - - name: Set up Docker Buildx |
19 | | - uses: docker/setup-buildx-action@v3 |
20 | | - |
21 | | - - name: Log in to Docker Hub |
22 | | - uses: docker/login-action@v3 |
23 | | - with: |
24 | | - username: ${{ secrets.DOCKER_USERNAME }} |
25 | | - password: ${{ secrets.DOCKER_PASSWORD }} |
26 | | - |
27 | | - - name: Extract Git ref and tag |
28 | | - id: extract |
29 | | - run: | |
30 | | - echo "GIT_REF=${GITHUB_REF}" >> $GITHUB_OUTPUT |
31 | | - echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT |
32 | | -
|
33 | | - # Extract sanitized branch name or tag |
34 | | - if [[ "${GITHUB_REF}" == refs/heads/* ]]; then |
35 | | - BRANCH_NAME="${GITHUB_REF#refs/heads/}" |
36 | | - TAG_NAME=$(echo "${BRANCH_NAME}" | tr '[:upper:]' '[:lower:]' | sed 's#[^a-z0-9_.-]#-#g') |
37 | | - elif [[ "${GITHUB_REF}" == refs/tags/* ]]; then |
38 | | - TAG_NAME="${GITHUB_REF#refs/tags/}" |
39 | | - else |
40 | | - TAG_NAME="unknown" |
41 | | - fi |
42 | | -
|
43 | | - echo "DOCKER_TAG=${TAG_NAME}" >> $GITHUB_OUTPUT |
44 | | -
|
45 | | - - name: Build and push Docker image |
46 | | - uses: docker/build-push-action@v5 |
47 | | - with: |
48 | | - context: . |
49 | | - push: true |
50 | | - tags: ghcr.io/ximeraproject/server:${{ steps.extract.outputs.DOCKER_TAG }} |
| 18 | + # Step 1: Set up Docker Buildx (supports layer caching) |
| 19 | + - name: Set up Docker Buildx |
| 20 | + uses: docker/setup-buildx-action@v2 |
| 21 | + |
| 22 | + # Step 2: Restore cache for Docker layers |
| 23 | + - name: Cache Docker layers |
| 24 | + uses: actions/cache@v4 |
| 25 | + with: |
| 26 | + path: /tmp/.buildx-cache |
| 27 | + key: buildx-cache-${{ github.sha }} |
| 28 | + restore-keys: | |
| 29 | + buildx-cache-${{ github.sha }} |
| 30 | + buildx-cache- |
| 31 | + buildx |
| 32 | +
|
| 33 | + # Step 3: Check out the repository |
| 34 | + - name: Checkout repository |
| 35 | + uses: actions/checkout@v4 |
| 36 | + |
| 37 | + # # Step 4: Log in to registry |
| 38 | + - name: Log into registry |
| 39 | + run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin |
| 40 | + |
| 41 | + - name: Initialize cache directory |
| 42 | + run: mkdir -p /tmp/.buildx-cache |
| 43 | + |
| 44 | + - name: Debug cache |
| 45 | + run: ls -l /tmp/.buildx-cache |
| 46 | + |
| 47 | + # Step 5: Build and push the Docker image with caching |
| 48 | + - name: Build and Push Docker image |
| 49 | + run: | |
| 50 | + docker buildx build \ |
| 51 | + --cache-from=type=local,src=/tmp/.buildx-cache \ |
| 52 | + --cache-to=type=local,dest=/tmp/.buildx-cache,mode=max \ |
| 53 | + --tag ghcr.io/ximeraproject/ximeraserver:${{ github.ref_name }} \ |
| 54 | + --push . |
0 commit comments