|
| 1 | +name: Create and publish Docker images with specific build args |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + - dev |
| 9 | + tags: |
| 10 | + - v* |
| 11 | + |
| 12 | +env: |
| 13 | + REGISTRY: ghcr.io |
| 14 | + |
| 15 | +jobs: |
| 16 | + build-main-image: |
| 17 | + runs-on: ${{ matrix.platform == 'linux/arm64' && 'ubuntu-24.04-arm' || 'ubuntu-latest' }} |
| 18 | + permissions: |
| 19 | + contents: read |
| 20 | + packages: write |
| 21 | + strategy: |
| 22 | + fail-fast: false |
| 23 | + matrix: |
| 24 | + platform: |
| 25 | + - linux/amd64 |
| 26 | + - linux/arm64 |
| 27 | + |
| 28 | + steps: |
| 29 | + # GitHub Packages requires the entire repository name to be in lowercase |
| 30 | + # although the repository owner has a lowercase username, this prevents some people from running actions after forking |
| 31 | + - name: Set repository and image name to lowercase |
| 32 | + run: | |
| 33 | + echo "IMAGE_NAME=${IMAGE_NAME,,}" >>${GITHUB_ENV} |
| 34 | + echo "FULL_IMAGE_NAME=ghcr.io/${IMAGE_NAME,,}" >>${GITHUB_ENV} |
| 35 | + env: |
| 36 | + IMAGE_NAME: "${{ github.repository }}" |
| 37 | + |
| 38 | + - name: Prepare |
| 39 | + run: | |
| 40 | + platform=${{ matrix.platform }} |
| 41 | + echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV |
| 42 | +
|
| 43 | + - name: Checkout repository |
| 44 | + uses: actions/checkout@v4 |
| 45 | + |
| 46 | + - name: Set up QEMU |
| 47 | + uses: docker/setup-qemu-action@v3 |
| 48 | + |
| 49 | + - name: Set up Docker Buildx |
| 50 | + uses: docker/setup-buildx-action@v3 |
| 51 | + |
| 52 | + - name: Log in to the Container registry |
| 53 | + uses: docker/login-action@v3 |
| 54 | + with: |
| 55 | + registry: ${{ env.REGISTRY }} |
| 56 | + username: ${{ github.actor }} |
| 57 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 58 | + |
| 59 | + - name: Extract metadata for Docker images (default latest tag) |
| 60 | + id: meta |
| 61 | + uses: docker/metadata-action@v5 |
| 62 | + with: |
| 63 | + images: ${{ env.FULL_IMAGE_NAME }} |
| 64 | + tags: | |
| 65 | + type=ref,event=branch |
| 66 | + type=ref,event=tag |
| 67 | + type=sha,prefix=git- |
| 68 | + type=semver,pattern={{version}} |
| 69 | + type=semver,pattern={{major}}.{{minor}} |
| 70 | + flavor: | |
| 71 | + latest=${{ github.ref == 'refs/heads/main' }} |
| 72 | +
|
| 73 | + - name: Extract metadata for Docker cache |
| 74 | + id: cache-meta |
| 75 | + uses: docker/metadata-action@v5 |
| 76 | + with: |
| 77 | + images: ${{ env.FULL_IMAGE_NAME }} |
| 78 | + tags: | |
| 79 | + type=ref,event=branch |
| 80 | + ${{ github.ref_type == 'tag' && 'type=raw,value=main' || '' }} |
| 81 | + flavor: | |
| 82 | + prefix=cache-${{ matrix.platform }}- |
| 83 | + latest=false |
| 84 | +
|
| 85 | + - name: Build Docker image (latest) |
| 86 | + uses: docker/build-push-action@v5 |
| 87 | + id: build |
| 88 | + with: |
| 89 | + context: . |
| 90 | + push: true |
| 91 | + platforms: ${{ matrix.platform }} |
| 92 | + labels: ${{ steps.meta.outputs.labels }} |
| 93 | + outputs: type=image,name=${{ env.FULL_IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true |
| 94 | + cache-from: type=registry,ref=${{ steps.cache-meta.outputs.tags }} |
| 95 | + cache-to: type=registry,ref=${{ steps.cache-meta.outputs.tags }},mode=max |
| 96 | + build-args: | |
| 97 | + BUILD_HASH=${{ github.sha }} |
| 98 | +
|
| 99 | + - name: Export digest |
| 100 | + run: | |
| 101 | + mkdir -p /tmp/digests |
| 102 | + digest="${{ steps.build.outputs.digest }}" |
| 103 | + touch "/tmp/digests/${digest#sha256:}" |
| 104 | +
|
| 105 | + - name: Upload digest |
| 106 | + uses: actions/upload-artifact@v4 |
| 107 | + with: |
| 108 | + name: digests-main-${{ env.PLATFORM_PAIR }} |
| 109 | + path: /tmp/digests/* |
| 110 | + if-no-files-found: error |
| 111 | + retention-days: 1 |
| 112 | + |
| 113 | + merge-main-images: |
| 114 | + runs-on: ubuntu-latest |
| 115 | + needs: [build-main-image] |
| 116 | + steps: |
| 117 | + # GitHub Packages requires the entire repository name to be in lowercase |
| 118 | + # although the repository owner has a lowercase username, this prevents some people from running actions after forking |
| 119 | + - name: Set repository and image name to lowercase |
| 120 | + run: | |
| 121 | + echo "IMAGE_NAME=${IMAGE_NAME,,}" >>${GITHUB_ENV} |
| 122 | + echo "FULL_IMAGE_NAME=ghcr.io/${IMAGE_NAME,,}" >>${GITHUB_ENV} |
| 123 | + env: |
| 124 | + IMAGE_NAME: "${{ github.repository }}" |
| 125 | + |
| 126 | + - name: Download digests |
| 127 | + uses: actions/download-artifact@v4 |
| 128 | + with: |
| 129 | + pattern: digests-main-* |
| 130 | + path: /tmp/digests |
| 131 | + merge-multiple: true |
| 132 | + |
| 133 | + - name: Set up Docker Buildx |
| 134 | + uses: docker/setup-buildx-action@v3 |
| 135 | + |
| 136 | + - name: Log in to the Container registry |
| 137 | + uses: docker/login-action@v3 |
| 138 | + with: |
| 139 | + registry: ${{ env.REGISTRY }} |
| 140 | + username: ${{ github.actor }} |
| 141 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 142 | + |
| 143 | + - name: Extract metadata for Docker images (default latest tag) |
| 144 | + id: meta |
| 145 | + uses: docker/metadata-action@v5 |
| 146 | + with: |
| 147 | + images: ${{ env.FULL_IMAGE_NAME }} |
| 148 | + tags: | |
| 149 | + type=ref,event=branch |
| 150 | + type=ref,event=tag |
| 151 | + type=sha,prefix=git- |
| 152 | + type=semver,pattern={{version}} |
| 153 | + type=semver,pattern={{major}}.{{minor}} |
| 154 | + flavor: | |
| 155 | + latest=${{ github.ref == 'refs/heads/main' }} |
| 156 | +
|
| 157 | + - name: Create manifest list and push |
| 158 | + working-directory: /tmp/digests |
| 159 | + run: | |
| 160 | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ |
| 161 | + $(printf '${{ env.FULL_IMAGE_NAME }}@sha256:%s ' *) |
| 162 | +
|
| 163 | + - name: Inspect image |
| 164 | + run: | |
| 165 | + docker buildx imagetools inspect ${{ env.FULL_IMAGE_NAME }}:${{ steps.meta.outputs.version }} |
0 commit comments