Try caching images in a local registry before pushing

This commit is contained in:
2020-12-28 15:27:20 +01:00
parent fd9eae23eb
commit f9c992dcb2

View File

@ -1,5 +1,8 @@
image: docker:19.03.12
stages:
- docker
variables:
# When using dind service, we need to instruct docker to talk with
# the daemon started inside of the service. The daemon is available
@ -19,20 +22,46 @@ variables:
# Use TLS https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#tls-enabled
DOCKER_HOST: tcp://docker:2376
# Support caching images between jobs
CI_LOCAL_REGISTRY_IMAGE: "registry:5000"
# CI_LOCAL_REGISTRY_USER: registry
# CI_LOCAL_REGISTRY_PASSWORD: thisismyregistry
services:
- docker:19.03.12-dind
- name: registry:2
alias: registry
# Login to job registry and publish registry
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
build:
stage: build
# Make sure we can build a docker image
# It's pushed to the pipeline-local registry which acts as a cache
build_docker:
stage: docker
script:
- docker pull $CI_REGISTRY_IMAGE:latest || true
- docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG --tag $CI_REGISTRY_IMAGE:latest .
- docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_LOCAL_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA --tag $CI_LOCAL_REGISTRY_IMAGE:latest .
- docker push $CI_LOCAL_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
- docker push $CI_LOCAL_REGISTRY_IMAGE:latest
test_pull_local_docker:
stage: docker
script:
- docker pull $CI_LOCAL_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
push_docker:
stage: docker
script:
- docker pull $CI_LOCAL_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA || true
- docker tag $CI_LOCAL_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
- docker tag $CI_LOCAL_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA $CI_REGISTRY_IMAGE:latest
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
- docker push $CI_REGISTRY_IMAGE:latest
# - docker pull $CI_REGISTRY_IMAGE:latest || true
# - docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG --tag $CI_REGISTRY_IMAGE:latest .
# - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
# - docker push $CI_REGISTRY_IMAGE:latest
only:
- tags