Last active
February 6, 2019 13:10
-
-
Save V1ncNet/08d448aaaaa1470da9ef532074b2b8ac to your computer and use it in GitHub Desktop.
A Docker tag and push script for use with Travis CI. Best results with git tags (vX.X.X).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
language: java | |
jdk: | |
- oraclejdk8 | |
branches: | |
only: | |
- master | |
services: | |
- docker | |
cache: | |
directories: | |
- $HOME/.m2 | |
env: | |
- DOCKER_REPO_SLUG=<your_username>/<your_repository_name> | |
install: | |
- mvn -DskipTests install | |
script: | |
- mvn test | |
after_success: | |
- docker build -t $DOCKER_REPO_SLUG:latest . | |
deploy: | |
- provider: script | |
script: bash docker_push.sh | |
on: | |
branch: master | |
- provider: script | |
script: bash docker_push.sh | |
on: | |
tags: true | |
notifications: | |
email: | |
recipients: | |
- <your_email_address> | |
on_success: never | |
on_failure: always |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Preamble: This script unfolds its full power if the git tags have the following format: vX.X.X | |
TAGS=() | |
# Splits a tag number e.g. v3.1.0 into an array of ( 3 1 0 ). The given tag must start with a lowercase "v" otherwise | |
# the validation fails. The array will be stored in the TAGS variable. | |
function split() { | |
local tag=$1 | |
if [[ ${tag} =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then | |
tag=$(echo ${tag} | cut -d "v" -f 2) # Removes "v" from the tag. Eg v3.1.0 to 3.1.0 | |
IFS='.' # Overrides the internal file separator | |
TAGS=(${tag}) # Turns the tag into an array | |
IFS=$OIFS # Restores the original separator | |
else | |
echo "'$tag' doesn't seem to be an acceptable tag" | |
exit 1 | |
fi | |
return 0 | |
} | |
# Takes any string and tags the :latest Docker image. The image will be pushed to the Docker registry afterwards. | |
function tag_and_push() { | |
local tag=$1 | |
# Omit the :latest tag | |
[[ ${tag} != "latest" ]] && docker tag $DOCKER_REPO_SLUG:latest $DOCKER_REPO_SLUG:${tag} | |
docker push $DOCKER_REPO_SLUG:${tag} | |
return 0 | |
} | |
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin || (echo "Unable to login"; exit 1) | |
# Distinguish between the push and cron event of the Travis CI. A push event will always leeds to a new :edge Docker | |
# image, while cron leeds to a new :nightly image. | |
# More about Travis events and environment variables on https://docs.travis-ci.com/user/environment-variables/ | |
case $TRAVIS_EVENT_TYPE in | |
"push") | |
tag_and_push "edge" | |
;; | |
"cron") | |
# Only tag and push with nightly if the branch matches master | |
[[ $TRAVIS_BRANCH = "master" ]] && tag_and_push "nightly" | |
;; | |
*) | |
echo "Unknown Travis event type" | |
exit 1 | |
;; | |
esac | |
# Matching branch name and git tag implies a new release. The Docker image will be tagged with :latest and convenient | |
# version numbers. E.g. v0.0.1=>0.0.1 v0.1.0=>0.1,0.1.0 v0.1.2=>0.1,0.1.2 v1.0.0=>1,1.0,1.0.0 v1.0.3=>1,1.0,1.0.3 | |
# v1.2.0=>1,1.2,1.2.0 v1.2.3=>1,1.2,1.2.3 | |
if [[ $TRAVIS_TAG = $TRAVIS_BRANCH ]]; then | |
split $TRAVIS_TAG | |
MAJOR=${TAGS[0]} | |
MINOR=${MAJOR}.${TAGS[1]} | |
PATCH=${MINOR}.${TAGS[2]} | |
tag_and_push "latest" # The git tag must be valid otherwise the script does not push the :latest. | |
[[ ${TAGS[0]} -gt 0 ]] && tag_and_push ${MAJOR} | |
[[ ${TAGS[0]} -gt 0 || ${TAGS[1]} -gt 0 ]] && tag_and_push ${MINOR} | |
[[ ${TAGS[0]} -gt 0 || ${TAGS[1]} -gt 0 && ${TAGS[2]} -gt 0 ]] && tag_and_push ${PATCH} | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment