-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·147 lines (118 loc) · 4.19 KB
/
entrypoint.sh
File metadata and controls
executable file
·147 lines (118 loc) · 4.19 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/sh -l
#set -e # stops execution
set -u # fail on undefined variables
CREATE_PR=${CREATE_PR:-false}
echo "Configuring git globals..."
git config --global user.name "${GIT_USER_NAME}"
git config --global user.email "${GIT_USER_EMAIL}"
process() {
(
# Temp dir preparation
if [ -d /tmp/git ]; then
echo "Temp dir found. Removing"
rm -r /tmp/git
echo "Creating new temp dir"
mkdir /tmp/git
else
echo "Creating temp dir"
mkdir /tmp/git
fi
# Cloning the repo
echo "Cloning git repository (branch=$GIT_INFRASTRUCTURE_REPOSITORY_BRANCH, owner=$GIT_INFRASTRUCTURE_REPOSITORY_OWNER, name=$GIT_INFRASTRUCTURE_REPOSITORY_NAME)"
if ! git clone --single-branch --branch "$GIT_INFRASTRUCTURE_REPOSITORY_BRANCH" "https://x-access-token:$GIT_USER_API_TOKEN@github.com/$GIT_INFRASTRUCTURE_REPOSITORY_OWNER/$GIT_INFRASTRUCTURE_REPOSITORY_NAME.git" /tmp/git; then
echo "Git clone failed"
exit 1
fi
cd /tmp/git
# Building the tag
echo "Building tag"
echo INPUT_TAG_NAME_SKIP=${INPUT_TAG_NAME_SKIP}
TAG=$(echo ${GITHUB_REF} | sed -e "s/refs\/tags\/${INPUT_TAG_NAME_SKIP}//")
echo "Tag=${TAG}"
if echo ${TAG} | grep refs; then
echo "Tag building failed"
exit 1
fi
# Switching branch if PR creation is requested
if [ "$CREATE_PR" = "true" ]; then
HEAD_GIT_BRANCH=$(printf "%s-v%s" $GIT_REPOSITORY_NAME $TAG)
echo "Switching git branch to $HEAD_GIT_BRANCH"
if ! git checkout -b $HEAD_GIT_BRANCH; then
echo "Git checkout failed"
exit 1
fi
else
HEAD_GIT_BRANCH=$GIT_INFRASTRUCTURE_REPOSITORY_BRANCH
fi
echo "Head git branch $HEAD_GIT_BRANCH"
# Processing docker image names
for DOCKER_IMAGE_NAME_ITEM in $(echo $DOCKER_IMAGE_NAME | tr "," "\n")
do
DOCKER_IMAGE=$(printf "%s/%s" $DOCKER_REPOSITORY_NAME $DOCKER_IMAGE_NAME_ITEM)
echo DOCKER_IMAGE=$DOCKER_IMAGE
DOCKER_IMAGE_SLASH=$(echo ${DOCKER_IMAGE} | sed 's#/#\\/#g')
echo "Full docker image=${DOCKER_IMAGE_SLASH}"
for YAML_FILE in $(grep -rn $DOCKER_IMAGE: ./ | awk -F: '{print $1}')
do
echo "Processing $YAML_FILE"
sed -i "s/${DOCKER_IMAGE_SLASH}:.*/${DOCKER_IMAGE_SLASH}:${TAG}/" ${YAML_FILE}
done
done
# Adding changed files to git
echo "Add changed files to git"
git add -A
echo "Changes:"
git diff --cached
echo "Committing to git"
git commit -m "$GIT_REPOSITORY_NAME ${TAG}"
echo "Pushing to git"
if ! git push --set-upstream origin $HEAD_GIT_BRANCH; then
echo "Git push failed"
exit 1
fi
echo $? > /tmp/exit_status
echo "Git changes log:"
git log -2
# Creating GitHub PR
if [ "$CREATE_PR" = "true" ]; then
echo "Creating PR"
PR_TITLE=$(printf "%s %s" $GIT_REPOSITORY_NAME $TAG)
PR_BODY=$(printf "%s %s update" $GIT_REPOSITORY_NAME $TAG)
PR_URL="https://api.github.com/repos/${GIT_INFRASTRUCTURE_REPOSITORY_OWNER}/${GIT_INFRASTRUCTURE_REPOSITORY_NAME}/pulls"
PR_DATA="{\"title\":\"${PR_TITLE}\",\"body\":\"${PR_BODY}\",\"head\":\"${HEAD_GIT_BRANCH}\",\"base\":\"${GIT_INFRASTRUCTURE_REPOSITORY_BRANCH}\"}"
echo "PR title: $PR_TITLE"
echo "PR body: $PR_BODY"
echo "PR URL: $PR_URL"
echo "PR data: $PR_DATA"
if ! PR_CREATION_RESPONSE=$(curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GIT_USER_API_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
--write-out '\n%{http_code}' \
--silent \
-d "$PR_DATA" \
$PR_URL); then
echo "PR creation CURL execution failed"
exit 1
fi
PR_CREATION_RESPONSE_HTTP_STATUS=$(echo "$PR_CREATION_RESPONSE" | tail -n1)
PR_CREATION_RESPONSE_JSON=$(echo "$PR_CREATION_RESPONSE" | sed '$d') # Remove last line (HTTP status)
echo "PR creation http status code: $PR_CREATION_RESPONSE_HTTP_STATUS"
if [ "$PR_CREATION_RESPONSE_HTTP_STATUS" -ne 201 ]; then
echo "PR creation failed"
echo "PR creation response JSON: $PR_CREATION_RESPONSE_JSON"
exit 1
fi
fi
) > /tmp/process.log 2>&1
}
if ! process; then
echo "Failed to process. Log:"
cat /tmp/process.log
exit 1
else
echo "Processing done. Log:"
cat /tmp/process.log
fi
exec "$@"