Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docker): use matrix for building multi-platform Docker images #888

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 93 additions & 19 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,31 @@ on:
- cron: "0 0 * * 0"

jobs:
docker:
name: Docker Build and Push
runs-on: ubuntu-22.04
build:
name: Build
runs-on: ${{ matrix.build.OS }}
strategy:
fail-fast: false
matrix:
build:
- { PLATFORM: linux/amd64, OS: ubuntu-22.04 }
- { PLATFORM: linux/arm64, OS: macos-14 }
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Prepare
run: |
platform=${{ matrix.build.PLATFORM }}
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
orhunp/git-cliff
ghcr.io/${{ github.repository_owner }}/git-cliff/git-cliff
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
type=sha
type=raw,value=latest
type=semver,pattern={{version}}

- name: Set up Docker Buildx
id: buildx
Expand Down Expand Up @@ -62,25 +66,95 @@ jobs:
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
id: docker_build
- name: Build and push by digest
id: build
uses: docker/build-push-action@v6
with:
context: ./
file: ./Dockerfile
platforms: ${{ matrix.build.PLATFORM }}
builder: ${{ steps.buildx.outputs.name }}
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
sbom: true
provenance: true
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
outputs: type=image,name=orhunp/git-cliff,name=ghcr.io/${{ github.repository_owner }}/git-cliff/git-cliff,push-by-digest=${{ github.event_name != 'pull_request' }},name-canonical=true
sbom: true
provenance: true

- name: Export digest
run: |
digest="${{ steps.build.outputs.digest }}"
mkdir -p /tmp/digests
touch "/tmp/digests/${digest#sha256:}"
cat "/tmp/digests/${digest#sha256:}"

- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-${{ env.PLATFORM_PAIR }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1

merge:
name: Merge
runs-on: ubuntu-latest
if: github.event_name != 'pull_request'
needs:
- build
steps:
- name: Download digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-*
merge-multiple: true

Comment on lines +111 to +112

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
merge-multiple: true
merge-multiple: true
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

Wow sorry... i just noticed i suggested to remove the wrong lines. The docs mentioned in the description have this step as well, so i guess it is needed for the Docker meta step as well.

This will not solve the issue with the missing docker socket on MacOs though...

Copy link

@LtdSauce LtdSauce Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But still doesn't matter as macos runners on the M1 chip cannot do docker builds anyway.

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
orhunp/git-cliff
ghcr.io/${{ github.repository_owner }}/git-cliff/git-cliff
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
type=sha
type=raw,value=latest
type=semver,pattern={{version}}

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

- name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Create manifest list and push
working-directory: /tmp/digests
shell: bash
run: |
for image in orhunp/git-cliff ghcr.io/${{ github.repository_owner }}/git-cliff/git-cliff; do
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf "${image}@sha256:%s " *)
done

- name: Inspect image
shell: bash
run: |
for image in orhunp/git-cliff ghcr.io/${{ github.repository_owner }}/git-cliff/git-cliff; do
docker buildx imagetools inspect "${image}:${{ steps.meta.outputs.version }}"
done

- name: Scan the image
uses: anchore/sbom-action@v0
with:
image: ghcr.io/${{ github.repository_owner }}/git-cliff/git-cliff

- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ COPY --from=planner /app/recipe.json recipe.json
ENV CARGO_NET_GIT_FETCH_WITH_CLI=true
RUN cargo chef cook --release --recipe-path recipe.json
COPY . .
RUN cargo build --release --locked --no-default-features --features github --features gitlab --features bitbucket
RUN cargo build --verbose --release --locked --no-default-features --features github --features gitlab --features bitbucket
RUN rm -f target/release/deps/git_cliff*

FROM debian:buster-slim as runner
Expand Down
Loading