-
Notifications
You must be signed in to change notification settings - Fork 0
187 lines (157 loc) · 7.68 KB
/
update-starter-workflows.yml
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
name: Update starter workflows
on:
push:
branches: [trunk]
paths:
- '.github/workflows/example-*.yml'
- '.github/workflows/update-starter-workflows.yml'
pull_request:
branches: [trunk]
paths:
- '.github/workflows/example-*.yml'
- '.github/workflows/update-starter-workflows.yml'
env:
REPOSITORY: 'core-github-actions-templates'
concurrency:
group: '${{ github.workflow }}-${{ github.ref }}'
cancel-in-progress: true
jobs:
update-starter-workflows:
name: Update starter workflows
runs-on: elvia-runner
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
path: ${{ env.REPOSITORY }}
- name: Get GitHub App token
uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ vars.GH_APP_ID }}
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
owner: ${{ github.repository_owner }}
- name: Checkout .github repository
uses: actions/checkout@v4
with:
path: 'dotgithub'
repository: '3lvia/.github'
token: ${{ steps.app-token.outputs.token }}
- name: Update workflows-templates
shell: bash
run: |
mkdir -p dotgithub/workflow-templates
for workflow_path in ${{ env.REPOSITORY }}/.github/workflows/example-*.yml; do
echo "Processing $workflow_path"
file_name=$(basename "$workflow_path")
file_name_no_prefix="${file_name#example-}"
printf "file_name: %s\n" "$file_name"
printf "file_name_no_prefix: %s\n" "$file_name_no_prefix"
new_workflow_path="dotgithub/workflow-templates/$file_name_no_prefix"
cp "$workflow_path" "$new_workflow_path"
sed -i "s/ APPLICATION_NAME: '.*'/ APPLICATION_NAME: '<your application name here>'/g" "$new_workflow_path"
sed -i "s/ SYSTEM_NAME: '.*'/ SYSTEM_NAME: '<your system name here>'/g" "$new_workflow_path"
sed -i "s/ HELM_VALUES_FILE: '.*'/ HELM_VALUES_FILE: '.github\/deploy\/values.yml'/g" "$new_workflow_path"
sed -i "s/ PROJECT_FILE: '.*'/ PROJECT_FILE: '<your project file path here>'/g" "$new_workflow_path"
sed -i "s/# pull_request:/ pull_request:/g" "$new_workflow_path"
sed -i 's/# branches:/ branches:/g' "$new_workflow_path"
sed -i 's/branches: \[trunk\]/branches: \[$default-branch\]/g' "$new_workflow_path"
sed -i "s/checkout: 'false'.*\$//g" "$new_workflow_path"
# We disable Trivy uploading report in testing, so reenable here.
sed -i "s/trivy-upload-report: 'false'/trivy-upload-report: 'true'/g" "$new_workflow_path"
# We checkout core repo to test on demo apps, so we remove this part from the examples.
perl -0777 -i -pe 's/# START REMOVE FROM EXAMPLE.*?# END REMOVE FROM EXAMPLE\n//gms' "$new_workflow_path"
# Special case for ISS example
sed -i 's/branches: \[too-complicated-to-setup-tests-for-this-so-we-dont\]/branches: \[$default-branch\]/g' "$new_workflow_path"
# Special case for replacing name of concurrency group for demo-api-go, since it is deployed twice (go.mod and Dockerfile).
# TODO: loop and don't hardcode envs three times
sed -i "s/group: 'dev-.*--this-group-is-replaced-in-final-example-needs-this-to-not-deploy-demo-api-go-simultaneously-in-two-examples'/group: '${{ '\${{ github.workflow }}-\${{ github.ref }}-deploy-dev' }}'/g" "$new_workflow_path"
sed -i "s/group: 'test-.*--this-group-is-replaced-in-final-example-needs-this-to-not-deploy-demo-api-go-simultaneously-in-two-examples'/group: '${{ '\${{ github.workflow }}-\${{ github.ref }}-deploy-test' }}'/g" "$new_workflow_path"
sed -i "s/group: 'prod-.*--this-group-is-replaced-in-final-example-needs-this-to-not-deploy-demo-api-go-simultaneously-in-two-examples'/group: '${{ '\${{ github.workflow }}-\${{ github.ref }}-deploy-prod' }}'/g" "$new_workflow_path"
new_workflow_properties_file="dotgithub/workflow-templates/${file_name_no_prefix%.yml}.properties.json"
if [ ! -f "$new_workflow_properties_file" ]; then
echo "Did not find properties file $new_workflow_properties_file, creating new properties file for workflow $new_workflow_path."
new_workflow_name=$(yq -r '.name' "$new_workflow_path")
cat << EOF > "$new_workflow_properties_file"
{
"name": "$new_workflow_name",
"description": "$new_workflow_name"
}
EOF
else
echo "Properties file $new_workflow_properties_file already exists for $new_workflow_path, will not overwrite."
fi
done
- name: Get examples diff
if: github.event_name == 'pull_request'
id: get-diff
run: |
git add workflow-templates
examples_files_changed=$(git diff --name-only --staged workflow-templates | base64 -w0)
examples_diff=$(git diff --staged workflow-templates | base64 -w0)
echo "examples-files-changed=$examples_files_changed" >> "$GITHUB_OUTPUT"
echo "examples-diff=$examples_diff" >> "$GITHUB_OUTPUT"
working-directory: 'dotgithub'
- name: Add comment with diff
if: github.event_name == 'pull_request' && steps.get-diff.outputs.examples-files-changed != ''
uses: actions/github-script@v7
with:
github-token: ${{ steps.app-token.outputs.token }}
script: |
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const header = "## 📝 Starter Workflow Templates Update\n";
const examplesFilesChanged = atob('${{ steps.get-diff.outputs.examples-files-changed }}');
const examplesDiff = atob('${{ steps.get-diff.outputs.examples-diff }}');
const botComment = comments.find(
(comment) =>
comment.user.type === "Bot" &&
comment.body.includes(header)
);
const body = `${header}
The starter workflow templates have been updated. Please review the changes below.\n
**Files changed:**\n
${examplesFilesChanged}
**<details><summary>Full diff:</summary>**
\`\`\`\n
${examplesDiff}
\`\`\`
</details>
`;
if (botComment) {
github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body,
});
} else {
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body,
});
}
- name: Commit changes
shell: bash
if: github.event_name == 'push'
run: |
if [[ -z "$(git status --porcelain)" ]]; then
echo 'No changes to commit.'
exit 0
fi
git config user.email '${{ vars.GH_APP_USER_EMAIL }}'
git config user.name '${{ vars.GH_APP_USERNAME }}'
git add workflow-templates
git commit -m 'Update starter workflows'
for git_file in $(git ls-files); do
cat "$git_file"
done
git push
working-directory: 'dotgithub'
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}