From a90b1e139fc19028f9221059929766367d1d8745 Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Mon, 6 Nov 2023 15:07:22 -0800 Subject: [PATCH] ci: include scope in GHA cache --- .github/workflows/ci.yml | 2 +- buildTestImages.ts | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 95398a8c..6fee0ca4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -62,7 +62,7 @@ jobs: uses: crazy-max/ghaction-github-runtime@v3 - name: build test images - run: ./buildTestImages.ts --buildOpts="--cache-from type=gha --cache-to type=gha,mode=max" + run: ./buildTestImages.ts - name: run test images run: ./runTestImages.ts diff --git a/buildTestImages.ts b/buildTestImages.ts index 61661de6..d2a3e70f 100755 --- a/buildTestImages.ts +++ b/buildTestImages.ts @@ -8,13 +8,12 @@ import { refreshDockerfile } from './makeDockerfile'; refreshDockerfile(); const options = { - buildOpts: { type: 'string' }, match: { short: 'm', type: 'string' }, dry: { type: 'boolean' }, } as const; const { values } = parseArgs({ options }); -const { buildOpts, match, dry } = values; +const { match, dry } = values; const allProposals = readProposals(); @@ -22,14 +21,23 @@ const proposals = match ? allProposals.filter(p => p.proposalName.includes(match)) : allProposals; +function buildOptsFor(name: string) { + // use GitHub Actions Cache if enabled https://docs.docker.com/build/cache/backends/gha/ + if (process.env.ACTIONS_RUNTIME_TOKEN) { + const refName = process.env.GITHUB_REF_NAME; + return `--cache-from type=gha,scope=${refName}-${name} --cache-to type=gha,mode=max,scope=${refName}-${name}`; + } +} + for (const proposal of proposals) { if (!dry) { console.log(`\nBuilding test image for proposal ${proposal.proposalName}`); } const { name, target } = imageNameForProposalTest(proposal); + const opts = buildOptsFor(name); // 'load' to ensure the images are output to the Docker client. Seems to be necessary // for the CI docker/build-push-action to re-use the cached stages. - const cmd = `docker buildx build ${buildOpts} --load --tag ${name} --target ${target} .`; + const cmd = `docker buildx build ${opts} --load --tag ${name} --target ${target} .`; console.log(cmd); if (!dry) { execSync(cmd, { stdio: 'inherit' });