Skip to content

Commit

Permalink
fix: correct docker image name; simplify build pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
placer14 committed Feb 19, 2021
1 parent 625a7cc commit b1c9873
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 50 deletions.
92 changes: 55 additions & 37 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,50 +32,68 @@ commands:
- run: sudo apt-get update
- run: git submodule sync
- run: git submodule update --init
build-docker-and-maybe-push:
parameters:
push:
description: whether to push created docker image after build
type: boolean
default: false
publish-docker-master-production:
steps:
- checkout
- setup_remote_docker:
version: "18.09.3"
- run:
name: Build Dev Docker image
command: docker build -t ${IMAGE_NAME}-dev -f Dockerfile.dev .
- run:
name: Build Production Docker image
command: docker build -t $IMAGE_NAME -f Dockerfile .
- when:
condition: << parameters.push >>
steps:
- run:
name: Publish Dev Docker Image to Docker Hub
command: |
echo "$DOCKERHUB_PASS" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
tag_suffix=$(echo "$CIRCLE_SHA1" | cut -c 1-8)
./scripts/push-docker-tags.sh "${IMAGE_NAME}-dev" "$CIRCLE_SHA1" "${CIRCLE_BRANCH}-${tag_suffix}-dev"
- run:
name: Publish Production Docker Image to Docker Hub
command: |
echo "$DOCKERHUB_PASS" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
tag_suffix=$(echo "$CIRCLE_SHA1" | cut -c 1-8)
./scripts/push-docker-tags.sh "${IMAGE_NAME}-dev" "$CIRCLE_SHA1" "${CIRCLE_BRANCH}-${tag_suffix}"
when: always
- run:
name: Publish Production Docker Image to Docker Hub
command: |
echo "$DOCKERHUB_PASS" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
tag_suffix=$(echo "$CIRCLE_SHA1" | cut -c 1-8)
./scripts/push-docker-tags.sh "${IMAGE_NAME}" "$CIRCLE_SHA1" "${CIRCLE_BRANCH}-${tag_suffix}"
publish-docker-master-dev:
steps:
- run:
name: Build Dev Docker image
command: docker build -t ${IMAGE_NAME} -f Dockerfile.dev .
- run:
name: Publish Dev Docker Image to Docker Hub
command: |
echo "$DOCKERHUB_PASS" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
tag_suffix=$(echo "$CIRCLE_SHA1" | cut -c 1-8)
./scripts/push-docker-tags.sh "${IMAGE_NAME}" "$CIRCLE_SHA1" "${CIRCLE_BRANCH}-${tag_suffix}-dev"
publish-docker-semver-production:
steps:
- run:
name: Build Production Semver Docker image
command: docker build -t ${IMAGE_NAME} -f Dockerfile .
- run:
name: Publish Production Semver Docker Image to Docker Hub
command: |
echo "$DOCKERHUB_PASS" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
./scripts/push-docker-tags.sh "${IMAGE_NAME}" "$CIRCLE_SHA1" "${CIRCLE_TAG}"
publish-docker-semver-dev:
steps:
- run:
name: Build Dev Semver Docker image
command: docker build -t ${IMAGE_NAME} -f Dockerfile.dev .
- run:
name: Publish Dev Semver Docker Image to Docker Hub
command: |
echo "$DOCKERHUB_PASS" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
./scripts/push-docker-tags.sh "${IMAGE_NAME}" "$CIRCLE_SHA1" "${CIRCLE_TAG}-dev"
jobs:
build-push-master:
publish-docker-from-master:
executor: dockerizer
steps:
- build-docker-and-maybe-push:
push: true
build-push-semver-tag:
- checkout
- setup_remote_docker:
docker_layer_caching: true
version: "18.09.3"
- publish-docker-master-dev
- publish-docker-master-production
publish-docker-from-tag:
executor: dockerizer
steps:
- build-docker-and-maybe-push:
push: true
- checkout
- setup_remote_docker:
docker_layer_caching: true
version: "18.09.3"
- publish-docker-semver-dev
- publish-docker-semver-production
mod-tidy-check:
executor: golang
steps:
Expand Down Expand Up @@ -124,18 +142,18 @@ workflows:
jobs:
- mod-tidy-check
- test
docker-and-deploy:
build-docker-images:
# `build-push-*` runs on master or main branches and tags that look like semver
# see: https://circleci.com/docs/2.0/workflows/#executing-workflows-for-a-git-tag
jobs:
- build-push-master:
- publish-docker-from-master:
# build and push latest master docker image
filters:
branches:
only: /^(master|main)$/
tags:
ignore: /.*/
- build-push-semver-tag:
- publish-docker-from-tag:
# build and push semver tags docker image
filters:
branches:
Expand Down
19 changes: 6 additions & 13 deletions scripts/push-docker-tags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@
# what tag, if any, to push to dockerhub.
#
# Usage:
# ./push-docker-tags.sh <image name> <git commit sha1> [git tag name] [dry run]
# ./push-docker-tags.sh <image name> <git commit sha1> <docker tag> [dry run]
#
# Example:
# # dry run. pass a 5th arg to have it print what it would do rather than do it.
# ./push-docker-tags.sh myiamge testingsha "" dryrun
#
# # push tag for commit on the main branch
# ./push-docker-tags.sh myimage testingsha
#
# # push tag for a new release tag
# ./push-docker-tags.sh myimage testingsha v0.5.0
#
Expand All @@ -26,10 +23,10 @@
#
set -euo pipefail

if [[ $# -lt 2 ]] ; then
echo 'At least 2 args required. Pass 4 args for a dry run.'
if [[ $# -lt 3 ]] ; then
echo 'At least 3 args required. Pass 4 args for a dry run.'
echo 'Usage:'
echo './push-docker-tags.sh <image name> <git commit sha1> [git tag name] [dry run]'
echo './push-docker-tags.sh <image name> <git commit sha1> <docker tag> [dry run]'
exit 1
fi

Expand All @@ -40,7 +37,7 @@ GIT_TAG=${3:-""}
DRY_RUN=${4:-false}

pushTag () {
local IMAGE_TAG="${1/\//-}"
local IMAGE_TAG="${1//\//-}"
if [ "$DRY_RUN" != false ]; then
echo "DRY RUN!"
echo docker tag "$IMAGE_NAME" "$IMAGE_NAME:$IMAGE_TAG"
Expand All @@ -52,8 +49,4 @@ pushTag () {
fi
}

if [ -z "${GIT_TAG}" ]; then
pushTag "${GIT_BRANCH}-${GIT_SHA1_SHORT}"
else
pushTag "${GIT_TAG}"
fi
pushTag "${GIT_TAG}"

0 comments on commit b1c9873

Please sign in to comment.