Skip to content

Commit

Permalink
Add fast Docker layer caching to CI
Browse files Browse the repository at this point in the history
This is done using Docker's own released actions in combination with GitHub's caching actions.
A local package registry is used, and has been added to Dockerfile(s) as ARGs.
These ARGs do not interfere with local Docker builds in my testing.
Individual caches are used to speed up caching (see: docker/build-push-action#252 (comment))
  • Loading branch information
andersfischernielsen committed Feb 17, 2021
1 parent 9fab3e5 commit d764afa
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2
updates:
# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
69 changes: 61 additions & 8 deletions .github/workflows/dockerimage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,68 @@ name: Docker Image CI
on: [push]

jobs:
build:
build_test_project:
runs-on: ubuntu-latest
services:
registry:
image: registry:2
ports:
- 5000:5000
steps:
- uses: actions/checkout@v2
- name: Build colonists-shared
run: docker build --tag colonists/colonists-shared:$(git log --pretty=format:'%h' -n 1) --build-arg BRANCH_TAG=$(git log --pretty=format:'%h' -n 1) ./colonists-shared
- name: Build colonists-server
run: docker build --tag colonists/colonists-server:$(git log --pretty=format:'%h' -n 1) --build-arg BRANCH_TAG=$(git log --pretty=format:'%h' -n 1) ./colonists-server
- name: Build colonists-client
run: docker build --tag colonists/colonists-client:$(git log --pretty=format:'%h' -n 1) --build-arg BRANCH_TAG=$(git log --pretty=format:'%h' -n 1) ./colonists-client
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
with:
driver-opts: network=host
- name: Set colonists-shared cache
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache-shared
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Set colonists-server cache
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache-server
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Set colonists-client cache
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache-client
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Build shared
uses: docker/build-push-action@v2
with:
push: true
tags: localhost:5000/colonists/colonists-shared
cache-from: type=local,src=/tmp/.buildx-cache-shared
cache-to: type=local,dest=/tmp/.buildx-cache-shared
context: ./colonists-shared
- name: Run shared tests
run: docker run --rm colonists/colonists-shared:$(git log --pretty=format:'%h' -n 1)
run: docker run --rm localhost:5000/colonists/colonists-shared
- name: Build server
uses: docker/build-push-action@v2
with:
push: false
tags: colonists/colonists-server
cache-from: type=local,src=/tmp/.buildx-cache-server
cache-to: type=local,dest=/tmp/.buildx-cache-server
context: ./colonists-server
build-args: |
"LOCAL_REGISTRY=localhost:5000/"
- name: Build client
uses: docker/build-push-action@v2
with:
push: false
tags: colonists/colonists-client
cache-from: type=local,src=/tmp/.buildx-cache-client
cache-to: type=local,dest=/tmp/.buildx-cache-client
context: ./colonists-client
build-args: |
"LOCAL_REGISTRY=localhost:5000/"
3 changes: 2 additions & 1 deletion colonists-client/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Library
ARG BRANCH_TAG=latest
FROM colonists/colonists-shared:${BRANCH_TAG} as library
ARG LOCAL_REGISTRY=
FROM ${LOCAL_REGISTRY}colonists/colonists-shared:${BRANCH_TAG} as library

# Cache
FROM node:alpine as dependency-cache-client
Expand Down
3 changes: 2 additions & 1 deletion colonists-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ARG BRANCH_TAG=latest
FROM colonists/colonists-shared:${BRANCH_TAG} as library
ARG LOCAL_REGISTRY
FROM ${LOCAL_REGISTRY}colonists/colonists-shared:${BRANCH_TAG} as library

FROM node as dependency-cache-server
WORKDIR /cache
Expand Down

0 comments on commit d764afa

Please sign in to comment.