Skip to content

Commit

Permalink
ci: refactor docker workflow for parallel platform builds
Browse files Browse the repository at this point in the history
  • Loading branch information
Mogyuchi committed Jan 26, 2025
1 parent 5c1e378 commit 317efeb
Showing 1 changed file with 114 additions and 25 deletions.
139 changes: 114 additions & 25 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,28 @@ env:
BUILDX_VERSION: v0.20.1

jobs:
docker-publish:
env:
# release-please によるコミットの時は workflow_call でのみ実行する
if: ${{ !( github.workflow == 'docker publish' && startsWith(github.event.head_commit.message, 'release:') && github.event.head_commit.author.name == 'github-actions[bot]' ) }}
runs-on: ubuntu-latest
outputs:
image_tags: ${{ steps.metadata.outputs.tags }}
permissions:
packages: write

REGISTRY_IMAGE: ${{ steps.lowercase.outputs.REGISTRY_IMAGE }}
PUSH: ${{ github.event_name != 'pull_request' && github.ref_name == github.event.repository.default_branch }}
steps:
- name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 # v3.8.0
with:
version: ${{ env.BUILDX_VERSION }}

- name: Log in to the Container registry
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- id: lowercase
run: |
registory_image='ghcr.io/${{ env.IMAGE_NAME }}'
echo "REGISTRY_IMAGE=${registory_image@L}" >> $GITHUB_OUTPUT
meta:
needs: [env]
runs-on: ubuntu-latest
outputs:
version: ${{ steps.metadata.outputs.version}}
tags: ${{ steps.metadata.outputs.tags }}
labels: ${{ steps.labels.outputs.labels }}
json: ${{ steps.metadata.outputs.json }}
steps:
- name: Docker Metadata
uses: docker/metadata-action@369eb591f429131d6889c46b94e711f089e6ca96 # v5.6.1
id: metadata
Expand All @@ -63,17 +60,52 @@ jobs:
EOF
EOS
docker-publish:
permissions:
packages: write
needs: [env, meta]
outputs:
digest: ${{ steps.build-push.outputs.digest }}

strategy:
fail-fast: false
matrix:
include:
- platform: linux/amd64
on: ubuntu-24.04
- platform: linux/arm64
on: ubuntu-24.04-arm

runs-on: ${{ matrix.on }}

steps:
- name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 # v3.8.0
with:
version: ${{ env.BUILDX_VERSION }}

- name: Log in to the Container registry
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- id: build-push
name: Build and push Docker image
name: Build and push by digest
uses: docker/build-push-action@ca877d9245402d1537745e0e356eab47c3520991 # v6.13.0
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
platforms: linux/amd64,linux/arm64
tags: ${{ steps.metadata.outputs.tags }}
labels: ${{ steps.labels.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: ${{ matrix.platform }}
tags: ${{ needs.meta.outputs.tags }}
labels: ${{ needs.meta.outputs.labels }}
outputs: type=image,name=${{ needs.env.outputs.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=${{ fromJson(needs.env.outputs.PUSH) }}
cache-from: type=gha,scope=buildkit-${{ matrix.platform }}
cache-to: type=gha,scope=buildkit-${{ matrix.platform }},mode=max

- if: ${{ github.event_name != 'pull_request' }}
name: Display image digest
Expand All @@ -86,3 +118,60 @@ jobs:
echo '```' >> $GITHUB_STEP_SUMMARY
echo "$name@$DIGEST" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
- id: export-digest
if: ${{ fromJson(needs.env.outputs.PUSH) }}
name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build-push.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- id: prepare
if: ${{ steps.export-digest.conclusion == 'success' }}
name: Prepare
run: |
platform=${{ matrix.platform }}
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_OUTPUT
- if: ${{ steps.export-digest.conclusion == 'success' }}
name: Upload digest
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
with:
name: digests-${{ steps.prepare.outputs.PLATFORM_PAIR }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1


merge:
permissions:
packages: write
needs: [env, meta, docker-publish]
if: ${{ fromJson(needs.env.outputs.PUSH) }}
runs-on: ubuntu-latest
steps:
- name: Download digests
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
with:
path: /tmp/digests
pattern: digests-*
merge-multiple: true

- name: Log in to the Container registry
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Create manifest list and push
working-directory: /tmp/digests
shell: bash -xe {0}
env:
DOCKER_METADATA_OUTPUT_JSON: ${{ needs.meta.outputs.json }}
run: |
docker buildx imagetools create $(jq -cr '.tags | map("--tag=" + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ needs.env.outputs.REGISTRY_IMAGE }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ needs.env.outputs.REGISTRY_IMAGE }}:${{ needs.meta.outputs.version }}

0 comments on commit 317efeb

Please sign in to comment.