docker #272
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: docker | |
| on: | |
| workflow_run: | |
| workflows: [build] | |
| types: [completed] | |
| # NOTE: Do NOT add `branches:` filter here - it breaks tag builds | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_BASE: ${{ github.actor }}/ralphex | |
| IMAGE_GO: ${{ github.actor }}/ralphex-go | |
| jobs: | |
| # Build base image for both architectures | |
| build-base: | |
| if: >- | |
| github.event.workflow_run.conclusion == 'success' && | |
| github.event.workflow_run.event == 'push' && | |
| (github.event.workflow_run.head_branch == 'master' || | |
| startsWith(github.event.workflow_run.head_branch, 'v')) | |
| permissions: | |
| contents: read | |
| packages: write | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| include: | |
| - platform: linux/amd64 | |
| runner: ubuntu-latest | |
| artifact: linux-amd64 | |
| - platform: linux/arm64 | |
| runner: ubuntu-24.04-arm | |
| artifact: linux-arm64 | |
| runs-on: ${{ matrix.runner }} | |
| steps: | |
| - name: checkout | |
| uses: actions/checkout@v6 | |
| with: | |
| ref: ${{ github.event.workflow_run.head_sha }} | |
| persist-credentials: false | |
| - name: set up docker buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: login to ghcr.io | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.PKG_TOKEN }} | |
| - name: prepare build args | |
| id: prep | |
| run: | | |
| branch="${{ github.event.workflow_run.head_branch }}" | |
| sha="${{ github.event.workflow_run.head_sha }}" | |
| echo "branch=${branch}" >> $GITHUB_OUTPUT | |
| echo "version=${branch}-${sha:0:7}-$(date +%Y%m%dT%H%M%S)" >> $GITHUB_OUTPUT | |
| - name: build and push base image by digest | |
| id: build | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| platforms: ${{ matrix.platform }} | |
| build-args: | | |
| CI=true | |
| GITHUB_SHA=${{ github.event.workflow_run.head_sha }} | |
| GIT_BRANCH=${{ steps.prep.outputs.branch }} | |
| outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_BASE }},push-by-digest=true,name-canonical=true,push=true | |
| - name: export digest | |
| run: | | |
| mkdir -p /tmp/digests | |
| digest="${{ steps.build.outputs.digest }}" | |
| touch "/tmp/digests/${digest#sha256:}" | |
| - name: upload digest | |
| uses: actions/upload-artifact@v6 | |
| with: | |
| name: digests-base-${{ matrix.artifact }} | |
| path: /tmp/digests/* | |
| if-no-files-found: error | |
| retention-days: 1 | |
| # Merge base image manifests | |
| merge-base: | |
| runs-on: ubuntu-latest | |
| needs: build-base | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: download digests | |
| uses: actions/download-artifact@v7 | |
| with: | |
| path: /tmp/digests | |
| pattern: digests-base-* | |
| merge-multiple: true | |
| - name: verify all digests present | |
| run: | | |
| expected=2 | |
| actual=$(ls /tmp/digests | wc -l) | |
| if [ "$actual" -ne "$expected" ]; then | |
| echo "Expected $expected digests, found $actual" | |
| ls -la /tmp/digests | |
| exit 1 | |
| fi | |
| echo "All $expected digests present" | |
| - name: set up docker buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: login to ghcr.io | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.PKG_TOKEN }} | |
| - name: set docker tags | |
| id: tags | |
| run: | | |
| branch="${{ github.event.workflow_run.head_branch }}" | |
| if [[ "$branch" =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then | |
| # semver tag: v0.7.2 -> tags: 0.7.2, 0.7, latest | |
| echo "tag=${BASH_REMATCH[1]}.${BASH_REMATCH[2]}.${BASH_REMATCH[3]}" >> $GITHUB_OUTPUT | |
| echo "minor=${BASH_REMATCH[1]}.${BASH_REMATCH[2]}" >> $GITHUB_OUTPUT | |
| echo "latest=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "tag=${branch}" >> $GITHUB_OUTPUT | |
| echo "minor=" >> $GITHUB_OUTPUT | |
| echo "latest=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: create manifest and push | |
| working-directory: /tmp/digests | |
| run: | | |
| tags="-t ${{ env.REGISTRY }}/${{ env.IMAGE_BASE }}:${{ steps.tags.outputs.tag }}" | |
| if [ -n "${{ steps.tags.outputs.minor }}" ]; then | |
| tags="$tags -t ${{ env.REGISTRY }}/${{ env.IMAGE_BASE }}:${{ steps.tags.outputs.minor }}" | |
| fi | |
| if [ "${{ steps.tags.outputs.latest }}" == "true" ]; then | |
| tags="$tags -t ${{ env.REGISTRY }}/${{ env.IMAGE_BASE }}:latest" | |
| fi | |
| docker buildx imagetools create $tags \ | |
| $(printf '${{ env.REGISTRY }}/${{ env.IMAGE_BASE }}@sha256:%s ' *) | |
| # Build Go image for both architectures (after base is available) | |
| build-go: | |
| needs: merge-base | |
| permissions: | |
| contents: read | |
| packages: write | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| include: | |
| - platform: linux/amd64 | |
| runner: ubuntu-latest | |
| artifact: linux-amd64 | |
| - platform: linux/arm64 | |
| runner: ubuntu-24.04-arm | |
| artifact: linux-arm64 | |
| runs-on: ${{ matrix.runner }} | |
| steps: | |
| - name: checkout | |
| uses: actions/checkout@v6 | |
| with: | |
| ref: ${{ github.event.workflow_run.head_sha }} | |
| persist-credentials: false | |
| - name: set up docker buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: login to ghcr.io | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.PKG_TOKEN }} | |
| - name: determine base tag | |
| id: base | |
| run: | | |
| branch="${{ github.event.workflow_run.head_branch }}" | |
| if [[ "$branch" =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then | |
| echo "tag=${BASH_REMATCH[1]}.${BASH_REMATCH[2]}.${BASH_REMATCH[3]}" >> $GITHUB_OUTPUT | |
| else | |
| echo "tag=${branch}" >> $GITHUB_OUTPUT | |
| fi | |
| - name: build and push go image by digest | |
| id: build | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| file: Dockerfile-go | |
| platforms: ${{ matrix.platform }} | |
| build-args: | | |
| BASE_TAG=${{ steps.base.outputs.tag }} | |
| outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_GO }},push-by-digest=true,name-canonical=true,push=true | |
| - name: export digest | |
| run: | | |
| mkdir -p /tmp/digests | |
| digest="${{ steps.build.outputs.digest }}" | |
| touch "/tmp/digests/${digest#sha256:}" | |
| - name: upload digest | |
| uses: actions/upload-artifact@v6 | |
| with: | |
| name: digests-go-${{ matrix.artifact }} | |
| path: /tmp/digests/* | |
| if-no-files-found: error | |
| retention-days: 1 | |
| # Merge Go image manifests | |
| merge-go: | |
| runs-on: ubuntu-latest | |
| needs: build-go | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: download digests | |
| uses: actions/download-artifact@v7 | |
| with: | |
| path: /tmp/digests | |
| pattern: digests-go-* | |
| merge-multiple: true | |
| - name: verify all digests present | |
| run: | | |
| expected=2 | |
| actual=$(ls /tmp/digests | wc -l) | |
| if [ "$actual" -ne "$expected" ]; then | |
| echo "Expected $expected digests, found $actual" | |
| ls -la /tmp/digests | |
| exit 1 | |
| fi | |
| echo "All $expected digests present" | |
| - name: set up docker buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: login to ghcr.io | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.PKG_TOKEN }} | |
| - name: set docker tags | |
| id: tags | |
| run: | | |
| branch="${{ github.event.workflow_run.head_branch }}" | |
| if [[ "$branch" =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then | |
| # semver tag: v0.7.2 -> tags: 0.7.2, 0.7, latest | |
| echo "tag=${BASH_REMATCH[1]}.${BASH_REMATCH[2]}.${BASH_REMATCH[3]}" >> $GITHUB_OUTPUT | |
| echo "minor=${BASH_REMATCH[1]}.${BASH_REMATCH[2]}" >> $GITHUB_OUTPUT | |
| echo "latest=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "tag=${branch}" >> $GITHUB_OUTPUT | |
| echo "minor=" >> $GITHUB_OUTPUT | |
| echo "latest=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: create manifest and push | |
| working-directory: /tmp/digests | |
| run: | | |
| tags="-t ${{ env.REGISTRY }}/${{ env.IMAGE_GO }}:${{ steps.tags.outputs.tag }}" | |
| if [ -n "${{ steps.tags.outputs.minor }}" ]; then | |
| tags="$tags -t ${{ env.REGISTRY }}/${{ env.IMAGE_GO }}:${{ steps.tags.outputs.minor }}" | |
| fi | |
| if [ "${{ steps.tags.outputs.latest }}" == "true" ]; then | |
| tags="$tags -t ${{ env.REGISTRY }}/${{ env.IMAGE_GO }}:latest" | |
| fi | |
| docker buildx imagetools create $tags \ | |
| $(printf '${{ env.REGISTRY }}/${{ env.IMAGE_GO }}@sha256:%s ' *) |