-
Notifications
You must be signed in to change notification settings - Fork 30
169 lines (155 loc) · 7.51 KB
/
auto-release.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
name: Auto Release Chart
env:
DEFAULT_FAIL_REVIEWER: "weizhoublue"
DEFAULT_SUCCEED_REVIEWER: "liyuerich,ty-dc"
on:
push:
branches:
- 'main'
# auto trigger, in case push failure sometimes
schedule:
# each day
- cron: "0 20 * * *"
jobs:
get_chart:
runs-on: ubuntu-latest
outputs:
ENV_charts_changed: ${{ env.ENV_charts_changed }}
ENV_PR_NUMBER: ${{ env.ENV_PR_NUMBER }}
ENV_PR_AUTHOR: ${{ env.ENV_PR_AUTHOR }}
ENV_JUST_PUSH: ${{ env.ENV_JUST_PUSH }}
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: get chart when schedule
run : |
if ${{ github.event_name == 'push' }} ; then
exit 0
fi
echo "ENV_charts_changed=all" >> $GITHUB_ENV
echo "ENV_JUST_PUSH=true" >> $GITHUB_ENV
- name: get chart when push
env:
GITHUB_TOKEN: ${{ secrets.WELAN_PAT}}
JSON: ${{ toJSON(github) }}
run: |
set -x
if ${{ github.event_name != 'push' }} ; then
exit 0
fi
echo "============ print var ======== "
grep -Eio "Merge pull request #[0-9]+ " <<< "${JSON}" || true
PR_NUMBER=` grep -Eio "Merge pull request #[0-9]+ " <<< "${JSON}" | grep -Eo "[0-9]+" | uniq ` || true
[ -n "${PR_NUMBER}" ] || { echo "no PR number, ignore" ; exit 0 ; }
PR_URL="https://github.com/${{ github.repository }}/pull/${PR_NUMBER}"
ACTION_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
grep '"username":' <<< "${JSON}"
PR_AUTHOR=` grep '"username":' <<< "${JSON}" | awk -F'"' '{print $4}' | sort | uniq | grep -v "web-flow" | head -1 | tr -d '\n' `
[ -n "${PR_AUTHOR}" ] || { echo "no PR_AUTHOR, ignore" ; }
gh pr view ${PR_NUMBER}
PR_LABEL=` gh pr view ${PR_NUMBER} | grep -i "^labels:" | tr ',' ' ' | tr -s ' ' | sed 's/labels://g' `
[ -n "${PR_LABEL}" ] || { echo "no PR_LABEL, ignore" ; }
PR_TITLE=`gh pr view ${PR_NUMBER} | sed -n '1 p' `
[ -n "${PR_TITLE}" ] || { echo "error, no PR_TITLE " ; exit 1 ; }
#
grep -Ei "https://github.com/.*/commit" <<< "${JSON}"
PR_COMMITS=` grep -Ei "https://github.com/.*/commit" <<< "${JSON}" | awk -F'"' '{print $4}' | uniq -c | awk '{ if ($1 == 1 ) print $2}' | awk -F'/' '{print $NF}' | tr '\n' ' ' `
[ -n "${PR_COMMITS}" ] || { echo "no PR_COMMITS, ignore" ; }
#
echo "number: ${PR_NUMBER}"
echo "action url: ${ACTION_URL}"
echo "PR_COMMITS: ${PR_COMMITS}"
echo "author: ${PR_AUTHOR}"
echo "url: ${PR_URL}"
echo "PR_LABEL: ${PR_LABEL}"
echo "PR_TITLE: ${PR_TITLE}"
echo "============= get changed chart========"
URL="https://api.github.com/repos/${{ github.repository }}/pulls/${PR_NUMBER}/files"
files_changed_data=$(curl -s --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' -X GET -G "$URL")
echo "files_changed_data: $files_changed_data"
files_changed="$(echo $files_changed_data | jq -r '.[] | .filename')"
echo "files_changed: $files_changed"
#--------------------
# Adding || true to avoid "Process exited with code 1" errors
charts_changed="$(echo "$files_changed" | xargs dirname | grep -E -o "charts/([^/]*)/\1" | grep -o "charts/[^/]*" | sort | uniq | awk -F'/' '{print $2}' | tr '\n' ' ' || true)"
echo "charts_changed: $charts_changed"
echo "ENV_charts_changed=${charts_changed}" >> $GITHUB_ENV
echo "ENV_PR_AUTHOR=${PR_AUTHOR}" >> $GITHUB_ENV
echo "ENV_PR_NUMBER=${PR_NUMBER}" >> $GITHUB_ENV
call_release:
needs: [get_chart]
if: ${{ needs.get_chart.outputs.ENV_charts_changed != '' }}
uses: ./.github/workflows/release-chart.yml
with:
project: "${{ needs.get_chart.outputs.ENV_charts_changed }}"
ref: "main"
justpush: "${{ needs.get_chart.outputs.ENV_JUST_PUSH }}"
secrets: inherit
create_issue:
needs: [get_chart,call_release]
if: ${{ needs.get_chart.outputs.ENV_JUST_PUSH != 'true' && needs.get_chart.outputs.ENV_charts_changed != '' && always() }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: create issue
env:
GITHUB_TOKEN: ${{ secrets.WELAN_PAT}}
run : |
set -x
PROJECT_LIST="${{ needs.get_chart.outputs.ENV_charts_changed }}"
ACTION_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
PR_NUMBER="${{ needs.get_chart.outputs.ENV_PR_NUMBER }}"
PR_URL="https://github.com/${{ github.repository }}/pull/${PR_NUMBER}"
PROJECT_INFO=""
for PROJECT in $PROJECT_LIST ; do
VERSION=`source charts/${PROJECT}/config && echo ${VERSION} `
PROJECT_INFO+=" ${PROJECT}<${VERSION}> "
done
echo "privious result: ${{ needs.call_release.result }}"
if ${{ needs.call_release.result != 'success' }} ; then
echo "failed to release chart ${PROJECT_INFO} "
LABEL="robot/release-chart-fail"
gh label create "${LABEL}" --force || true
#
REVIWER=${{ needs.get_chart.outputs.ENV_PR_AUTHOR }}
[ -n "${REVIWER}" ] || REVIWER=${{ env.DEFAULT_FAIL_REVIEWER }}
#
gh issue create \
--body "I am robot, failed to release chart ${PROJECT_INFO} to daocloud chart registery. Trigger by PR <${PR_URL}>. Refer to Github Action <${ACTION_URL}>" \
--title "robot: failed to release ${PROJECT_INFO} by PR ${PR_NUMBER}" \
--label "${LABEL}" \
--assignee "${REVIWER}"
else
echo "succeeded to release chart ${PROJECT_INFO} "
LABEL="robot/release-chart-succeed"
gh label create "${LABEL}" --force || true
FAILED=""
for PROJECT in $PROJECT_LIST ; do
VERSION=`source charts/${PROJECT}/config && echo ${VERSION} `
TEST_ASSIGNER=`source charts/${PROJECT}/config && echo ${TEST_ASSIGNER} ` || true
if [ -n "$TEST_ASSIGNER" ] ; then
echo "valid assigner $TEST_ASSIGNER"
else
echo "invalid assigner $TEST_ASSIGNER, assign to default"
TEST_ASSIGNER=${{ env.DEFAULT_SUCCEED_REVIEWER }}
fi
echo "---------- create issue for $PROJECT -------"
# maybe the TEST_ASSIGNER is wrong or not existed, failed to execute
gh issue create \
--body "I am robot, Trigger by PR <${PR_URL}>, charts ${PROJECT} ${VERSION} has been released with new version to daocloud chart registery. Refer to Github Action <${ACTION_URL}>" \
--title "robot: test new version ${PROJECT} ${VERSION} by PR ${PR_NUMBER}" \
--label "${LABEL}" \
--assignee "${TEST_ASSIGNER}" || FAILED="true"
done
if [ -n "$FAILED" ] ; then
echo "failed to create issue for some project"
exit 1
else
echo "all succeed"
fi
fi