-
Notifications
You must be signed in to change notification settings - Fork 19
/
.gitlab-ci-check-helm-version-bump.yml
101 lines (97 loc) · 5.03 KB
/
.gitlab-ci-check-helm-version-bump.yml
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# .gitlab-ci-check-helm-version-bump.yml
#
# This gitlab-ci template updates the Helm Chart versions
# for a given container
#
# Requires DOCKER_REGISTRY_ADDRESS variable to be set in the calling Pipeline.
# Add it to the project in hand through Gitlab's include functionality
# variables:
# DOCKER_REGISTRY_ADDRESS: registry.mender.io
# include:
# - project: 'Northern.tech/Mender/mendertesting'
# file: '.gitlab-ci-check-helm-version-bump.yml
#
# It requires that the upstream pipeline specifies:
#
# - CONTAINERS: alvaldi-gui # space-separated container names that later will
# be translated into values.yaml reference
# - DOCKER_PUBLISH_COMMIT_TAG: ${CI_COMMIT_REF_NAME}_${CI_COMMIT_SHA}
# - SYNC_ENVIRONMENT: [staging|prod] # the values-${SYNC_ENVIRONMENT}.yaml to be updated
# - CHART_DIR: mender # the Helm Chart directory in the helm repo
# - HELM_PATCH_VERSION: ${CI_PIPELINE_ID} # the Chart.yaml version - use Semver format
# - DRY_RUN: [true|false] # if true, it will not push back the changes to the repo
#
# The non-production version updates the Chart.yaml with a prerelease version:
# version: x.y.${CI_PIPELINE_ID}-staging
# On the downstream, FluxCD is configured to trigger a new Helm deployment every releases and pre-releases on the non-prod envs.
# At the same way, the production envs are configured to trigger only stable releases.
stages:
- version-bump
helm-version-bump:
rules:
- if: $CI_PIPELINE_SOURCE == "pipeline"
stage: version-bump
tags:
- hetzner-amd-beefy
image: registry.gitlab.com/northern.tech/mender/mender-test-containers:aws-k8s-v1-master
before_script:
- |
echo "INFO - setting up git"
git config --global user.email "[email protected]"
git config --global user.name "Mender Test Bot"
export GITHUB_TOKEN=${GITHUB_BOT_TOKEN_REPO_FULL}
- export TS_SUFFIX=$(date +%s)
script:
# Adding the GH repo to push back new tags
- |
git remote add github-${TS_SUFFIX} https://${GITHUB_USER}:${GITHUB_TOKEN}@${GITHUB_HELM_REPO}
git fetch github-${TS_SUFFIX} ${SYNC_ENVIRONMENT:-staging}:overlay-version-bump-${TS_SUFFIX}
git checkout overlay-version-bump-${TS_SUFFIX}
- |
echo "INFO - checking values files"
test -e ${CHART_DIR}/values-${SYNC_ENVIRONMENT}.yaml || ( echo "ERROR - ${CHART_DIR}/values-${SYNC_ENVIRONMENT}.yaml doesn't exists" ; exit 1 )
test -e ${CHART_DIR}/Chart.yaml || ( echo "ERROR - ${CHART_DIR}/Chart.yaml doesn't exists" ; exit 1 )
- |
for CONTAINER in ${CONTAINERS}; do
echo "INFO - bumping version ${DOCKER_PUBLISH_COMMIT_TAG} to ${CONTAINER} image tag"
test -n ${DOCKER_PUBLISH_COMMIT_TAG} || ( echo "ERROR - version tag not found." ; exit 1 )
THIS_KEY=".${CONTAINER}.image.tag" THIS_VALUE="${DOCKER_PUBLISH_COMMIT_TAG}" yq -i 'eval(strenv(THIS_KEY)) = strenv(THIS_VALUE)' ${CHART_DIR}/values-${SYNC_ENVIRONMENT}.yaml
echo "DEBUG - container registry is: $CONTAINER_REGISTRY"
if [[ -n "${CONTAINER_REGISTRY}" ]]; then
echo "INFO - bumping registry ${CONTAINER_REGISTRY} to ${CONTAINER} container"
test -n ${CONTAINER_REGISTRY} || ( echo "ERROR - CONTAINER_REGISTRY variable is empty." ; exit 1 )
THIS_KEY=".${CONTAINER}.image.registry" THIS_VALUE="${CONTAINER_REGISTRY}" yq -i 'eval(strenv(THIS_KEY)) = strenv(THIS_VALUE)' ${CHART_DIR}/values-${SYNC_ENVIRONMENT}.yaml
fi
echo "INFO - bumping registry ${DOCKER_REGISTRY_ADDRESS:-registry.mender.io} to ${CONTAINER} container"
THIS_KEY=".${CONTAINER}.image.registry" THIS_VALUE="${DOCKER_REGISTRY_ADDRESS:-registry.mender.io}" yq -i 'eval(strenv(THIS_KEY)) = strenv(THIS_VALUE)' ${CHART_DIR}/values-${SYNC_ENVIRONMENT}.yaml
done
git add ${CHART_DIR}/values-${SYNC_ENVIRONMENT}.yaml
- |
echo "INFO - bumping helm chart version"
FULL_VERSION=$(yq ".version" ${CHART_DIR}/Chart.yaml)
MAJOR_VERSION=$(echo $FULL_VERSION | cut -f1 -d.)
MINOR_VERSION=$(echo $FULL_VERSION | cut -f2 -d.)
PATCH_VERSION=$(echo $FULL_VERSION | cut -f3 -d. | cut -f1 -d\-)
THIS_VALUE="${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}-${HELM_PATCH_VERSION}" yq -i '.version = strenv(THIS_VALUE)' ${CHART_DIR}/Chart.yaml
git add ${CHART_DIR}/Chart.yaml
# Commit
- |
if [[ "${DRY_RUN}" == "false" ]]; then
git commit --signoff --message "[CI/CD] bump helm chart"
for retry in $(seq 5); do
if git push github-${TS_SUFFIX} overlay-version-bump-${TS_SUFFIX}:${SYNC_ENVIRONMENT:-staging}; then
exit 0
fi
git fetch github-${TF_SUFFIX} ${SYNC_ENVIRONMENT:-staging}
git rebase github-${TF_SUFFIX}/${SYNC_ENVIRONMENT:-staging}
sleep ${TIMEOUT_SECONDS:-5}
done
echo "ERROR - can't push to github"
exit 1
else
echo "INFO - dry-run mode enabled: skipping git push"
echo "DEBUG - printing the resulted values file"
git diff --staged
cat ${CHART_DIR}/values-${SYNC_ENVIRONMENT}.yaml
cat ${CHART_DIR}/Chart.yaml
fi