|
| 1 | +--- |
| 2 | +name: Build Docker Image (tags on master) |
| 3 | + |
| 4 | +on: |
| 5 | + push: |
| 6 | + tags: ['v*.*.*'] |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +concurrency: |
| 10 | + group: ${{ github.repository }}:tag:${{ github.sha }} |
| 11 | + cancel-in-progress: true |
| 12 | + |
| 13 | +jobs: |
| 14 | + guard: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + outputs: |
| 17 | + skip: ${{ steps.detect.outputs.on_default_branch == 'true' }} |
| 18 | + steps: |
| 19 | + - name: Checkout code |
| 20 | + uses: actions/checkout@v4 |
| 21 | + with: |
| 22 | + fetch-depth: 0 |
| 23 | + fetch-tags: true |
| 24 | + |
| 25 | + - name: Detect default branch |
| 26 | + id: detect |
| 27 | + run: | |
| 28 | + DEFAULT="${{ github.event.repository.default_branch }}" |
| 29 | + git fetch origin "$DEFAULT" --depth=1 |
| 30 | + if git merge-base --is-ancestor "$GITHUB_SHA" "origin/$DEFAULT"; then |
| 31 | + echo "on_default_branch=true" >> $GITHUB_OUTPUT |
| 32 | + else |
| 33 | + echo "on_default_branch=false" >> $GITHUB_OUTPUT |
| 34 | + fi |
| 35 | +
|
| 36 | + build: |
| 37 | + needs: guard |
| 38 | + if: needs.guard.outputs.skip == 'true' |
| 39 | + runs-on: ubuntu-latest |
| 40 | + permissions: |
| 41 | + contents: read |
| 42 | + packages: write |
| 43 | + steps: |
| 44 | + - name: Checkout code |
| 45 | + uses: actions/checkout@v4 |
| 46 | + with: |
| 47 | + fetch-depth: 0 |
| 48 | + fetch-tags: true |
| 49 | + |
| 50 | + - name: Set env |
| 51 | + run: | |
| 52 | + SHORT_SHA=$(echo "${GITHUB_SHA}" | cut -c1-7) |
| 53 | + echo "SHORT_SHA=${SHORT_SHA}" >> $GITHUB_ENV |
| 54 | +
|
| 55 | + - name: Set up Docker Buildx |
| 56 | + uses: docker/setup-buildx-action@v3 |
| 57 | + |
| 58 | + - name: Log in to GHCR |
| 59 | + uses: docker/login-action@v3 |
| 60 | + with: |
| 61 | + registry: ghcr.io |
| 62 | + username: ${{ github.actor }} |
| 63 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 64 | + |
| 65 | + - name: Docker meta |
| 66 | + id: meta |
| 67 | + uses: docker/metadata-action@v5 |
| 68 | + with: |
| 69 | + images: ghcr.io/${{ github.repository }} |
| 70 | + flavor: | |
| 71 | + latest=auto |
| 72 | + tags: | |
| 73 | + type=semver,pattern={{version}} |
| 74 | + type=semver,pattern={{major}}.{{minor}} |
| 75 | + type=semver,pattern={{major}} |
| 76 | + type=raw,value=${{ github.event.repository.default_branch }} |
| 77 | +
|
| 78 | + - name: Build and push |
| 79 | + uses: docker/build-push-action@v6 |
| 80 | + with: |
| 81 | + context: . |
| 82 | + push: true |
| 83 | + cache-from: type=gha |
| 84 | + cache-to: type=gha,mode=max |
| 85 | + tags: ${{ steps.meta.outputs.tags }} |
| 86 | + labels: ${{ steps.meta.outputs.labels }} |
0 commit comments