|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +source hack/lib/common.sh |
| 4 | + |
| 5 | +semver_regex="^v(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)$" |
| 6 | + |
| 7 | +# |
| 8 | +# push_image_tags <source_image> <push_image> |
| 9 | +# |
| 10 | +# push_image_tags tags the source docker image with zero or more |
| 11 | +# image tags based on TravisCI environment variables and the |
| 12 | +# presence of git tags in the repository of the current working |
| 13 | +# directory. If a second argument is present, it will be used as |
| 14 | +# the base image name in pushed image tags. |
| 15 | +# |
| 16 | +function push_image_tags() { |
| 17 | + source_image=$1; shift || fatal "${FUNCNAME} usage error" |
| 18 | + push_image=$1; shift || push_image=$source_image |
| 19 | + |
| 20 | + print_image_info $source_image |
| 21 | + print_git_tags |
| 22 | + docker_login |
| 23 | + check_can_push || return 0 |
| 24 | + |
| 25 | + images=$(get_image_tags $push_image) |
| 26 | + |
| 27 | + for image in $images; do |
| 28 | + docker tag "$source_image" "$image" |
| 29 | + docker push "$image" |
| 30 | + done |
| 31 | +} |
| 32 | + |
| 33 | +# |
| 34 | +# print_image_info <image_name> |
| 35 | +# |
| 36 | +# print_image_info prints helpful information about a docker |
| 37 | +# image. |
| 38 | +# |
| 39 | +function print_image_info() { |
| 40 | + image_name=$1; shift || fatal "${FUNCNAME} usage error" |
| 41 | + image_id=$(docker inspect "$image_name" -f "{{.Id}}") |
| 42 | + image_created=$(docker inspect "$image_name" -f "{{.Created}}") |
| 43 | + |
| 44 | + if [[ -n "$image_id" ]]; then |
| 45 | + echo "Docker image info:" |
| 46 | + echo " Name: $image_name" |
| 47 | + echo " ID: $image_id" |
| 48 | + echo " Created: $image_created" |
| 49 | + echo "" |
| 50 | + else |
| 51 | + echo "Could not find docker image \"$image_name\"" |
| 52 | + return 1 |
| 53 | + fi |
| 54 | +} |
| 55 | + |
| 56 | +# |
| 57 | +# print_git_tags |
| 58 | +# |
| 59 | +# print_git_tags prints all tags present in the git repository. |
| 60 | +# |
| 61 | +function print_git_tags() { |
| 62 | + git_tags=$(git tag -l | sed 's|^| |') |
| 63 | + if [[ -n "$git_tags" ]]; then |
| 64 | + echo "Found git tags:" |
| 65 | + echo "$git_tags" |
| 66 | + echo "" |
| 67 | + fi |
| 68 | +} |
| 69 | + |
| 70 | +# |
| 71 | +# docker_login <image_name> |
| 72 | +# |
| 73 | +# docker_login performs a docker login for the server of the provided |
| 74 | +# image if the DOCKER_USERNAME and DOCKER_PASSWORD environment variables |
| 75 | +# are set. |
| 76 | +# |
| 77 | +function docker_login() { |
| 78 | + if [[ -n "$DOCKER_USERNAME" && -n "$DOCKER_PASSWORD" ]]; then |
| 79 | + server=$(docker_server_for_image $image_name) |
| 80 | + echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin "$server" |
| 81 | + fi |
| 82 | +} |
| 83 | + |
| 84 | +# |
| 85 | +# check_can_push |
| 86 | +# |
| 87 | +# check_can_push performs various checks to determine whether images |
| 88 | +# built from this commit should be pushed. It prints a message and |
| 89 | +# returns a failure code if any check doesn't pass. |
| 90 | +# |
| 91 | +function check_can_push() { |
| 92 | + if [[ "$TRAVIS" != "true" ]]; then |
| 93 | + echo "Detected execution in a non-TravisCI environment. Skipping image push." |
| 94 | + return 1 |
| 95 | + elif [[ "$TRAVIS_EVENT_TYPE" == "pull_request" ]]; then |
| 96 | + echo "Detected pull request commit. Skipping image push" |
| 97 | + return 1 |
| 98 | + elif [[ ! -f "$HOME/.docker/config.json" ]]; then |
| 99 | + echo "Docker login credentials required to push. Skipping image push." |
| 100 | + return 1 |
| 101 | + fi |
| 102 | +} |
| 103 | + |
| 104 | +# |
| 105 | +# get_image_tags <image_name> |
| 106 | +# |
| 107 | +# get_image_tags returns a list of tags that are eligible to be pushed. |
| 108 | +# If an image name is passed as an argument, the full <name>:<tag> will |
| 109 | +# be returned for each eligible tag. The criteria is: |
| 110 | +# 1. Is TRAVIS_BRANCH set? => <image_name>:$TRAVIS_BRANCH |
| 111 | +# 2. Is TRAVIS_TAG highest semver release? => <image_name>:latest |
| 112 | +# |
| 113 | +function get_image_tags() { |
| 114 | + image_name=$1 |
| 115 | + [[ -n "$image_name" ]] && image_name="${image_name}:" |
| 116 | + |
| 117 | + # Tag `:$TRAVIS_BRANCH` if it is set. |
| 118 | + # Note that if the build is for a tag, $TRAVIS_BRANCH is set |
| 119 | + # to the tag, so this works in both cases |
| 120 | + if [[ -n "$TRAVIS_BRANCH" ]]; then |
| 121 | + echo "${image_name}${TRAVIS_BRANCH}" |
| 122 | + fi |
| 123 | + |
| 124 | + # Tag `:latest` if $TRAVIS_TAG is the highest semver tag found in |
| 125 | + # the repository. |
| 126 | + if is_latest_tag "$TRAVIS_TAG"; then |
| 127 | + echo "${image_name}latest" |
| 128 | + fi |
| 129 | +} |
| 130 | + |
| 131 | +# |
| 132 | +# docker_server_for_image <image_name> |
| 133 | +# |
| 134 | +# docker_server_for_image returns the server component of the image |
| 135 | +# name. If the image name does not contain a server component, an |
| 136 | +# empty string is returned. |
| 137 | +# |
| 138 | +function docker_server_for_image() { |
| 139 | + image_name=$1; shift || fatal "${FUNCNAME} usage error" |
| 140 | + IFS='/' read -r -a segments <<< "$image_name" |
| 141 | + if [[ "${#segments[@]}" -gt "2" ]]; then |
| 142 | + echo "${segments[0]}" |
| 143 | + else |
| 144 | + echo "" |
| 145 | + fi |
| 146 | +} |
| 147 | + |
| 148 | +# |
| 149 | +# latest_git_version |
| 150 | +# |
| 151 | +# latest_git_version returns the highest semantic version |
| 152 | +# number found in the repository, with the form "vX.Y.Z". |
| 153 | +# Version numbers not matching the semver release format |
| 154 | +# are ignored. |
| 155 | +# |
| 156 | +function latest_git_version() { |
| 157 | + git tag -l | egrep "${semver_regex}" | sort -V | tail -1 |
| 158 | +} |
| 159 | + |
| 160 | +# |
| 161 | +# is_latest_tag <candidate> |
| 162 | +# |
| 163 | +# is_latest_tag returns whether the candidate tag matches |
| 164 | +# the latest tag from the git repository, based on semver. |
| 165 | +# To be the latest tag, the candidate must match the semver |
| 166 | +# release format. |
| 167 | +# |
| 168 | +function is_latest_tag() { |
| 169 | + candidate=$1; shift || fatal "${FUNCNAME} usage error" |
| 170 | + if ! [[ "$candidate" =~ $semver_regex ]]; then |
| 171 | + return 1 |
| 172 | + fi |
| 173 | + |
| 174 | + latest="$(latest_git_version)" |
| 175 | + [[ -z "$latest" || "$candidate" == "$latest" ]] |
| 176 | +} |
| 177 | + |
| 178 | +push_image_tags "$@" |
0 commit comments