Skip to content

Commit

Permalink
ci(docker): distribute build across runners
Browse files Browse the repository at this point in the history
  • Loading branch information
crazy-max committed Oct 14, 2023
1 parent 2742a70 commit a39e4c6
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 33 deletions.
159 changes: 126 additions & 33 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,57 @@ jobs:
PYPI_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: twine upload --disable-progress-bar -u ${PYPI_USERNAME} -p ${PYPI_PASSWORD} dist/*

docker:
docker-test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build
uses: docker/build-push-action@v5
with:
context: .
load: true
tags: mkdocs-material:test

- name: Check Docker image
working-directory: /tmp
env:
REPO_FULL_NAME: '${{ github.event.repository.full_name }}'
run: |
docker run --rm -i -v ${PWD}:/docs mkdocs-material:test new .
docker run --rm -i -v ${PWD}:/docs mkdocs-material:test build
docker-prepare:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.platforms.outputs.matrix }}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Create matrix
id: platforms
run: |
echo "matrix=$(docker buildx bake image-all --print | jq -cr '.target."image-all".platforms')" >>${GITHUB_OUTPUT}
- name: Show matrix
run: |
echo ${{ steps.platforms.outputs.matrix }}
docker-build:
runs-on: ubuntu-latest
needs:
- docker-test
- docker-prepare
strategy:
fail-fast: false
matrix:
platform: ${{ fromJson(needs.docker-prepare.outputs.matrix) }}
steps:
- name: Checkout repository
uses: actions/checkout@v4

Expand Down Expand Up @@ -163,43 +210,89 @@ jobs:
flavor: |
latest=${{ github.event.release.prerelease == false }}
- name: Build Docker image
uses: docker/build-push-action@v5
with:
context: .
load: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

- name: Check Docker image
working-directory: /tmp
env:
REPO_FULL_NAME: '${{ github.event.repository.full_name }}'
- name: Rename meta bake definition file
run: |
docker run --rm -i -v ${PWD}:/docs ${REPO_FULL_NAME,,}:${{ steps.meta.outputs.version }} new .
docker run --rm -i -v ${PWD}:/docs ${REPO_FULL_NAME,,}:${{ steps.meta.outputs.version }} build
mv "${{ steps.meta.outputs.bake-file }}" "/tmp/bake-meta.json"
- name: Set platforms
if: github.event_name == 'release'
- name: Upload meta bake definition
uses: actions/upload-artifact@v3
with:
name: bake-meta
path: /tmp/bake-meta.json
if-no-files-found: error
retention-days: 1

- name: Build
id: bake
uses: docker/bake-action@v4
with:
files: |
./docker-bake.hcl
/tmp/bake-meta.json
targets: image
set: |
*.tags=
*.platform=${{ matrix.platform }}
*.output=type=image,"name=${{ github.event.repository.full_name }},ghcr.io/${{ github.event.repository.full_name }}",push-by-digest=true,name-canonical=true,push=${{ github.event_name == 'release' }}
- name: Export digest
run: |
echo "PLATFORMS=linux/amd64,linux/arm64,linux/arm/v7" >> $GITHUB_ENV
mkdir -p /tmp/digests
digest="${{ fromJSON(steps.bake.outputs.metadata).image['containerimage.digest'] }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Publish Docker image
uses: docker/build-push-action@v5
- name: Upload digest
uses: actions/upload-artifact@v3
with:
context: .
platforms: ${{ env.PLATFORMS }}
push: ${{ github.event_name == 'release' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
name: digests
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1

- name: Check manifest
if: github.event_name == 'release'
run: |
docker buildx imagetools inspect ${{ github.event.repository.full_name }}:${{ steps.meta.outputs.version }}
docker-merge:
runs-on: ubuntu-latest
if: github.event_name == 'release'
needs:
- docker-build
steps:
- name: Download meta bake definition
uses: actions/download-artifact@v3
with:
name: bake-meta
path: /tmp

- name: Inspect image
if: github.event_name == 'release'
- name: Download digests
uses: actions/download-artifact@v3
with:
name: digests
path: /tmp/digests

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

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

- name: Create manifest list and push
working-directory: /tmp/digests
run: |
docker buildx imagetools create $(jq -cr '.target."docker-metadata-action".tags | map(select(startswith("${{ github.event.repository.full_name }}")) | "-t " + .) | join(" ")' /tmp/bake-meta.json) \
$(printf '${{ github.event.repository.full_name }}@sha256:%s ' *)
docker buildx imagetools create $(jq -cr '.target."docker-metadata-action".tags | map(select(startswith("ghcr.io/${{ github.event.repository.full_name }}")) | "-t " + .) | join(" ")' /tmp/bake-meta.json) \
$(printf 'ghcr.io/${{ github.event.repository.full_name }}@sha256:%s ' *)
-
name: Inspect image
run: |
docker pull ${{ github.event.repository.full_name }}:${{ steps.meta.outputs.version }}
docker image inspect ${{ github.event.repository.full_name }}:${{ steps.meta.outputs.version }}
tag=$(jq -r '.target."docker-metadata-action".args.DOCKER_META_VERSION' /tmp/bake-meta.json)
docker buildx imagetools inspect ${{ github.event.repository.full_name }}:${tag}
docker buildx imagetools inspect ghcr.io/${{ github.event.repository.full_name }}:${tag}
31 changes: 31 additions & 0 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
variable "DEFAULT_TAG" {
default = "mkdocs-material:local"
}

// Special target: https://github.com/docker/metadata-action#bake-definition
target "docker-metadata-action" {
tags = ["${DEFAULT_TAG}"]
}

// Default target if none specified
group "default" {
targets = ["image-local"]
}

target "image" {
inherits = ["docker-metadata-action"]
}

target "image-local" {
inherits = ["image"]
output = ["type=docker"]
}

target "image-all" {
inherits = ["image"]
platforms = [
"linux/amd64",
"linux/arm/v7",
"linux/arm64"
]
}

0 comments on commit a39e4c6

Please sign in to comment.