|
| 1 | +name: Build and Push Docker Image |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + tags: |
| 8 | + - "v*.*.*" # v1.2.3 |
| 9 | + - "v*.*.*-*" # v1.2.3-rc1, v1.2.3-alpha.1, v1.2.3-beta |
| 10 | + - "v*.*.*-*.*" # v1.2.3-alpha.1, v1.2.3-rc.2 |
| 11 | + pull_request: |
| 12 | + branches: |
| 13 | + - master |
| 14 | + workflow_dispatch: |
| 15 | + inputs: |
| 16 | + ref: |
| 17 | + description: 'Git ref (branch, tag, or commit SHA) to build' |
| 18 | + required: false |
| 19 | + default: 'master' |
| 20 | + type: string |
| 21 | + push_image: |
| 22 | + description: 'Push image to registry' |
| 23 | + required: false |
| 24 | + default: true |
| 25 | + type: boolean |
| 26 | + |
| 27 | +env: |
| 28 | + REGISTRY: ghcr.io |
| 29 | + IMAGE_NAME: ${{ github.repository }} |
| 30 | + |
| 31 | +jobs: |
| 32 | + build-and-push: |
| 33 | + runs-on: ubuntu-latest |
| 34 | + permissions: |
| 35 | + contents: read |
| 36 | + packages: write |
| 37 | + |
| 38 | + steps: |
| 39 | + - name: Checkout repository |
| 40 | + uses: actions/checkout@v4 |
| 41 | + with: |
| 42 | + ref: ${{ github.event.inputs.ref || github.ref }} |
| 43 | + fetch-depth: 0 |
| 44 | + |
| 45 | + - name: Set up Docker Buildx |
| 46 | + uses: docker/setup-buildx-action@v3 |
| 47 | + |
| 48 | + - name: Log in to Container Registry |
| 49 | + if: github.event_name != 'pull_request' && (github.event_name != 'workflow_dispatch' || github.event.inputs.push_image == 'true') |
| 50 | + uses: docker/login-action@v3 |
| 51 | + with: |
| 52 | + registry: ${{ env.REGISTRY }} |
| 53 | + username: ${{ github.actor }} |
| 54 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 55 | + |
| 56 | + - name: Extract metadata |
| 57 | + id: meta |
| 58 | + uses: docker/metadata-action@v5 |
| 59 | + with: |
| 60 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 61 | + tags: | |
| 62 | + # set latest tag for master branch |
| 63 | + type=raw,value=latest,enable={{is_default_branch}} |
| 64 | + # set version tag for tags |
| 65 | + type=ref,event=tag |
| 66 | + # set branch name for branch pushes |
| 67 | + type=ref,event=branch |
| 68 | + # set pr-<number> for pull requests |
| 69 | + type=ref,event=pr |
| 70 | + # set sha-<short_sha> for any push |
| 71 | + type=sha,prefix=sha- |
| 72 | + # set manual-<short_sha> for manual workflow dispatch |
| 73 | + type=sha,prefix=manual-,enable=${{ github.event_name == 'workflow_dispatch' }} |
| 74 | +
|
| 75 | + - name: Get package info |
| 76 | + id: package_info |
| 77 | + run: | |
| 78 | + echo "PACKAGE_VERSION=$(git describe --tags --always)" >> $GITHUB_OUTPUT |
| 79 | + echo "PACKAGE_COMMIT_ID=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT |
| 80 | + GIT_STATUS=$(git status --porcelain) |
| 81 | + if [ -n "$GIT_STATUS" ]; then |
| 82 | + echo "GIT_STATUS=dirty" >> $GITHUB_OUTPUT |
| 83 | + else |
| 84 | + echo "GIT_STATUS=clean" >> $GITHUB_OUTPUT |
| 85 | + fi |
| 86 | +
|
| 87 | + - name: Build and push Docker image |
| 88 | + id: build |
| 89 | + uses: docker/build-push-action@v5 |
| 90 | + with: |
| 91 | + context: . |
| 92 | + platforms: linux/amd64,linux/arm64 |
| 93 | + push: ${{ github.event_name != 'pull_request' && (github.event_name != 'workflow_dispatch' || github.event.inputs.push_image == 'true') }} |
| 94 | + tags: ${{ steps.meta.outputs.tags }} |
| 95 | + labels: ${{ steps.meta.outputs.labels }} |
| 96 | + build-args: | |
| 97 | + PACKAGE_VERSION=${{ steps.package_info.outputs.PACKAGE_VERSION }} |
| 98 | + PACKAGE_COMMIT_ID=${{ steps.package_info.outputs.PACKAGE_COMMIT_ID }} |
| 99 | + GIT_STATUS=${{ steps.package_info.outputs.GIT_STATUS }} |
| 100 | + cache-from: type=gha |
| 101 | + cache-to: type=gha,mode=max |
| 102 | + |
| 103 | + - name: Generate artifact attestation |
| 104 | + if: github.event_name != 'pull_request' && (github.event_name != 'workflow_dispatch' || github.event.inputs.push_image == 'true') |
| 105 | + uses: actions/attest-build-provenance@v1 |
| 106 | + with: |
| 107 | + subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}} |
| 108 | + subject-digest: ${{ steps.build.outputs.digest }} |
| 109 | + push-to-registry: true |
0 commit comments