Add caching to Docker builds #5
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
name: Cached Docker build | |
on: | |
push: | |
branches: | |
- "feat/add-build-cache " | |
pull_request: | |
env: | |
REGISTRY: europe-north1-docker.pkg.dev/artifact-registry-5n/dapla-metadata-docker/datadoc | |
IMAGE: datadoc | |
TAG: ${{ github.ref_name }}-${{ github.sha }} | |
jobs: | |
docker-cached: | |
permissions: | |
contents: "read" | |
id-token: "write" | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: "Authenticate to Google Cloud" | |
id: "auth" | |
uses: "google-github-actions/[email protected]" | |
with: | |
workload_identity_provider: "projects/848539402404/locations/global/workloadIdentityPools/gh-actions/providers/gh-actions" | |
service_account: "gh-actions-dapla-metadata@artifact-registry-5n.iam.gserviceaccount.com" | |
token_format: "access_token" | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
id: buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: "oauth2accesstoken" | |
password: "${{ steps.auth.outputs.access_token }}" | |
- name: Docker meta | |
id: metadata | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.REGISTRY }}/${{ env.IMAGE }} | |
# Docker tags based on the following events/attributes | |
tags: | | |
type=schedule | |
type=ref,event=branch | |
type=ref,event=pr | |
type=semver,pattern=v{{version}} | |
type=semver,pattern=v{{major}}.{{minor}} | |
type=semver,pattern=v{{major}} | |
type=sha | |
type=raw,value=${{ env.TAG }}, enable=true | |
- name: Python Build Cache for Docker | |
uses: actions/cache@v3 | |
with: | |
path: python-build-cache | |
key: ${{ runner.os }}-python-build-cache-${{ hashFiles('**/poetry.lock') }} | |
- name: inject python-build-cache into docker | |
# v1 was composed of two actions: "inject" and "extract". | |
# v2 is unified to a single action. | |
uses: reproducible-containers/[email protected] | |
with: | |
cache-source: python-build-cache | |
- name: Build and push | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
platforms: linux/amd64,linux/arm64 | |
file: Dockerfile | |
push: true | |
tags: | | |
${{ steps.metadata.outputs.tags }} | |
labels: ${{ steps.metadata.outputs.labels }} | |
- name: Output image digest 1 | |
run: echo ${{ steps.docker_build.outputs.digest }} |