Skip to content

Additional Cargo & Docker cache improvements #63

Additional Cargo & Docker cache improvements

Additional Cargo & Docker cache improvements #63

Workflow file for this run

name: Build container image
on:
workflow_dispatch:
push:
branches:
- 'musubi.moe/main'
- 'musubi.moe/develop'
tags:
- 'v[0-9]+.[0-9]+.[0-9]+\+musubi-moe*'
paths:
- .github/workflows/build-image.yml
- Dockerfile
- src/**.rs
- Cargo.*
pull_request:
paths:
- .github/workflows/build-image.yml
- Dockerfile
- src/**.rs
- Cargo.*
permissions:
contents: read
packages: write
env:
DOCKER_IMAGE: ghcr.io/${{ github.repository }}
jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
docker-platforms: ['linux/amd64', 'linux/arm64']
steps:
- uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
with:
platforms: ${{ matrix.docker-platforms }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to the GitHub Container registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
if: github.event_name != 'pull_request'
- run: echo "DOCKER_IMAGE=${DOCKER_IMAGE,,}" >>${GITHUB_ENV} # repository name must be lowercase
- name: Generate Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.DOCKER_IMAGE }}
- name: Build and push by digest
id: build
uses: docker/build-push-action@v5
with:
context: .
platforms: ${{ matrix.docker-platforms }}
cache-from: type=gha,scope=$GITHUB_REF_NAME/${{ matrix.docker-platforms }}
cache-to: type=gha,scope=$GITHUB_REF_NAME/${{ matrix.docker-platforms }},mode=max
labels: ${{ steps.meta.outputs.labels }}
outputs: type=image,name=${{ env.DOCKER_IMAGE }},push-by-digest=${{ github.event_name != 'pull_request' }},name-canonical=true,push=${{ github.event_name != 'pull_request' }}
- name: Export digest
id: digest
run: |
digest="${{ steps.build.outputs.digest }}"
echo "digest=${digest#sha256:}" >> $GITHUB_OUTPUT
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: ${{ steps.digest.outputs.digest }}
path: /dev/null
retention-days: 1
mix:
if: github.event_name != 'pull_request'
needs:
- build
runs-on: ubuntu-latest
steps:
- name: Create DIGESTS_PATH
run: |
export DIGESTS_PATH="$RUNNER_TEMP/$(uuidgen)/digests"
echo "DIGESTS_PATH=$DIGESTS_PATH" >>${GITHUB_ENV}
- name: Download digests
uses: actions/download-artifact@v4
with:
path: ${{ env.DIGESTS_PATH }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to the GitHub Container registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
if: github.event_name != 'pull_request'
- run: echo "DOCKER_IMAGE=${DOCKER_IMAGE,,}" >>${GITHUB_ENV} # repository name must be lowercase
- name: Generate Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.DOCKER_IMAGE }}
flavor: |
latest=auto
tags: |
type=edge,branch=musubi.moe/main
type=edge,branch=musubi.moe/develop
type=semver,pattern={{raw}}
type=ref,event=pr
- name: Create manifest list and push
working-directory: ${{ env.DIGESTS_PATH }}
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.DOCKER_IMAGE }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ env.DOCKER_IMAGE }}:${{ steps.meta.outputs.version }}