Skip to content

Commit 4e0709b

Browse files
authored
ci(mirror): adjust mirroring workflow (#296)
* adjust * add workflow_dispatch * refactor * r * fix condition
1 parent bdd04e9 commit 4e0709b

File tree

1 file changed

+63
-9
lines changed

1 file changed

+63
-9
lines changed

.github/workflows/mirror.yml

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,85 @@
11
name: Trigger Mirror
22

33
on:
4+
delete:
45
push:
6+
branches-ignore:
7+
- 'renovate/**'
58
workflow_dispatch:
9+
release:
10+
types: [released]
611

7-
permissions:
8-
contents: read
12+
# For dynamic naming of the workflow runs
13+
run-name: >-
14+
Trigger ${{ github.event_name }}:
15+
${{
16+
contains(fromJSON('["push", "workflow_dispatch"]'), github.event_name) && github.ref_name ||
17+
github.event_name == 'release' && github.event.release.tag_name ||
18+
github.event_name == 'delete' && github.event.ref ||
19+
''
20+
}}
21+
22+
# This will ensure that only one workflow is active for each event (and it's ref) by canceling previously triggered active workflow runs.
23+
# For example, if there are multiple pushes to a branch in a short time frame, only the last one will be processed, the rest will be canceled.\
24+
# See docs: https://docs.github.com/en/actions/using-jobs/using-concurrency
25+
concurrency:
26+
group: >-
27+
${{ github.workflow }}-${{ github.event_name }}-${{
28+
github.event_name == 'push' && github.ref_name ||
29+
github.event_name == 'release' && github.event.release.tag_name ||
30+
github.event_name == 'delete' && github.event.ref ||
31+
''
32+
}}
33+
cancel-in-progress: true
934

1035
jobs:
1136
trigger_mirror:
1237
name: Trigger Mirror Workflow
1338
runs-on: ubuntu-latest
39+
env:
40+
EVENT_TYPE: >-
41+
${{
42+
contains(fromJSON('["push", "workflow_dispatch"]'), github.event_name) && 'mirror-push' ||
43+
github.event_name == 'release' && 'mirror-tag' ||
44+
github.event_name == 'delete' && 'mirror-delete-ref' || ''
45+
}}
46+
PAYLOAD_KEY: >-
47+
${{
48+
contains(fromJSON('["push", "workflow_dispatch"]'), github.event_name) && 'source_branch' ||
49+
github.event_name == 'release' && 'tag_name' ||
50+
github.event_name == 'delete' && 'delete_ref' || ''
51+
}}
52+
PAYLOAD_VALUE: >-
53+
${{
54+
contains(fromJSON('["push", "workflow_dispatch"]'), github.event_name) && github.ref_name ||
55+
github.event_name == 'release' && github.event.release.tag_name ||
56+
github.event_name == 'delete' && github.event.ref || ''
57+
}}
1458
steps:
15-
- name: Trigger mirror workflow
59+
- name: Trigger ${{ env.EVENT_TYPE}} with payload ${{ env.PAYLOAD_KEY}}=${{ env.PAYLOAD_VALUE}}
1660
run: |
17-
echo "branch name: ${{ github.head_ref || github.ref_name }}"
18-
response=$(curl -L \
61+
response=$(curl -s -w "%{http_code}" -o mirror_response.txt \
1962
-X POST \
2063
-H "Accept: application/vnd.github.v3+json" \
2164
-H "Authorization: Bearer ${{ secrets.MIRROR_WORKFLOW_TOKEN }}" \
22-
-w "%{http_code}" \
2365
--max-time 30 \
2466
"${{ secrets.MIRROR_API_URL }}/dispatches" \
25-
-d '{"event_type":"mirror_trigger","client_payload":{"source_repo":"https://github.com/${{ github.repository }}", "source_branch":"${{ github.head_ref || github.ref_name }}"}}')
67+
-d '{
68+
"event_type": "${{ env.EVENT_TYPE}}",
69+
"client_payload": {
70+
"source_repo": "${{ github.repository }}",
71+
"${{ env.PAYLOAD_KEY}}": "${{ env.PAYLOAD_VALUE }}"
72+
}
73+
}'
74+
)
75+
76+
status_code="${response: -3}"
2677
27-
status_code=${response: -3}
28-
if [ "$status_code" -ne 204 ]; then
78+
if [ "$status_code" -eq 204 ]; then
79+
echo "Mirror workflow triggered successfully."
80+
else
2981
echo "Failed to trigger mirror workflow. Status code: $status_code"
82+
echo "Response body:"
83+
cat mirror_response.txt
3084
exit 1
3185
fi

0 commit comments

Comments
 (0)