-
Notifications
You must be signed in to change notification settings - Fork 10
319 lines (318 loc) · 14.1 KB
/
merge-acm-flow.yaml
File metadata and controls
319 lines (318 loc) · 14.1 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
name: Common merge flow
on:
workflow_call:
inputs:
node-version:
description: node version
default: '16'
required: false
type: string
go-version:
description: go version
required: true
type: string
upstream:
description: Upstream repo path in owner/repo format
required: true
type: string
downstream:
description: Downstream repo path in owner/repo format
required: true
type: string
downstream-branch:
description: Downstream branch to create PR
required: false
default: release-2.12
type: string
sandbox:
description: Sandbox repo path in owner/repo format. Used as a base to create PR against downstream.
required: true
type: string
restore-upstream:
description: List of files to be reset using upstream content on merge conflict.
required: false
default: ''
type: string
restore-downstream:
description: List of files to be reset using downstream content on merge conflict.
required: false
default: ''
type: string
assets-cmd:
description: Commands which generates assets.
required: false
default: ''
type: string
downstream-version-expression:
description: Expression to extract downstream version from downstream repo.
required: false
default: ''
type: string
go-mod-tidy:
description: Run `go mod tidy` after a merge
required: false
default: true
type: boolean
secrets:
cloner-app-id:
description: Github ID of cloner app
required: true
cloner-app-private-key:
description: Github private key of cloner app
required: true
pr-app-id:
description: Github ID of PR creation app
required: true
pr-app-private-key:
description: Github private key of PR creation app
required: true
slack-webhook-url:
description: Slack webhook URL to send notification
required: true
jobs:
merge:
runs-on: ubuntu-latest
name: Perform merge operation
steps:
- name: Get latest upstream tag
id: upstream
run: |
UPSTREAM_VERSION=$(curl --fail --silent "https://api.github.com/repos/${{ inputs.upstream }}/releases/latest" | jq -r '.tag_name')
if [ "$UPSTREAM_VERSION" == "" ]; then
echo "upstream-version is invalid" >> "$GITHUB_OUTPUT"
exit 1
fi
echo "release=${UPSTREAM_VERSION}" >> "$GITHUB_OUTPUT"
- name: Find github org name from repo name
id: org
run: |
{
echo "upstream=$(dirname ${{ inputs.upstream }})"
echo "downstream=$(dirname ${{ inputs.downstream }})"
echo "sandbox=$(dirname ${{ inputs.sandbox }})"
} >> "$GITHUB_OUTPUT"
DOWNSTREAM_VERSION=$(curl -sL "https://raw.githubusercontent.com/${{ inputs.downstream }}/${{ inputs.downstream-branch }}/VERSION")
if [[ "${DOWNSTREAM_VERSION}" =~ ^$|"404: Not Found" ]]; then
# Strip the trailing URL from the expression.
DOWNSTREAM_VERSION_SED=$(echo "${{ inputs.downstream-version-expression }}" | sed -e 's/\(http[^ ]*\).*$/\1/' -e 's/http[^ ]*$//')
# Strip the leading sed command from the expression.
DOWNSTREAM_VERSION_URL=$(echo "${{ inputs.downstream-version-expression }}" | sed -n 's/^.*\(http[^ ]*\).*$/\1/p')
if [ "${DOWNSTREAM_VERSION_SED}" == "" ] || [ "${DOWNSTREAM_VERSION_URL}" == "" ]; then
echo "::error::downstream-version-expression is invalid"
exit 1
fi
DOWNSTREAM_VERSION=$(curl --silent "${DOWNSTREAM_VERSION_URL}" | eval "${DOWNSTREAM_VERSION_SED}")
if ! [[ "${DOWNSTREAM_VERSION}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "::error::downstream-version-expression is invalid"
exit 1
fi
fi
echo "downstream-version=${DOWNSTREAM_VERSION}" >> "$GITHUB_OUTPUT"
- uses: madhead/semver-utils@latest
id: version
with:
version: ${{ steps.org.outputs.downstream-version }}
compare-to: ${{ steps.upstream.outputs.release }}
lenient: false # fail if either of the versions cannot be parsed
- name: Check ACM stolostron fork status
id: fork-sync
run: |
SEMVER_RESULT="${{ steps.version.outputs.comparison-result }}"
echo "${{ inputs.downstream }}@${{ steps.org.outputs.downstream-version }} ${SEMVER_RESULT} ${{ inputs.upstream }}@${{ steps.upstream.outputs.release }}"
if [ "${SEMVER_RESULT}" == "<" ]; then
echo "status=outdated" >> "$GITHUB_OUTPUT"
echo "::notice::downstream outdated"
elif [ "${SEMVER_RESULT}" == "=" ]; then
echo "status=uptodate" >> "$GITHUB_OUTPUT"
echo "::notice::downstream up-to-date"
exit 0
else
echo "status=ahead" >> "$GITHUB_OUTPUT"
echo "::notice::downstream ahead"
exit 0
fi
- uses: actions/checkout@v4
with:
repository: ${{ inputs.downstream }}
fetch-depth: 0
ref: ${{ inputs.downstream-branch }}
- name: Fetch all upstream tags
run: |
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
git config --global core.editor "/bin/true"
git fetch https://github.com/${{ inputs.upstream }} --tags
- name: Merge with upstream ${{ steps.upstream.outputs.release }} tag
id: merge
run: |
git merge -X theirs refs/tags/${{ steps.upstream.outputs.release }} --no-edit || echo 'MERGE_CONFLICT=true' >> "$GITHUB_OUTPUT"
- name: Resolve conflict using upstream contents
if: ${{ steps.merge.outputs.MERGE_CONFLICT == 'true' && inputs.restore-upstream != ''}}
run: |
echo "reset ${{ inputs.restore-upstream }}"
git checkout --theirs ${{ inputs.restore-upstream }} || true
git add ${{ inputs.restore-upstream }} || true
- name: Remove deleted files
if: ${{ steps.merge.outputs.MERGE_CONFLICT == 'true' }}
run: |
git diff --name-only --diff-filter=D | xargs -r git rm
- name: Resolve conflict using downstream contents
if: ${{ steps.merge.outputs.MERGE_CONFLICT == 'true' && inputs.restore-downstream != ''}}
run: |
echo "reset ${{ inputs.restore-downstream }}"
git checkout --ours ${{ inputs.restore-downstream }}
git add ${{ inputs.restore-downstream }}
- name: Resolve conflict due to deleted downstream files
if: ${{ steps.merge.outputs.MERGE_CONFLICT == 'true' }}
run: |
git status --porcelain | awk '{ if ($1=="DU") print $2 }' | xargs -I {} git rm {}
- name: Continue after merge conflict
if: ${{ steps.merge.outputs.MERGE_CONFLICT == 'true' }}
run: git merge --continue
- name: Add VERSION file if not present
run: |
# All tags use the vX.Y.Z format currently.
version_from_tag=$(echo ${{ steps.upstream.outputs.release }} | sed -e "s/^v//")
if [ -f VERSION ]; then
version_from_file=$(cat VERSION)
if [ "$version_from_tag" != "$version_from_file" ];then
echo "::error:: tag version ${version_from_tag} doesn't correspond to version ${version_from_file} from VERSION file"
exit 1
fi
echo "::notice::VERSION file already present"
exit 0
fi
echo "$version_from_tag" > VERSION
git add VERSION
git diff --cached --exit-code || git commit -s -m "[bot] add VERSION file with ${version_from_tag}"
- uses: actions/setup-go@v5
with:
go-version: ${{ inputs.go-version }}
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
- name: go mod tidy + vendor
if: ${{ inputs.go-mod-tidy }}
run: |
go mod tidy
git add go.mod go.sum
git diff --cached --exit-code || git commit -s -m "[bot]go mod tidy"
- name: Generate assets
if: ${{ inputs.assets-cmd != '' }}
run: ${{ inputs.assets-cmd }}
- name: Get auth token to create pull request for ${{ inputs.downstream }}
if: github.event_name != 'pull_request'
id: pr
uses: getsentry/action-github-app-token@v3
with:
app_id: ${{ secrets.pr-app-id }}
private_key: ${{ secrets.pr-app-private-key }}
scope: ${{ steps.org.outputs.downstream }}
- name: Get auth token to push to ${{ inputs.sandbox }}
if: github.event_name != 'pull_request'
id: cloner
uses: getsentry/action-github-app-token@v3
with:
app_id: ${{ secrets.cloner-app-id }}
private_key: ${{ secrets.cloner-app-private-key }}
scope: ${{ steps.org.outputs.sandbox }}
- name: Create Pull Request
if: github.event_name != 'pull_request'
uses: rhobs/acm-create-pull-request@push-to-fork-token
id: create-pr
with:
title: "[ACM Obs bot] Bump ${{ inputs.downstream }} to ${{ steps.upstream.outputs.release }}"
body: |
## Description
This is an automated version bump from CI.
The logs for this run can be found [in the syncbot repo actions](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}).
If you wish to perform this manually, execute the following commands from ${{ inputs.downstream }} repo:
```
git fetch https://github.com/${{ inputs.upstream }} --tags
if ! git merge refs/tags/${{ steps.upstream.outputs.release }} --no-edit; then
git checkout --theirs ${{ inputs.restore-upstream }}
git checkout --ours ${{ inputs.restore-downstream }}
git add ${{ inputs.restore-upstream }} ${{ inputs.restore-downstream }}
git merge --continue
fi
go mod tidy
go mod vendor
${{ inputs.assets-cmd }}
if [ -f scripts/rh-manifest.sh ]; then
bash scripts/rh-manifest.sh
git add rh-manifest.txt
git diff --cached --exit-code || git commit -s -m "[bot] update rh-manifest.txt"
fi
```
author: 'github-actions[bot]<github-actions[bot]@users.noreply.github.com>'
committer: 'github-actions[bot]<github-actions[bot]@users.noreply.github.com>'
signoff: true
branch: automated-updates-acm-${{ inputs.downstream-branch }}
delete-branch: true
token: ${{ steps.pr.outputs.token }}
push-to-fork: ${{ inputs.sandbox }}
push-to-fork-token: ${{ steps.cloner.outputs.token }}
- name: Check if PR exists using gh cli
# This step is a hack for the time being as the create-pull-request action has an issue with listing PRs across different named forks of the same repo.
# This is only to get the PR URL if it exists.
if: github.event_name != 'pull_request' && failure()
id: pr-exists
env:
GH_TOKEN: ${{ steps.pr.outputs.token }}
run: |
if [ "${{ steps.create-pr.outcome }}" != "success" ]; then
echo "${{ steps.create-pr.outcome }}"
PR_URL=$(gh pr list --json url --jq '.[0].url' --repo ${{ inputs.downstream }} --state open --head automated-updates-acm-${{ inputs.downstream-branch }})
if [ -n "$PR_URL" ]; then
echo "pr_exists=1" >> "$GITHUB_OUTPUT"
echo "pr_url=$PR_URL" >> "$GITHUB_OUTPUT"
echo "PR exists >> $PR_URL"
else
echo "pr_exists=0" >> "$GITHUB_OUTPUT"
fi
fi
- name: Compose slack message body
if: github.event_name != 'pull_request' && (success() || steps.fork-sync.outputs.status == 'uptodate' || steps.fork-sync.outputs.status == 'ahead' || steps.pr-exists.outputs.pr_exists == '1' )
continue-on-error: true
id: slack-message
run: |
if [ "${{ steps.pr-exists.outputs.pr_exists }}" == "1" ]; then
PR_URL="${{ steps.pr-exists.outputs.pr_url }}"
else
PR_URL="${{ steps.create-pr.outputs.pull-request-url }}"
fi
if [ "${{ steps.fork-sync.outputs.status }}" == "uptodate" ] || [ "${{ steps.fork-sync.outputs.status }}" == "ahead" ]; then
echo "message=${{ inputs.downstream }} is already ${{ steps.fork-sync.outputs.status }} with tag ${{ steps.org.outputs.downstream-version }}." >> "$GITHUB_OUTPUT"
else
echo "message=PR $PR_URL has been ${{ steps.create-pr.outputs.pull-request-operation || 'updated' }}." >> "$GITHUB_OUTPUT"
fi
- uses: 8398a7/action-slack@v3
if: github.event_name != 'pull_request' && (success() || steps.fork-sync.outputs.status == 'uptodate' || steps.fork-sync.outputs.status == 'ahead' || steps.pr-exists.outputs.pr_exists == '1')
continue-on-error: true
with:
status: custom
fields: workflow
custom_payload: |
{
attachments: [{
color: 'good',
text: `${process.env.AS_WORKFLOW}\n ${{ steps.slack-message.outputs.message }}`,
}]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.slack-webhook-url }}
- uses: 8398a7/action-slack@v3
if: github.event_name != 'pull_request' && (failure() && steps.fork-sync.outputs.status != 'uptodate' && steps.fork-sync.outputs.status != 'ahead' && !(steps.pr-exists.outputs.pr_exists == '1'))
continue-on-error: true
with:
status: custom
fields: workflow
custom_payload: |
{
attachments: [{
color: 'danger',
text: `${process.env.AS_WORKFLOW} has failed.`,
}]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.slack-webhook-url }}