-
Notifications
You must be signed in to change notification settings - Fork 0
179 lines (155 loc) · 6.7 KB
/
trigger-auto-merge.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
170
171
172
173
174
175
176
177
178
179
name: Trigger Auto Merge
on:
push:
branches:
- metadata # Trigger only on the 'metadata' branch
paths:
- 'executions/**/test-report.html' # File path pattern for test-report.html
jobs:
setup:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.matrix.outputs.config }}
metadata_file: ${{ steps.metadata.outputs.metadata_file }}
trigger_automerge: ${{ steps.matrix.outputs.trigger_automerge }}
steps:
- name: Checkout repository
uses: actions/checkout@v3
- uses: dorny/paths-filter@v3
id: filter
with:
# Enable listing of files matching each filter.
# Paths to files will be available in `${FILTER_NAME}_files` output variable.
# Paths will be escaped and space-delimited.
# Output is usable as command-line argument list in Linux shell
list-files: shell
# Detect changes against the most recent commit on the same branch before the push
base: ${{ github.ref }}
# Only filters added or modified files.
filters: |
test_report:
- added|modified: 'executions/**/test-report.html'
- name: Print File Paths
if: ${{ steps.filter.outputs.test_report == 'true' }}
id: metadata
env:
TEST_REPORT_FILE: ${{ steps.filter.outputs.test_report_files }}
run: |
echo "metadata_file=${TEST_REPORT_FILE%/*}/metadata.yaml" | tee -a $GITHUB_OUTPUT
echo "Test Report File: ${{ env.TEST_REPORT_FILE }}"
- name: Generate Matrix Config
if: ${{ steps.filter.outputs.test_report == 'true' }}
id: matrix
run: |
test_result=$(yq -o json ${{ steps.metadata.outputs.metadata_file }} | jq -r '.metadata.test_result')
test_result=$(echo "$test_result" | tr '[:upper:]' '[:lower:]')
echo "test_result=$test_result"
if [ "$test_result" == "pass" ]; then
echo "Tests have passed successfully. Auto-merge will now be triggered!"
echo "trigger_automerge=yes" >> $GITHUB_OUTPUT
else
echo "Tests have failed. Auto-merge will be skipped!"
echo "trigger_automerge=no" >> $GITHUB_OUTPUT
fi
config=$(yq -o json ${{ steps.metadata.outputs.metadata_file }} | jq -c '.metadata.git')
echo "${config}" | jq '.'
length=$(echo $config | jq '. | length')
echo "$length repo(s) will be synced by this workflow"
if [[ $length -eq 0 ]]
then
echo "No valid repos available for auto-merge."
exit 1
fi
echo "config=$config" >> $GITHUB_OUTPUT
sync:
name: sync (${{ matrix.mapping.name }})
needs: [setup]
if: ${{ needs.setup.outputs.trigger_automerge == 'yes' }}
runs-on: ubuntu-latest
outputs:
metadata_file: ${{ needs.setup.outputs.metadata_file }}
strategy:
fail-fast: false
matrix:
mapping: ${{ fromJSON(needs.setup.outputs.matrix) }}
steps:
- name: Print matrix config
run: |
echo ${{ matrix.mapping.ignore_files }}
echo ${{ join(matrix.mapping.ignore_files, ',') }}
echo ${{ needs.setup.outputs.matrix }}
- name: Checkout repository
uses: actions/checkout@v3
with:
ref: main
- uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ secrets.APP_ID }}
private-key: ${{ secrets.PRIVATE_KEY }}
owner: ${{ github.repository_owner }}
- name: Get GitHub App User ID
id: get-user-id
run: echo "user-id=$(gh api "/users/${{ steps.app-token.outputs.app-slug }}[bot]" --jq .id)" >> "$GITHUB_OUTPUT"
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
- name: configure git committer string
run: |-
git config --global user.name '${{ steps.app-token.outputs.app-slug }}[bot]'
git config --global user.email '${{ steps.get-user-id.outputs.user-id }}+${{ steps.app-token.outputs.app-slug }}[bot]@users.noreply.github.com>'
- name: Sync Repository
run: ./scripts/sync-git-repos.sh
env:
UPSTREAM_REPO: "${{ matrix.mapping.src.url }}"
UPSTREAM_BRANCH: "${{ matrix.mapping.src.branch }}"
DOWNSTREAM_REPO: "${{ matrix.mapping.dest.url }}"
DOWNSTREAM_BRANCH: "${{ matrix.mapping.dest.branch }}"
IGNORE_FILES: "${{ join(matrix.mapping.ignore_files, ',') }}"
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
MERGE_ARGS: "--no-edit"
PUSH_ARGS: ""
FETCH_ARGS: ""
update-metadata:
needs: [sync]
if: always() # This ensures the job runs regardless of the sync job's success or failure.
runs-on: ubuntu-latest
env:
METADATA_FILE: ${{ needs.sync.outputs.metadata_file }}
steps:
- uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ secrets.APP_ID }}
private-key: ${{ secrets.PRIVATE_KEY }}
owner: red-hat-data-services
repositories: Gated-Auto-Merger
- name: Get GitHub App User ID
id: get-user-id
run: echo "user-id=$(gh api "/users/${{ steps.app-token.outputs.app-slug }}[bot]" --jq .id)" >> "$GITHUB_OUTPUT"
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
- name: configure git committer string
run: |-
git config --global user.name '${{ steps.app-token.outputs.app-slug }}'
git config --global user.email '${{ steps.get-user-id.outputs.user-id }}+${{ steps.app-token.outputs.app-slug }}[bot]@users.noreply.github.com>'
- uses: actions/checkout@v3
with:
repository: red-hat-data-services/Gated-Auto-Merger
token: ${{ steps.app-token.outputs.token }}
ref: metadata
path: gam
- name: Update Metadata
working-directory: gam
run: |
STATUS=${{ needs.sync.result }}
WORKFLOW_RUN_URL=${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}
yq e ".metadata.status = \"$STATUS\" | .metadata.auto_merge_url = \"$WORKFLOW_RUN_URL\"" -i ${{ env.METADATA_FILE }}
- name: Commit and push changes to metadata branch
working-directory: gam
run: |
COMPONENT=$(cat ${{ env.METADATA_FILE }} | yq '.config.name')
GAM_EXECUTION_ID=$(cat ${{ env.METADATA_FILE }} | yq '.metadata.execution_id')
git status
git add .
git commit -m "Updating metadata.yaml - ${COMPONENT}/${GAM_EXECUTION_ID}"
git push origin metadata