Build docker images in CI #3
Workflow file for this run
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
# Based on | |
# - https://docs.github.com/en/actions/use-cases-and-examples/publishing-packages/publishing-docker-images | |
# - https://docs.docker.com/build/ci/github-actions/multi-platform/#distribute-build-across-multiple-runners | |
# and the branchwater config | |
# - https://github.com/sourmash-bio/branchwater/blob/main/.github/workflows/docker-images.yml | |
name: Create and publish Docker images | |
on: | |
push: | |
branches: | |
- 'main' | |
tags: | |
- 'v*' | |
pull_request: | |
paths: | |
- .github/workflows/docker-images.yml | |
- Dockerfile | |
## disabling for most PRs, too many builds | |
# branches: | |
# - 'main' | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.repository }} | |
jobs: | |
build: | |
runs-on: ${{ matrix.os }} | |
strategy: | |
matrix: | |
target: [web, worker] | |
platform: | |
- linux/amd64 | |
- linux/arm64 | |
include: | |
- platform: linux/amd64 | |
os: ubuntu-latest | |
- platform: linux/arm64 | |
os: ubuntu-24.04-arm | |
permissions: | |
contents: read | |
packages: write | |
id-token: write | |
steps: | |
- name: Delete huge unnecessary tools folder | |
run: | | |
rm -rf /opt/hostedtoolcache | |
cd /opt | |
find . -maxdepth 1 -mindepth 1 '!' -path ./containerd '!' -path ./actionarchivecache '!' -path ./runner '!' -path ./runner-cache -exec rm -rf '{}' ';' | |
- name: Checkout repository | |
uses: actions/checkout@85e6279cec87321a52edac9c87bce653a07cf6c2 | |
- name: Log in to the Container registry | |
uses: docker/login-action@327cd5a69de6c009b9ce71bce8395f28e651bf99 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 | |
- name: Prepare | |
run: | | |
platform=${{ matrix.platform }} | |
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@8e1d5461f02b7886d3c1a774bfbd873650445aa2 | |
with: | |
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
- name: Build and push by digest | |
id: build | |
uses: docker/build-push-action@ca877d9245402d1537745e0e356eab47c3520991 | |
with: | |
context: . | |
platforms: ${{ matrix.platform }} | |
labels: ${{ steps.meta.outputs.labels }} | |
target: ${{ matrix.target }} | |
outputs: type=image,"name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}",push-by-digest=true,name-canonical=true,push=true | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
# don't push on PRs | |
push: ${{ ! startsWith(github.ref, 'refs/pull/') }} | |
- name: Export digest | |
run: | | |
mkdir -p ${{ runner.temp }}/digests | |
digest="${{ steps.build.outputs.digest }}" | |
touch "${{ runner.temp }}/digests/${digest#sha256:}" | |
- name: Upload digest | |
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 | |
with: | |
name: digests-${{ matrix.target }}-${{ env.PLATFORM_PAIR }} | |
path: ${{ runner.temp }}/digests/* | |
if-no-files-found: error | |
retention-days: 1 | |
merge: | |
runs-on: ubuntu-latest | |
# avoid tagging on PRs | |
if: ${{ ! startsWith(github.ref, 'refs/pull/') }} | |
needs: | |
- build | |
strategy: | |
matrix: | |
target: [web, worker] | |
permissions: | |
contents: read | |
packages: write | |
attestations: write | |
id-token: write | |
steps: | |
- name: Download digests | |
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 | |
with: | |
path: ${{ runner.temp }}/digests | |
pattern: digests-${{ matrix.target }}-* | |
merge-multiple: true | |
- name: Log in to the Container registry | |
uses: docker/login-action@327cd5a69de6c009b9ce71bce8395f28e651bf99 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@8e1d5461f02b7886d3c1a774bfbd873650445aa2 | |
with: | |
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
tags: | | |
type=ref,event=branch,prefix=${{ matrix.target }}- | |
type=ref,event=pr,prefix=${{ matrix.target }}-pr- | |
type=semver,prefix=${{ matrix.target }}-,pattern={{version}} | |
- name: Create manifest list and push | |
working-directory: ${{ runner.temp }}/digests | |
run: | | |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ | |
$(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *) | |
- name: Inspect image | |
run: | | |
docker buildx imagetools inspect ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }} | |
# - name: Generate artifact attestation | |
# uses: actions/attest-build-provenance@v2 | |
# with: | |
# subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
# subject-digest: ${{ steps.build.outputs.digest }} | |
# push-to-registry: true |