From 26e8197b82586f8f0a2d083b7c83109d1afe84de Mon Sep 17 00:00:00 2001 From: Carolyn Van Slyck Date: Fri, 24 Aug 2018 10:36:42 -0500 Subject: [PATCH] Publish docker images during CI builds (#544) master builds publish using the git describe output as the immutable tag and canary for the mutable tag. Examples: gomods/proxy:abc1234 gomods/proxy:canary tagged builds publish using the tag as the immutable tag, which is usually going to be a semver value and with latest for the mutable tag. Examples: gomods/proxy:v1.0.0 gomods/proxy:latest branch builds publish using the git describe output as the immutable tag and the branch name for the mutable tag. Examples: gomods/proxy:def4567 gomods/proxy:beta-1 --- .travis.yml | 11 ++++++++ Makefile | 4 +++ scripts/push-docker-images.sh | 47 +++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100755 scripts/push-docker-images.sh diff --git a/.travis.yml b/.travis.yml index 24eef5e7..76ae37e1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,7 @@ env: - ATHENS_MONGO_CONNECTION_STRING=mongodb://127.0.0.1:27017 - CODE_COV=1 - ATHENS_DIR=${GOPATH}/src/github.com/gomods/athens + - REGISTRY=carolynvs/ # Temporary workaround until we have access to the gomods/ registry before_script: - mkdir -p $ATHENS_DIR && rsync -azr . $ATHENS_DIR && cd $ATHENS_DIR @@ -30,3 +31,13 @@ after_success: else echo codecov not enabled; fi + +before_deploy: + - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin + +deploy: + provider: script + script: make docker-push + on: + repo: gomods/athens + all_branches: true diff --git a/Makefile b/Makefile index 224dbb0e..3f65eaa4 100644 --- a/Makefile +++ b/Makefile @@ -46,6 +46,10 @@ olympus-docker: proxy-docker: docker build -t gomods/proxy -f cmd/proxy/Dockerfile . +.PHONY: docker-push +docker-push: docker + ./scripts/push-docker-images.sh + bench: ./scripts/benchmark.sh diff --git a/scripts/push-docker-images.sh b/scripts/push-docker-images.sh new file mode 100755 index 00000000..f74229d7 --- /dev/null +++ b/scripts/push-docker-images.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +# Push our docker images to a registry +set -xeuo pipefail + +REGISTRY=${REGISTRY:-gomods/} + +# Use the travis variables when available because travis clones different than what is on a local dev machine +# VERSION = the tag if present, otherwise the short commit hash +# BRANCH = the current branch, empty if not on a branch +if [[ "${TRAVIS-}" == "true" ]]; then + VERSION=${TRAVIS_TAG:-${TRAVIS_COMMIT::7}} + BRANCH=${TRAVIS_BRANCH} +else + TAG=$(git describe --tags --exact-match 2> /dev/null || true) + COMMIT=$(git rev-parse --short=7 HEAD) + VERSION=${VERSION:-${TAG:-${COMMIT}}} + BRANCH=${BRANCH:-$(git symbolic-ref -q --short HEAD || echo "")} +fi + +# MUTABLE_TAG is the docker image tag that we will reuse between pushes, it is not a stable tag like a commit hash or tag. +if [[ "${MUTABLE_TAG:-}" == "" ]]; then + # tagged builds + if [[ "$VERSION" =~ ^v[0-9]+\.[0-9]+\.[0-9]+ ]]; then + MUTABLE_TAG="latest" + # master build + elif [[ "$BRANCH" == "master" ]]; then + MUTABLE_TAG="canary" + # branch build + else + MUTABLE_TAG=${BRANCH} + fi +fi + +REPO_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." >/dev/null && pwd )/" + +docker build -t ${REGISTRY}proxy:${VERSION} -f ${REPO_DIR}cmd/proxy/Dockerfile ${REPO_DIR} +docker build -t ${REGISTRY}olympus:${VERSION} -f ${REPO_DIR}cmd/olympus/Dockerfile ${REPO_DIR} + +# Apply the mutable tag to the immutable version +docker tag ${REGISTRY}proxy:${VERSION} ${REGISTRY}proxy:${MUTABLE_TAG} +docker tag ${REGISTRY}olympus:${VERSION} ${REGISTRY}olympus:${MUTABLE_TAG} + +docker push ${REGISTRY}proxy:${VERSION} +docker push ${REGISTRY}proxy:${MUTABLE_TAG} +docker push ${REGISTRY}olympus:${VERSION} +docker push ${REGISTRY}olympus:${MUTABLE_TAG}