|
1 | 1 | name: Trigger Mirror
|
2 | 2 |
|
3 | 3 | on:
|
| 4 | + delete: |
4 | 5 | push:
|
| 6 | + branches-ignore: |
| 7 | + - 'renovate/**' |
5 | 8 | workflow_dispatch:
|
| 9 | + release: |
| 10 | + types: [released] |
6 | 11 |
|
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 |
9 | 34 |
|
10 | 35 | jobs:
|
11 | 36 | trigger_mirror:
|
12 | 37 | name: Trigger Mirror Workflow
|
13 | 38 | 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 | + }} |
14 | 58 | steps:
|
15 |
| - - name: Trigger mirror workflow |
| 59 | + - name: Trigger ${{ env.EVENT_TYPE}} with payload ${{ env.PAYLOAD_KEY}}=${{ env.PAYLOAD_VALUE}} |
16 | 60 | 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 \ |
19 | 62 | -X POST \
|
20 | 63 | -H "Accept: application/vnd.github.v3+json" \
|
21 | 64 | -H "Authorization: Bearer ${{ secrets.MIRROR_WORKFLOW_TOKEN }}" \
|
22 |
| - -w "%{http_code}" \ |
23 | 65 | --max-time 30 \
|
24 | 66 | "${{ 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}" |
26 | 77 |
|
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 |
29 | 81 | echo "Failed to trigger mirror workflow. Status code: $status_code"
|
| 82 | + echo "Response body:" |
| 83 | + cat mirror_response.txt |
30 | 84 | exit 1
|
31 | 85 | fi
|
0 commit comments