Skip to content

Commit f09f909

Browse files
authored
Get approver for production (#21)
1 parent 0b5f24a commit f09f909

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

.github/workflows/build-deploy-backend.yml

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,11 @@ jobs:
128128
needs: deploy-stage
129129
runs-on: ubuntu-latest
130130
steps:
131-
# - name: Get workflow run ID
132-
# id: get-run-id
133-
# run: echo "workflow_run_id=${{ github.run_id }}" >> $GITHUB_ENV
134-
135131
- name: Get approval actor from audit log
136-
id: get-approver
137132
env:
138133
GH_TOKEN: ${{ secrets.READ_AUDIT_LOG }}
139134
OWNER: kosli-dev
140135
REPO: github-release-example
141-
# WORKFLOW_RUN_ID: ${{ github.run_id }}
142136
run: |
143137
API_URL="https://api.github.com/orgs/${OWNER}/audit-log"
144138
MAX_PAGES=5
@@ -172,7 +166,7 @@ jobs:
172166
fi
173167
174168
echo "approver=$APPROVER" >> $GITHUB_OUTPUT
175-
echo "### Approval Actor: $APPROVER" >> $GITHUB_STEP_SUMMARY
169+
echo "### Approval Actor for stage: $APPROVER" >> $GITHUB_STEP_SUMMARY
176170
177171
semver-tag:
178172
needs: [build,deploy-stage]
@@ -241,3 +235,47 @@ jobs:
241235
version: ${{ needs.build.outputs.image-version }}
242236
resource: prod-backend
243237
secrets: inherit
238+
239+
get-approver-for-production:
240+
needs: deploy-production
241+
runs-on: ubuntu-latest
242+
steps:
243+
- name: Get approval actor from audit log
244+
env:
245+
GH_TOKEN: ${{ secrets.READ_AUDIT_LOG }}
246+
OWNER: kosli-dev
247+
REPO: github-release-example
248+
run: |
249+
API_URL="https://api.github.com/orgs/${OWNER}/audit-log"
250+
MAX_PAGES=5
251+
PER_PAGE=5
252+
APPROVER=""
253+
254+
for PAGE in $(seq 1 $MAX_PAGES); do
255+
echo "Checking page $PAGE..."
256+
curl -s -H "Authorization: Bearer ${GH_TOKEN}" \
257+
-H "Accept: application/vnd.github+json" \
258+
--get \
259+
--data-urlencode "phrase=repo:${OWNER}/${REPO}" \
260+
--data-urlencode "phrase=action:workflows.approve_workflow_job" \
261+
--data-urlencode "per_page=${PER_PAGE}" \
262+
--data-urlencode "page=${PAGE}" \
263+
"$API_URL" > audit.json
264+
265+
MATCH=$(jq -r --arg run_id ${{ github.run_id }} '
266+
.[] | select(.workflow_run_id == ($run_id | tonumber)) | .actor' audit.json)
267+
268+
if [[ -n "$MATCH" ]]; then
269+
echo "Found matching approval by: $MATCH"
270+
APPROVER="$MATCH"
271+
break
272+
fi
273+
done
274+
275+
if [[ -z "$APPROVER" ]]; then
276+
echo "No approval found for workflow_run_id: $WORKFLOW_RUN_ID"
277+
exit 1
278+
fi
279+
280+
echo "approver=$APPROVER" >> $GITHUB_OUTPUT
281+
echo "### Approval Actor for production: $APPROVER" >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)