Skip to content

Instantly share code, notes, and snippets.

@lawrencegripper
Created November 7, 2019 00:26
Show Gist options
  • Save lawrencegripper/5979977b895d345fe688c5c26e99748f to your computer and use it in GitHub Desktop.
Save lawrencegripper/5979977b895d345fe688c5c26e99748f to your computer and use it in GitHub Desktop.
Docker cache
trigger:
- master
pool:
vmImage: 'Ubuntu-16.04'
steps:
# Cache the docker image file
- task: CacheBeta@0
inputs:
key: go-cache | go.sum
path: ".gocache"
restoreKeys: go-cache
displayName: Cache go mod cache
# Cache the docker image file
- task: CacheBeta@0
inputs:
key: docker-image | .devcontainer/**
path: ".dockercache"
restoreKeys: docker-image
cacheHitVar: DOCKER_CACHE_HIT
displayName: Cache docker layers
- script: |
bash -f ./ci.sh
displayName: 'Run CI'
#! /bin/bash
set -e
set -x
# Get storage drive details
docker info
# Create .dockercache directory
mkdir -p ./.dockercache/
# Import devcontainer from cache to speed up build
if [ -f ".dockercache/devcontainer.tar" ];
then
echo "-------> Restoring docker image"
time docker load -i .dockercache/devcontainer.tar
fi
echo "-------> Building devcontainer"
# Use the devcontainer to run the build as it has all the environment setup that we need
time docker build --cache-from devcontainer:latest -t devcontainer -f ./.devcontainer/Dockerfile ./.devcontainer
# Create a directory for go mod cache
mkdir -p ${PWD}/.gocache
echo "-------> Building code"
# Run `make` to build and test the code
time docker run -v ${PWD}/.gocache:/go/pkg/ -v /var/run/docker.sock:/var/run/docker.sock -v ${PWD}:/src --workdir /src --entrypoint /bin/bash devcontainer -c "make"
# Ensure .gocache permmissions correct for build to save cache
sudo chown -R $USER ./.gocache
# If the current cached image is out of date save devcontainer so it can be cached
if [ $DOCKER_CACHE_HIT != "true" ];
then
echo "-------> Saving docker image"
time docker image save -o ./.dockercache/devcontainer.tar devcontainer
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment