|
| 1 | +name: Docker |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - 'pkg/**' |
| 9 | + - '*.go' |
| 10 | + - 'go.*' |
| 11 | + - Dockerfile |
| 12 | + - .github/workflows/docker.yaml |
| 13 | + pull_request: |
| 14 | + branches: |
| 15 | + - main |
| 16 | + paths: |
| 17 | + - 'pkg/**' |
| 18 | + - '*.go' |
| 19 | + - 'go.*' |
| 20 | + - Dockerfile |
| 21 | + - .github/workflows/docker.yaml |
| 22 | + release: |
| 23 | + types: |
| 24 | + - published |
| 25 | + |
| 26 | +env: |
| 27 | + REGISTRY: ghcr.io |
| 28 | + IMAGE_NAME: ${{ github.repository }} |
| 29 | + PLATFORMS: ${{ github.event_name == 'pull_request' && 'linux/amd64' || 'linux/amd64,linux/arm64' }} |
| 30 | + |
| 31 | +jobs: |
| 32 | + build: |
| 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 | + fetch-depth: 0 |
| 43 | + fetch-tags: true |
| 44 | + |
| 45 | + - name: Set up QEMU |
| 46 | + uses: docker/setup-qemu-action@v2 |
| 47 | + |
| 48 | + - name: "Generate Build ID (main)" |
| 49 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
| 50 | + run: | |
| 51 | + branch=${GITHUB_REF##*/} |
| 52 | + sha=${GITHUB_SHA::8} |
| 53 | + ts=$(date +%s) |
| 54 | + echo "BUILD_ID=${branch}-${ts}-${sha}" >> $GITHUB_ENV |
| 55 | +
|
| 56 | + - name: "Generate Build ID (PR)" |
| 57 | + if: github.event_name == 'pull_request' |
| 58 | + run: | |
| 59 | + echo "BUILD_ID=pr-${{ github.event.number }}-$GITHUB_RUN_ID" >> $GITHUB_ENV |
| 60 | +
|
| 61 | + - name: "Generate Build ID (Release)" |
| 62 | + if: github.event_name == 'release' |
| 63 | + run: | |
| 64 | + echo "BUILD_ID=${GITHUB_REF##*/}" >> $GITHUB_ENV |
| 65 | +
|
| 66 | + - name: 'Generate App Version' |
| 67 | + run: echo "VERSION=$(make version)" >> $GITHUB_ENV |
| 68 | + |
| 69 | + - name: Log in to the Container registry |
| 70 | + uses: docker/login-action@v2 |
| 71 | + with: |
| 72 | + registry: ${{ env.REGISTRY }} |
| 73 | + username: ${{ github.actor }} |
| 74 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 75 | + |
| 76 | + - name: Set up Docker Buildx |
| 77 | + uses: docker/setup-buildx-action@v2 |
| 78 | + |
| 79 | + - name: Extract metadata (tags, labels) for Docker |
| 80 | + id: meta |
| 81 | + uses: docker/metadata-action@v4 |
| 82 | + with: |
| 83 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 84 | + tags: | |
| 85 | + type=ref,event=pr |
| 86 | + type=ref,event=branch |
| 87 | + type=raw,value=${{ env.BUILD_ID }} |
| 88 | + type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }} |
| 89 | +
|
| 90 | + - name: Build and push Docker image |
| 91 | + uses: docker/build-push-action@v4 |
| 92 | + with: |
| 93 | + context: . |
| 94 | + push: true |
| 95 | + tags: ${{ steps.meta.outputs.tags }} |
| 96 | + labels: ${{ steps.meta.outputs.labels }} |
| 97 | + platforms: ${{ env.PLATFORMS }} |
| 98 | + cache-from: type=gha |
| 99 | + cache-to: type=gha,mode=max |
| 100 | + build-args: | |
| 101 | + VERSION=${{ env.VERSION }} |
0 commit comments