-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgitops-functions.sh
More file actions
executable file
·71 lines (60 loc) · 2.33 KB
/
Copy pathgitops-functions.sh
File metadata and controls
executable file
·71 lines (60 loc) · 2.33 KB
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
#!/usr/bin/env bash
# GitOps helper functions for updating Kubernetes manifests.
# Sourced by update-gitops.sh — not executed directly.
#
# Expected env vars (set by caller):
# INPUT_DOCKER_REGISTRY, INPUT_DOCKER_IMAGE, INPUT_TAG, INPUT_PUSH,
# INPUT_GITOPS_USER, INPUT_GITOPS_TOKEN,
# INPUT_GITOPS_ORGANIZATION, INPUT_GITOPS_REPOSITORY,
# GITHUB_REPOSITORY, GITHUB_SHA, IMAGE
push_to_gitops_repo() {
git pull --rebase "https://${INPUT_GITOPS_USER}:${INPUT_GITOPS_TOKEN}@github.com/${INPUT_GITOPS_ORGANIZATION}/${INPUT_GITOPS_REPOSITORY}.git"
git push "https://${INPUT_GITOPS_USER}:${INPUT_GITOPS_TOKEN}@github.com/${INPUT_GITOPS_ORGANIZATION}/${INPUT_GITOPS_REPOSITORY}.git"
}
commit_changes() {
if [[ "${INPUT_PUSH}" == "true" ]]; then
git add .
if git diff-index --quiet HEAD; then
echo "There were no changes..."
return
fi
git commit -m "Release ${INPUT_DOCKER_REGISTRY}/${INPUT_DOCKER_IMAGE}:${INPUT_TAG}"
retry_with_backoff 5 2 push_to_gitops_repo
fi
}
update_file() {
local file="$1"
local field="$2"
local image="$3"
echo "Check if path ${file} ${field} exists and get old current version"
yq -e ."${field}" "${file}"
echo "Run update ${file} ${field} ${image}"
if [[ "${field}" == *.tag ]]; then
yq -i ".${field}=\"${INPUT_TAG}\"" "${file}"
else
local field_type
field_type=$(yq "(.${field} | type)" "${file}" 2>/dev/null || echo "!!null")
if [[ "${field_type}" == "!!map" ]] && yq -e ".${field} | (has(\"tag\") or has(\"repository\"))" "${file}" > /dev/null 2>&1; then
yq -i ".${field}.tag=\"${INPUT_TAG}\"" "${file}"
else
yq -i ."${field}"=\""${image}"\" "${file}"
fi
fi
echo "Writing deployment annotations to ${file}"
yq -i '.metadata.annotations["deploy.staffbase.com/repositoryFullName"] = "'"${GITHUB_REPOSITORY}"'"' "${file}"
yq -i '.metadata.annotations["deploy.staffbase.com/commitSha"] = "'"${GITHUB_SHA}"'"' "${file}"
yq -i '.metadata.annotations["deploy.staffbase.com/version"] = "'"${INPUT_TAG}"'"' "${file}"
}
process_file_updates() {
local file_list="$1"
local should_commit="$2"
while IFS= read -r line; do
[[ -z "$line" ]] && continue
local file field
read -r file field <<< "$line"
update_file "$file" "$field" "$IMAGE"
done <<< "$file_list"
if [[ "$should_commit" == "true" ]]; then
commit_changes
fi
}