ci: refactor docker workflow for parallel platform builds #1678
This file contains 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 publish | |
on: | |
push: | |
branches: [main] | |
tags: [v*.*.*] # 手動で作成した時用 | |
pull_request: | |
workflow_call: | |
inputs: | |
tag-name: | |
required: true | |
type: string | |
env: | |
IMAGE_NAME: om | |
# バージョン指定 | |
# renovate: datasource=github-releases depName=docker/buildx | |
BUILDX_VERSION: v0.20.1 | |
jobs: | |
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: | |
REGISTRY_IMAGE: ${{ steps.lowercase.outputs.REGISTRY_IMAGE }} | |
PUSH: ${{ github.event_name != 'pull_request' && github.ref_name == github.event.repository.default_branch }} | |
steps: | |
- 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 | |
with: | |
images: ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }} | |
tags: | | |
type=semver,pattern={{version}},value=${{ inputs.tag-name }} | |
type=semver,pattern={{major}}.{{minor}},value=${{ inputs.tag-name }} | |
type=semver,pattern={{major}},value=${{ inputs.tag-name }},enable=${{ !(startsWith(github.ref, 'refs/tags/v0.') || startsWith(inputs.tag-name, 'v0.')) }} | |
type=edge,branch=main | |
- name: Delete org.opencontainers.image.licenses label | |
id: labels | |
run: | | |
cat << EOS >> "$GITHUB_OUTPUT" | |
labels<<EOF | |
$(echo '${{ steps.metadata.outputs.labels }}' | sed '/^org.opencontainers.image.licenses=/d') | |
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 by digest | |
uses: docker/build-push-action@ca877d9245402d1537745e0e356eab47c3520991 # v6.13.0 | |
with: | |
context: . | |
push: ${{ github.event_name != 'pull_request' }} | |
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 | |
env: | |
NAME: ${{ fromJson(steps.build-push.outputs.metadata)['image.name'] }} | |
DIGEST: ${{ fromJson(steps.build-push.outputs.metadata)['containerimage.digest'] }} | |
run: | | |
name=$(cut --delimiter=',' --fields=1 <<< "$NAME") | |
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 }} |