Skip to content

Commit 707fd4c

Browse files
authored
Merge pull request #3100 from flatcar/krnowak/fix-tag-push
Fix nightly tag pushing
2 parents 4234b8b + 1081bac commit 707fd4c

File tree

4 files changed

+818
-95
lines changed

4 files changed

+818
-95
lines changed

ci-automation/ci_automation_common.sh

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ function check_version_string() {
2222
# --
2323

2424
function update_and_push_version() {
25-
local version="$1"
26-
local target_branch="${2:-}"
25+
local version=${1}
26+
local target_branch=${2:-}
2727

2828
# set up author and email so git does not complain when tagging
2929
if ! git config --get user.name >/dev/null 2>&1 ; then
30-
git -C . config user.name "${CI_GIT_AUTHOR}"
30+
git config user.name "${CI_GIT_AUTHOR}"
3131
fi
3232
if ! git config --get user.email >/dev/null 2>&1 ; then
33-
git -C . config user.email "${CI_GIT_EMAIL}"
33+
git config user.email "${CI_GIT_EMAIL}"
3434
fi
3535

3636
# Add and commit local changes
@@ -39,39 +39,50 @@ function update_and_push_version() {
3939
git commit --allow-empty -m "New version: ${version}"
4040

4141
git fetch --all --tags --force
42-
local ret=0
43-
git diff --exit-code "${version}" || ret=$?
42+
local -i ret=0
43+
git diff --quiet --exit-code "${version}" 2>/dev/null || ret=${?}
4444
# This will return != 0 if
4545
# - the remote tag does not exist (rc: 127)
4646
# - the tag does not exist locally (rc: 128)
4747
# - the remote tag has changes compared to the local tree (rc: 1)
48-
if [ "$ret" = "0" ]; then
49-
echo "Reusing existing tag" >&2
50-
git checkout -f "${version}"
51-
return
52-
elif [ "$ret" = "1" ]; then
53-
echo "Remote tag exists already and is not equal" >&2
54-
return 1
55-
elif [ "$ret" != "127" ] && [ "$ret" != "128" ]; then
56-
echo "Error: Unexpected git diff return code ($ret)" >&2
57-
return 1
48+
if [[ ret -eq 0 ]]; then
49+
# this means that we created an empty commit above, reusing the tag gets rid of it
50+
echo "Reusing existing tag" >&2
51+
git checkout --force "${version}"
52+
return
53+
elif [[ ret -eq 1 ]]; then
54+
echo "Remote tag exists already and is not equal" >&2
55+
return 1
56+
elif [[ ret -ne 127 && ret -ne 128 ]]; then
57+
echo "Error: Unexpected git diff return code (${ret})" >&2
58+
return 1
5859
fi
5960

60-
local -a TAG_ARGS
61-
if [ "${SIGN-0}" = 1 ]; then
62-
TAG_ARGS=("-s" "-m" "${version}")
61+
local -a TAG_ARGS=()
62+
if [[ ${SIGN-0} = 1 ]]; then
63+
TAG_ARGS=("--sign" "--message=${version}")
6364
fi
6465

65-
git tag -f "${TAG_ARGS[@]}" "${version}"
66+
git tag --force "${TAG_ARGS[@]}" "${version}"
6667

67-
if [[ -n "${target_branch}" ]]; then
68-
git push origin "HEAD:${target_branch}"
68+
if [[ -n ${target_branch} ]]; then
69+
git push origin "HEAD:${target_branch}"
6970
fi
7071

7172
git push origin "${version}"
7273
}
7374
# --
7475

76+
function check_bincache_images_existence() {
77+
case ${CIA_DEBUGIMAGESEXIST:-} in
78+
'yes') return 0;;
79+
'no' ) return 1;;
80+
'') curl --head --fail --fail-early --silent --show-error --location "${@}" || return 1;;
81+
*) echo "Invalid CIA_DEBUGIMAGESEXIST value (${CIA_DEBUGIMAGESEXIST@Q})" >&2; exit 1;;
82+
esac
83+
}
84+
# --
85+
7586
function copy_from_buildcache() {
7687
local what="$1"
7788
local where_to="$2"

ci-automation/packages-tag.sh

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,67 +56,108 @@ function packages_tag() {
5656
# --
5757

5858
function _packages_tag_impl() {
59-
local version="$1"
59+
local version=${1}
6060

6161
source ci-automation/ci_automation_common.sh
6262
source ci-automation/gpg_setup.sh
6363

6464
check_version_string "${version}"
6565

6666
source sdk_container/.repo/manifests/version.txt
67-
local sdk_version="${FLATCAR_SDK_VERSION}"
67+
local sdk_version=${FLATCAR_SDK_VERSION}
6868

69+
if [[ -n ${CIA_DEBUGTESTRUN:-} ]]; then
70+
set -x
71+
fi
6972
# Create new tag in scripts repo w/ updated versionfile
7073
# Also push the changes to the branch ONLY IF we're doing a nightly
7174
# build of the 'flatcar-MAJOR' branch AND we're definitely ON the respective branch
7275
local target_branch=''
7376
# These variables are here to make it easier to test nightly
7477
# builds without messing with actual release branches.
75-
local flatcar_branch_prefix='flatcar'
76-
local nightly='nightly'
77-
# Patterns used below.
78-
local nightly_pattern_1='^(stable|alpha|beta|lts)-[0-9.]+-'"${nightly}"'-[-0-9]+$'
79-
local nightly_pattern_2='^(stable|alpha|beta|lts)-[0-9.]+(|-'"${nightly}"'-[-0-9]+)$'
80-
local flatcar_pattern='^'"${flatcar_branch_prefix}"'-[0-9]+$'
81-
if [[ "${version}" =~ ${nightly_pattern_1} ]] \
82-
&& [[ "$(git rev-parse --abbrev-ref HEAD)" =~ ${flatcar_pattern} ]] ; then
83-
target_branch="$(git rev-parse --abbrev-ref HEAD)"
84-
local existing_tag=""
78+
local flatcar_branch_prefix=${CIA_DEBUGFLATCARBRANCHPREFIX:-flatcar}
79+
local nightly=${CIA_DEBUGNIGHTLY:-nightly}
80+
# Matches the usual nightly tag name (stable-1234.2.3-nightly-yyyymmdd-hhmm)
81+
local nightly_pattern='^(stable|alpha|beta|lts)-([0-9]+)(\.[0-9]+){2}-'"${nightly}"'-[0-9]{8}-[0-9]{4}$'
82+
local -i major_version=0
83+
local branch_name=''
84+
local branch_hash=''
85+
if [[ ${version} =~ ${nightly_pattern} ]]; then
86+
major_version=${BASH_REMATCH[2]}
87+
branch_name=${flatcar_branch_prefix}-${major_version}
88+
branch_hash=$(git rev-parse "origin/${branch_name}")
89+
fi
90+
local -a existing_tags=()
91+
if [[ -n ${branch_hash} ]]; then
92+
if [[ $(git rev-parse HEAD) != "${branch_hash}" ]]; then
93+
echo "We are doing a nightly build but we are not on top of the ${branch_name} branch. This is wrong and would result in the nightly tag not being a part of the branch." >&2
94+
exit 1
95+
fi
96+
target_branch=${branch_name}
8597
# Check for the existing tag only when we allow shortcutting
8698
# the builds. That way we can skip the checks for build
8799
# shortcutting.
88100
if bool_is_true "${AVOID_NIGHTLY_BUILD_SHORTCUTS}"; then
89101
echo "Continuing the build because AVOID_NIGHTLY_BUILD_SHORTCUTS is bool true (${AVOID_NIGHTLY_BUILD_SHORTCUTS})" >&2
90102
else
91-
existing_tag=$(git tag --points-at HEAD) # exit code is always 0, output may be empty
103+
git fetch --all --tags --force
104+
# exit code of git tag is always 0; output may be empty,
105+
# but may also have multiple tags
106+
mapfile -t existing_tags < <(git tag --points-at HEAD)
92107
fi
93-
# If the found tag is a release or nightly tag, we stop this build if there are no changes
94-
if [[ "${existing_tag}" =~ ${nightly_pattern_2} ]]; then
95-
local ret=0
96-
git diff --exit-code "${existing_tag}" || ret=$?
97-
if [[ ret -eq 0 ]]; then
98-
if curl --head --fail --silent --show-error --location "https://${BUILDCACHE_SERVER}/images/amd64/${FLATCAR_VERSION}/flatcar_production_image.bin.bz2" \
99-
&& curl --head --fail --silent --show-error --location "https://${BUILDCACHE_SERVER}/images/arm64/${FLATCAR_VERSION}/flatcar_production_image.bin.bz2"; then
108+
fi
109+
local nightly_or_release_tag=''
110+
if [[ major_version -gt 0 && ${#existing_tags[@]} -gt 0 ]]; then
111+
local nightly_or_release_pattern='^(stable|alpha|beta|lts)-'"${major_version}"'(\.[0-9]+){2}(-'"${nightly}"'-[0-9]{8}-[0-9]{4})?$'
112+
local tag
113+
for tag in "${existing_tags[@]}"; do
114+
if [[ ${tag} =~ ${nightly_or_release_pattern} ]]; then
115+
nightly_or_release_tag=${tag}
116+
break
117+
fi
118+
done
119+
fi
120+
# If the found tag is a release or nightly tag, we stop this build
121+
# if there are no changes and the relevant images can be found in
122+
# bincache.
123+
if [[ -n ${nightly_or_release_tag} ]]; then
124+
local -i ret=0
125+
git diff --exit-code --quiet "${nightly_or_release_tag}" || ret=$?
126+
if [[ ret -eq 0 ]]; then
127+
# no changes in the code, but check if images exist (they
128+
# could be missing if build failed)
129+
if check_bincache_images_existence \
130+
"https://${BUILDCACHE_SERVER}/images/amd64/${FLATCAR_VERSION}/flatcar_production_image.bin.bz2" \
131+
"https://${BUILDCACHE_SERVER}/images/arm64/${FLATCAR_VERSION}/flatcar_production_image.bin.bz2"; then
100132
touch ./skip-build
101-
echo "Creating ./skip-build flag file, indicating that the build must not to continue because no new tag got created as there are no changes since tag ${existing_tag} and the Flatcar images exist" >&2
133+
echo "Creating ./skip-build flag file, indicating that the build must not to continue because no new tag got created as there are no changes since tag ${nightly_or_release_tag} and the Flatcar images exist" >&2
102134
return 0
103135
fi
104136
echo "No changes but continuing build because Flatcar images do not exist"
105-
elif [[ ret -eq 1 ]]; then
106-
echo "Found changes since last tag ${existing_tag}" >&2
107-
else
137+
elif [[ ret -eq 1 ]]; then
138+
echo "HEAD is tagged with a nightly tag and yet there a differences? This is fishy and needs to be investigated. Maybe you forgot to commit your changes?" >&2
139+
exit 1
140+
else
108141
echo "Error: Unexpected git diff return code (${ret})" >&2
109142
return 1
110-
fi
111143
fi
112144
fi
145+
if [[ -n ${CIA_DEBUGTESTRUN:-} ]]; then
146+
set +x
147+
fi
113148

114149
# Create version file
115150
(
116-
source sdk_lib/sdk_container_common.sh
117-
create_versionfile "$sdk_version" "$version"
151+
source sdk_lib/sdk_container_common.sh
152+
create_versionfile "${sdk_version}" "${version}"
118153
)
154+
if [[ -n ${CIA_DEBUGTESTRUN:-} ]]; then
155+
set -x
156+
fi
119157
update_and_push_version "${version}" "${target_branch}"
158+
if [[ -n ${CIA_DEBUGTESTRUN:-} ]]; then
159+
exit 0
160+
fi
120161
apply_local_patches
121162
}
122163
# --

0 commit comments

Comments
 (0)