-
Notifications
You must be signed in to change notification settings - Fork 29
/
make-image-tag.sh
executable file
·74 lines (65 loc) · 2.67 KB
/
make-image-tag.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
# Copyright 2017-2020 Authors of Cilium
# SPDX-License-Identifier: Apache-2.0
set -o errexit
set -o pipefail
set -o nounset
# This script provides two image tagging mechanisms.
#
# For general images that use most of the tree as input, it's most sensible to
# use git commit hash as a tag, or git version tag. Any tags that do not match
# a simple 2-dot version pattern are ignored, and commit hash is used.
#
# For images that use contents of a subdirectory as input, it's convenient to use
# a git tree hash. Running `git show` with tree hash based tag will display the
# contents of the subdirectory that was used as build input, mitigating any doubts
# in what was used to build this image.
#
# For both types of tags To differentiate any non-authoritative builds, i.e.
# builds from development branches, `-dev` suffix is added. Any builds that may
# include uncommitted changes will have `-wip` tag.
if [ "$#" -gt 1 ] ; then
echo "$0 supports exactly 1 or no arguments"
exit 1
fi
root_dir="$(git rev-parse --show-toplevel)"
cd "${root_dir}"
if [ "$#" -eq 1 ] ; then
# if one argument was given, assume it's a directory and retrieve its last commit to generate a tag
image_dir="${1}"
if ! [ -d "${image_dir}" ] ; then
echo "${image_dir} is not a directory (path is relative to git root)"
exit 1
fi
timestamp="$(git log -1 --pretty=format:"%ct" "${image_dir}")"
short_commit="$(git log -1 --pretty=format:"%h" "${image_dir}")"
image_tag="${timestamp}-${short_commit}"
else
# if no arguments are given, attempt detecting if version tag is present,
# otherwise use the a short commit hash
image_dir="${root_dir}"
git_tag="$(git name-rev --name-only --tags HEAD)"
if printf "%s" "${git_tag}" | grep -q -E '^[v]?[0-9]+\.[0-9]+\.[0-9]+.*$' ; then
# get tag in conventional format, since name-rev use the format with ^0 suffix,
# however name-rev is required to determine presence of a tag
git_tag="$(git tag --sort tag --points-at "${git_tag}")"
# ensure version tag always has the v prefix and drop duplicates
image_tag="$(printf "%s" "${git_tag}" | sed 's/^[v]*/v/' | uniq)"
else
# if no version tag is given, use commit hash
image_tag="$(git rev-parse --short HEAD)"
# only append -dev suffix when no version tag is used, since tags
# can be set on release branches
if [ -z "${WITHOUT_SUFFIX+x}" ] ; then
if ! git merge-base --is-ancestor "$(git rev-parse HEAD)" origin/master ; then
image_tag="${image_tag}-dev"
fi
fi
fi
fi
if [ -z "${WITHOUT_SUFFIX+x}" ] ; then
if [ "$(git status --porcelain "${image_dir}" | wc -l)" -gt 0 ] ; then
image_tag="${image_tag}-wip"
fi
fi
printf "%s" "${image_tag}"