Skip to content

Commit eb6e366

Browse files
authored
Create main.sh
1 parent c032869 commit eb6e366

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

main.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/bin/bash
2+
set -e
3+
4+
if [ -z $CLUSTER_GIT_CLONE_URL ]; then
5+
echo "Missing CLUSTER_GIT_CLONE_URL environment var"
6+
exit 1
7+
fi
8+
9+
if [ -z $YAML_FILE_PATH ]; then
10+
echo "Missing YAML_FILE_PATH environment var"
11+
exit 1
12+
fi
13+
14+
if [ -z $NAMESPACE ]; then
15+
echo "Missing NAMESPACE environment var"
16+
exit 1
17+
fi
18+
19+
if [ -z $DOCKER_REPO ]; then
20+
echo "Missing DOCKER_REPO environment var"
21+
exit 1
22+
fi
23+
24+
if [ -z $DOCKER_IMAGE ]; then
25+
echo "Missing DOCKER_IMAGE environment var"
26+
exit 1
27+
fi
28+
29+
if [ -z $DOCKER_TAG ]; then
30+
echo "Missing DOCKER_TAG environment var"
31+
exit 1
32+
fi
33+
34+
# add extra validations here if you need, regarding the value of those variables for example (you may want to check that only expected directories are modified)
35+
# if [ ... ]; then
36+
# echo "Given value [...] is not authorised for deployment. Perhaps this is a typo?"
37+
# exit 1
38+
# fi
39+
40+
git clone --depth=1 $CLUSTER_GIT_CLONE_URL
41+
# if needed :
42+
# cd some-folder/some-subfolder/
43+
44+
IMAGE_NAME=$DOCKER_REPO/$DOCKER_IMAGE:$DOCKER_TAG
45+
46+
yq eval -i "select(.kind == \"Deployment\").spec.template.spec.containers[0].image = \"$IMAGE_NAME\"" $YAML_FILE_PATH
47+
48+
# bonus : if your cluster Git repo does follow a clean nomenclature, then the path of the right .yaml file to update can be guessed with extra environment variables, like this for example :
49+
# yq eval -i "select(.kind == \"Deployment\").spec.template.spec.containers[0].image = \"$IMAGE_NAME\"" "$CLUSTER_FOLDER_NAME"/"$NAMESPACE"/"$PROJECT_TYPE"s/"$DEPLOYMENT_NAME".yaml
50+
51+
# bonus : modify at the same time a container environment variable that tracks the current version deployed, for Datadog Agent for example :
52+
# yq eval -i "(select(.kind == \"Deployment\").spec.template.spec.containers[0].env[] | select(.name == \"DD_VERSION\")).value = \"$DOCKER_TAG\"" "$CLUSTER_FOLDER_NAME"/"$NAMESPACE"/"$PROJECT_TYPE"s/"$DEPLOYMENT_NAME".yaml
53+
54+
git add -A
55+
if [ -n "$(git status --porcelain)" ]; then
56+
echo "File "$YAML_FILE_PATH" has been modified"
57+
else
58+
echo "The image $IMAGE_NAME is already used by Deployment $NAMESPACE:deployment/$DOCKER_IMAGE"
59+
exit 1
60+
fi
61+
62+
git commit -m "Rolling image $IMAGE_NAME to Deployment $NAMESPACE:deployment/$DOCKER_IMAGE"
63+
git push

0 commit comments

Comments
 (0)