Skip to content

Instantly share code, notes, and snippets.

@Frizi
Last active November 12, 2018 22:53
Show Gist options
  • Save Frizi/bcd539a23aeb9d4e469673195113eb24 to your computer and use it in GitHub Desktop.
Save Frizi/bcd539a23aeb9d4e469673195113eb24 to your computer and use it in GitHub Desktop.
gcloud cached docker build setup
#!/usr/bin/env bash
cd `dirname $0`/../..
set -e
IMAGE_NAME=eu.gcr.io/${PROJECT_ID}/${REPO_NAME}/$1
# Use multiple cache sources that make sense in given context.
# This setup is good for pull-request based CI, as most branches
# are based on master branch, each commit has a high chance
# to hit the cache of it's direct parent.
IMAGE_MASTER=${IMAGE_NAME}:master
IMAGE_BRANCH=${IMAGE_NAME}:${BRANCH_NAME}
IMAGE_COMMIT=${IMAGE_NAME}:${COMMIT_SHA}
IMAGE_LATEST=${IMAGE_NAME}:latest
docker pull "${IMAGE_MASTER}" || true &
docker pull "${IMAGE_BRANCH}" || true &
wait
# use different dockerfile depending on build stage
docker build \
--cache-from "${IMAGE_MASTER}" \
--cache-from "${IMAGE_BRANCH}" \
-t "${IMAGE_LATEST}" \
-t "${IMAGE_BRANCH}" \
-t "${IMAGE_COMMIT}" \
-f "ci/Dockerfile.$1" .
steps:
# Build ui image
- id: build-ui
name: gcr.io/cloud-builders/docker
entrypoint: 'scripts/build.sh'
args: ['ui']
env:
- REPO_NAME=$REPO_NAME
- COMMIT_SHA=$COMMIT_SHA
- PROJECT_ID=$PROJECT_ID
- BRANCH_NAME=$BRANCH_NAME
waitFor: ['-']
# Build server image
- id: build-server
name: gcr.io/cloud-builders/docker
entrypoint: 'scripts/build.sh'
args: ['server']
env:
- REPO_NAME=$REPO_NAME
- COMMIT_SHA=$COMMIT_SHA
- PROJECT_ID=$PROJECT_ID
- BRANCH_NAME=$BRANCH_NAME
waitFor: ['-']
# Build testing image
# You don't need to "COPY" your test source files in dockerfile.
# Sources will be mounted as volume to the build container in next step.
- id: build-test
name: gcr.io/cloud-builders/docker
entrypoint: 'scripts/build.sh'
args: ['test']
env:
- REPO_NAME=$REPO_NAME
- COMMIT_SHA=$COMMIT_SHA
- PROJECT_ID=$PROJECT_ID
- BRANCH_NAME=$BRANCH_NAME
waitFor: ['-']
# Test built images
- id: test
# Prepare an image with your testing environment, e.g. selenium, chrome and node.
# You may use an image that was build in the same config in previous steps.
name: eu.gcr.io/${PROJECT_ID}/${REPO_NAME}/test:${COMMIT_SHA}
args: ['scripts/test.sh']
# you will probably need some envs to use inside your built image to resolve image names
env:
- REPO_NAME=$REPO_NAME
- COMMIT_SHA=$COMMIT_SHA
- PROJECT_ID=$PROJECT_ID
waitFor:
- build-test
- build-ui
- build-server
images:
- eu.gcr.io/${PROJECT_ID}/${REPO_NAME}/ui:latest
- eu.gcr.io/${PROJECT_ID}/${REPO_NAME}/ui:${BRANCH_NAME}
- eu.gcr.io/${PROJECT_ID}/${REPO_NAME}/ui:${COMMIT_SHA}
- eu.gcr.io/${PROJECT_ID}/${REPO_NAME}/server:latest
- eu.gcr.io/${PROJECT_ID}/${REPO_NAME}/server:${BRANCH_NAME}
- eu.gcr.io/${PROJECT_ID}/${REPO_NAME}/server:${COMMIT_SHA}
- eu.gcr.io/${PROJECT_ID}/${REPO_NAME}/test:latest
- eu.gcr.io/${PROJECT_ID}/${REPO_NAME}/test:${BRANCH_NAME}
- eu.gcr.io/${PROJECT_ID}/${REPO_NAME}/test:${COMMIT_SHA}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment