-
Notifications
You must be signed in to change notification settings - Fork 1
/
HarnessKustomizePluginScript-GIT-HTTPS-AUTH
67 lines (44 loc) · 2.57 KB
/
HarnessKustomizePluginScript-GIT-HTTPS-AUTH
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
#!/bin/sh
#Variables section
GIT_USER="joebloggs"
GIT_EMAIL="[email protected]"
#note HTTPS url requires URL encoding for special characters
GIT_PASSWORD_URLENCODED=${secrets.getValue("git_password_url_encoded")}
GIT_BRANCH="main"
#reference to harness image artifact variable
IMAGE=${artifact.metadata.image}
#repo URL suffix
REPOSITORY_NAME=HarnessKustomizePlugin
#generic repo path
REPO_PATH=https://github.com/gregkroon/HarnessKustomizePlugin
#build full auth string with URL encoded password for remote repo add and git push
REPO_AUTH_STRING=https://$GIT_USER:[email protected]/$GIT_USER/$REPOSITORY_NAME.git
if [[ ! -d "/root/K_PLUGINS/${app.name}/${service.name}/${env.name}/kustomize/plugin/version1/harnesskustomizeplugin" ]];then
mkdir -p /root/K_PLUGINS/${app.name}/${service.name}/${env.name}/kustomize/plugin/version1/harnesskustomizeplugin
fi
#cleanup old script
rm -f /root/K_PLUGINS/${app.name}/${service.name}/${env.name}/kustomize/plugin/version1/harnesskustomizeplugin/HarnessKustomizePlugin
#generate plugin script
cat <<EOF > /root/K_PLUGINS/${app.name}/${service.name}/${env.name}/kustomize/plugin/version1/harnesskustomizeplugin/HarnessKustomizePlugin
#!/bin/sh
#Setup GIT config
git config --global user.email $GIT_EMAIL
git config --global user.name $GIT_USER
# capture standad input to file for yq processing
cat -> input.yaml
#Process input file and transform for plugin stdout , this uses a yq select so as to change only the deployment.yaml not the service.yaml
yq eval '(select(.spec.template.spec.containers[].image) |.spec.template.spec.containers[].image = "$IMAGE")' input.yaml
# create update for commit to GIT
yq eval '(select(.kind == "Deployment") | del(.metadata.annotations."kustomize.config.k8s.io/id") | .spec.template.spec.containers[].image = "$IMAGE"' input.yaml > deployment.yaml.new
rm deployment.yaml
mv deployment.yaml.new deployment.yaml
################################## GIT OPTIONAL YOU CAN REMOVE THIS BLOCK IF NOT USING GIT ##################################
git pull $REPO_PATH $GIT_BRANCH > /dev/null 2>&1
git add -f deployment.yaml > /dev/null 2>&1
git commit -m "Harness Automated commit" > /dev/null 2>&1
git remote set-url origin $REPO_AUTH_STRING > /dev/null 2>&1
git push origin HEAD:$GIT_BRANCH > /dev/null 2>&1
################################## GIT OPTIONAL YOU CAN REMOVE THIS BLOCK IF NOT USING GIT ##################################
EOF
#Set plugin scipt permissions
chmod +x /root/K_PLUGINS/${app.name}/${service.name}/${env.name}/kustomize/plugin/version1/harnesskustomizeplugin/HarnessKustomizePlugin