-
Notifications
You must be signed in to change notification settings - Fork 30
229 lines (203 loc) · 10.5 KB
/
auto-upgrade.yaml
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
name: Auto Upgrade Chart
env:
ISSUE_LABEL_PREFIX: "robot-upgrade"
DEFAULT_REVIEWER: "weizhoublue"
on:
schedule:
# each day
- cron: "0 20 * * *"
workflow_dispatch:
inputs:
project:
description: 'project names seperated by comma, or else empty for all projects'
required: false
jobs:
check_project:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: check project
run: |
#get all existed project
TMP=`ls charts`
PROJECT_LIST=""
for ITEM in $TMP ; do
if [ -d "charts/${ITEM}/${ITEM}" ] ; then
PROJECT_LIST+=" ${ITEM} "
fi
done
echo "all existed projects: ${PROJECT_LIST}"
[ -n "$PROJECT_LIST" ] || { echo "error, found no projects" ; exit 1 ; }
if ${{ github.event_name == 'workflow_dispatch' }} ; then
echo "workflow_dispatch event"
var=${{ github.event.inputs.project }}
echo "input $var"
if [ -n "${var}" ] ; then
for ITEM in ${var} ; do
grep " $ITEM " <<< "$PROJECT_LIST" &>/dev/null && continue
echo "error, project $ITEM is forbidden to upgrade"
exit 1
done
echo "ENV_UPGRADE_PROJECT_LIST=${var}" >> $GITHUB_ENV
else
echo "empty input, upgrade all projects"
echo "ENV_UPGRADE_PROJECT_LIST=${PROJECT_LIST}" >> $GITHUB_ENV
fi
else
echo "schedule event, upgrade all projects"
echo "ENV_UPGRADE_PROJECT_LIST=${PROJECT_LIST}" >> $GITHUB_ENV
fi
- name: Set up Helm
uses: azure/[email protected]
- name: upgrade
env:
GITHUB_TOKEN: ${{ secrets.WELAN_PAT}}
run: |
set -x
PROJECTS="${{ env.ENV_UPGRADE_PROJECT_LIST }}"
echo "begin to upgrade for projects ${PROJECTS}"
FAILED_MESSAGE=""
SUCCEED_PROJECT=""
git branch
git config user.email "[email protected]"
git config user.name "robot"
for PROJECT in $PROJECTS ; do
echo "====================== checking project $PROJECT ================="
CONFIG_FILE="charts/${PROJECT}/config"
REPO_URL=` source ${CONFIG_FILE} && echo ${REPO_URL} `
REPO_NAME=` source ${CONFIG_FILE} && echo ${REPO_NAME} `
CHART_NAME=` source ${CONFIG_FILE} && echo ${CHART_NAME} `
VERSION=` source ${CONFIG_FILE} && echo ${VERSION} `
METHOD=` source ${CONFIG_FILE} && echo ${UPGRADE_METHOD} `
UPGRADE_REVIWER=` source ${CONFIG_FILE} && echo ${UPGRADE_REVIWER} `
[ -n "$METHOD" ] || METHOD="none"
if [ "${METHOD}" == "none" ]; then
echo "no need to upgrade for project $PROJECT "
continue
fi
[ -n "$REPO_URL" ] || { MSG="error, project ${PROJECT} miss REPO_URL" ; echo "${MSG}"; FAILED_MESSAGE+="${MSG} \n"; continue ; }
[ -n "$REPO_NAME" ] || { MSG="error, project ${PROJECT} miss REPO_NAME" ; echo "${MSG}"; FAILED_MESSAGE+="${MSG} \n"; continue ; }
[ -n "$CHART_NAME" ] || { MSG="error, project ${PROJECT} miss CHART_NAME" ; echo "${MSG}"; FAILED_MESSAGE+="${MSG} \n"; continue ; }
[ -n "$VERSION" ] || { MSG="error, project ${PROJECT} miss VERSION" ; echo "${MSG}"; FAILED_MESSAGE+="${MSG} \n"; continue ; }
[ -n "$UPGRADE_REVIWER" ] || UPGRADE_REVIWER=${{ env.DEFAULT_REVIEWER }}
echo "${PROJECT} REPO_URL=${REPO_URL}"
echo "${PROJECT} REPO_NAME=${REPO_NAME}"
echo "${PROJECT} CHART_NAME=${CHART_NAME}"
echo "${PROJECT} VERSION=${VERSION}"
echo "${PROJECT} METHOD=${METHOD}"
echo "${PROJECT} UPGRADE_REVIWER=${UPGRADE_REVIWER}"
helm repo remove $REPO_NAME || true
helm repo add $REPO_NAME $REPO_URL || { MSG="error, failed to add REPO_URL $REPO_URL for project ${PROJECT} " ; echo "${MSG}"; FAILED_MESSAGE+="${MSG} \n"; continue ; }
helm repo update $REPO_NAME
helm search repo ${REPO_NAME}/${CHART_NAME} || true
LATEST_VERSION=` helm search repo ${REPO_NAME}/${CHART_NAME} | sed '1 d' | grep -E "^${REPO_NAME}\/${CHART_NAME}[[:space:]]" | awk '{print \$2}' ` || true
[ -n "${LATEST_VERSION}" ] || { MSG="error, failed to find latest chart version for project ${PROJECT} " ; FAILED_MESSAGE+="${MSG} \n"; continue ; }
echo "latest chart version of project ${PROJECT} is $LATEST_VERSION"
if [ "${LATEST_VERSION}" == "${VERSION}" ] ; then
echo "no chart change for project $PROJECT "
continue
fi
echo "begin to upgrade for project $PROJECT "
LABEL="${{ env.ISSUE_LABEL_PREFIX }}/${PROJECT}"
gh label create "${LABEL}" --force || true
git reset --hard HEAD || true
git checkout main
if [ "${METHOD}" == "issue" ]; then
echo "upgrade to issue"
TITLE_PREFIX="Robot Upgrade: ${PROJECT} chart upgrade"
FINAL_TITLE="${TITLE_PREFIX} from $VERSION to ${LATEST_VERSION}"
gh issue list --label "${LABEL}"
HIS=`gh issue list --label "${LABEL}"`
if grep "${FINAL_TITLE}" <<< "$HIS" &>/dev/null ; then
echo "an issue has been submitted for ${PROJECT} ${LATEST_VERSION}, ignore"
continue
fi
if grep "${TITLE_PREFIX}" <<< "$HIS" &>/dev/null ; then
LIST=`grep "${TITLE_PREFIX}" <<< "$HIS" | awk '{print \$1}' | tr '\n' ' ' ` || true
echo "remove historical robot ISSUE for project ${PROJECT}: ${LIST}"
for ISSUE_NUM in ${LIST} ; do
gh issue close ${ISSUE_NUM} -c "robot: found newer version ${LATEST_VERSION}"
done
fi
gh issue create \
--body "I am robot, ${PROJECT} chart could upgrade from $VERSION to ${LATEST_VERSION} " \
--title "${FINAL_TITLE}" \
--label "${LABEL}" \
--assignee "${UPGRADE_REVIWER}"
if (( $? != 0 )) ; then
MSG="error, failed to create PR for project ${PROJECT} for LATEST_VERSION=$LATEST_VERSION"
FAILED_MESSAGE+="${MSG} \n"
git reset --hard HEAD || true
continue
fi
SUCCEED_PROJECT+=" new ${PROJECT} commits to issue \n"
continue
else
sed -iE 's?export VERSION=.*?export VERSION='"${LATEST_VERSION}"'?' ${CONFIG_FILE}
rm -f ${CONFIG_FILE}E || true
if ! make build_chart -e PROJECT=${PROJECT} ; then
MSG="error, failed to build_chart for project ${PROJECT} for LATEST_VERSION=$LATEST_VERSION"
FAILED_MESSAGE+="${MSG} \n"
git reset --hard HEAD || true
continue
fi
git status
if git diff --exit-code --quiet ; then
MSG="error, no code change, when build_chart for project ${PROJECT} for LATEST_VERSION=$LATEST_VERSION"
FAILED_MESSAGE+="${MSG} \n"
git reset --hard HEAD || true
continue
fi
PR_BRANCH=upgrade/${PROJECT}/${LATEST_VERSION}
git checkout -b ${PR_BRANCH}
git add .
git commit -s -m "robot: project ${PROJECT} chart upgrades from $VERSION to ${LATEST_VERSION}"
git push origin ${PR_BRANCH}:${PR_BRANCH} -f
TITLE_PREFIX="Robot Upgrade: ${PROJECT} chart upgrade"
FINAL_TITLE="${TITLE_PREFIX} from $VERSION to ${LATEST_VERSION}"
gh pr list --label "${LABEL}"
HIS=`gh pr list --label "${LABEL}"`
if grep "${FINAL_TITLE}" <<< "$HIS" &>/dev/null ; then
echo "an PR has been submitted for ${PROJECT} ${LATEST_VERSION}, ignore"
continue
fi
if grep "${TITLE_PREFIX}" <<< "$HIS" &>/dev/null ; then
LIST=`grep "${TITLE_PREFIX}" <<< "$HIS" | awk '{print \$1}' | tr '\n' ' ' ` || true
echo "remove historical robot PR for project ${PROJECT}: ${LIST}"
for PR_NUM in ${LIST} ; do
gh pr close ${PR_NUM} -c "robot: found newer version ${LATEST_VERSION}"
done
fi
gh pr create --title "${FINAL_TITLE}" \
--assignee "${UPGRADE_REVIWER}" \
--label "${LABEL}" \
--body "I am robot, upgrade: project ${PROJECT} chart upgrade from $VERSION to ${LATEST_VERSION}" \
--base main
if (( $? != 0 )) ; then
MSG="error, failed to create PR for project ${PROJECT} for LATEST_VERSION=$LATEST_VERSION"
FAILED_MESSAGE+="${MSG} \n"
git reset --hard HEAD || true
continue
fi
SUCCEED_PROJECT+=" new ${PROJECT} commits to pr\n"
echo "succeeded to upgrade project ${PROJECT} chart upgrade from $VERSION to ${LATEST_VERSION}"
fi
done
echo "ENV_FAILED_MESSAGE=${FAILED_MESSAGE}" >> $GITHUB_ENV
echo "SUCCEED_PROJECT=${SUCCEED_PROJECT}" >> $GITHUB_ENV
- name: result
env:
GITHUB_TOKEN: ${{ secrets.WELAN_PAT}}
run: |
SUCCEED_PROJECT="${{ env.SUCCEED_PROJECT }}"
FAILED_MESSAGE="${{ env.ENV_FAILED_MESSAGE }}"
echo -e "SUCCEED_PROJECT=${SUCCEED_PROJECT}"
echo -e "FAILED_MESSAGE=${FAILED_MESSAGE}"
if [ -n "$FAILED_MESSAGE" ] ; then
exit 1
else
exit 0
fi