Skip to content

Commit

Permalink
Fix build cache in CI not being used properly
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecrs committed Mar 20, 2024
1 parent b7c7edb commit c019d2b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 53 deletions.
4 changes: 3 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
},
"overrideCommand": false,
"mounts": [
"source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind"
"source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind",
// This allows sharing the build cache when running in GitHub Actions
"source=${localEnv:HOME}/.docker,target=/home/devcontainer/.docker,type=bind"
],
"runArgs": ["--network=host"],
"postCreateCommand": [".devcontainer/post_create.sh"],
Expand Down
127 changes: 75 additions & 52 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,81 @@ on:
merge_group:

jobs:
ci:
build:
runs-on: ubuntu-latest

strategy:
matrix:
architecture:
- amd64
- arm64

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

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

- name: Build to export cache
uses: docker/bake-action@v4
with:
set: |
devcontainer.platform=linux/${{ matrix.architecture }}
jenkins-agent-dind.platform=linux/${{ matrix.architecture }}
devcontainer.cache-from=type=gha,scope=devcontainer-${{ matrix.architecture }}
jenkins-agent-dind.cache-from=type=gha,scope=jenkins-agent-dind-${{ matrix.architecture }}
devcontainer.cache-to=type=gha,scope=devcontainer-${{ matrix.architecture }},mode=max
jenkins-agent-dind.cache-to=type=gha,scope=jenkins-agent-dind-${{ matrix.architecture }},mode=max
test:
needs: build

runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: network=host

- name: Build to import cache
uses: docker/bake-action@v4
with:
set: |
devcontainer.cache-from=type=gha,scope=devcontainer-amd64
jenkins-agent-dind.cache-from=type=gha,scope=jenkins-agent-dind-amd64
load: true

- name: Test
uses: devcontainers/[email protected]
with:
runCmd: ./test.sh

push:
needs: test

runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

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

- name: Docker meta (devcontainer)
id: docker-meta-devcontainer
uses: docker/metadata-action@v5
Expand Down Expand Up @@ -62,56 +130,6 @@ jobs:
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: network=host

- name: Install pkgx
uses: pkgxdev/setup@v2

- name: Install test dependencies
run: .devcontainer/post_create.sh

# Build devcontainer first otherwise docker bake will start building devcontainer twice
# as jenkins-agent-dind depends on it.
- name: Build (devcontainer only)
uses: docker/bake-action@v4
with:
targets: devcontainer
files: |
./docker-bake.hcl
${{ steps.docker-meta-devcontainer.outputs.bake-file }}
set: |
*.platform=linux/amd64,linux/arm64
devcontainer.cache-from=type=gha,scope=cached-stage-devcontainer
devcontainer.cache-to=type=gha,scope=cached-stage-devcontainer,mode=max
- name: Build
uses: docker/bake-action@v4
with:
files: |
./docker-bake.hcl
${{ steps.docker-meta-devcontainer.outputs.bake-file }}
${{ steps.docker-meta-jenkins-agent-dind.outputs.bake-file }}
set: |
*.platform=linux/amd64,linux/arm64
devcontainer.cache-from=type=gha,scope=cached-stage-devcontainer
jenkins-agent-dind.cache-from=type=gha,scope=cached-stage-jenkins-agent-dind
devcontainer-jenkins-agent-dind.cache-from=type=gha,scope=cached-stage-devcontainer-jenkins-agent-dind
devcontainer.cache-to=type=gha,scope=cached-stage-devcontainer,mode=max
jenkins-agent-dind.cache-to=type=gha,scope=cached-stage-jenkins-agent-dind,mode=max
devcontainer-jenkins-agent-dind.cache-to=type=gha,scope=cached-stage-devcontainer-jenkins-agent-dind,mode=max
*.cache-to=type=inline
- name: Test
env:
GITHUB_ACTIONS: "true"
run: ./test.sh

- name: Push
uses: docker/bake-action@v4
with:
Expand All @@ -120,5 +138,10 @@ jobs:
${{ steps.docker-meta-devcontainer.outputs.bake-file }}
${{ steps.docker-meta-jenkins-agent-dind.outputs.bake-file }}
set: |
*.platform=linux/amd64,linux/arm64
devcontainer.platform=linux/amd64,linux/arm64
jenkins-agent-dind.platform=linux/amd64,linux/arm64
devcontainer.cache-from=type=gha,scope=devcontainer-amd64
devcontainer.cache-from=type=gha,scope=devcontainer-arm64
jenkins-agent-dind.cache-from=type=gha,scope=jenkins-agent-dind-amd64
jenkins-agent-dind.cache-from=type=gha,scope=jenkins-agent-dind-arm64
push: true

0 comments on commit c019d2b

Please sign in to comment.