Skip to content

PostSync

PostSync #430

name: Repository Dispatch
on:
repository_dispatch:
types:
- PreSync
- PostSync
- SyncFail
permissions:
deployments: write
jobs:
update-status:
runs-on: ubuntu-latest
steps:
- name: Find deployment ID
id: find-deployment
run: |
# Use GitHub API to find the deployment ID
DEPLOYMENT_ID=$(curl -s -H "Authorization: Bearer ${{ github.token }}" "https://api.github.com/repos/${{ github.repository }}/deployments" | jq -r '.[] | select(.ref == "${{ github.event.client_payload.ref }}") | .id' | head -n 1)
# Output the deployment ID as an output variable
echo "::set-output name=DEPLOYMENT_ID::$DEPLOYMENT_ID"
- name: Check if deployment ID was found
id: check-deployment
run: |
if [ -z "${{ steps.find-deployment.outputs.DEPLOYMENT_ID }}" ]; then
echo "No deployment found for ref: ${{ github.event.client_payload.ref }}"
# TODO exit(1) when there should always be a deployment is argo is doing stuff, eg the preview label is set (and removed) automatically
echo "::set-output name=DEPLOYMENT_FOUND::false"
else
echo "::set-output name=DEPLOYMENT_FOUND::true"
fi
- name: Update deployment status
if: ${{ steps.check-deployment.outputs.DEPLOYMENT_FOUND == 'true' }}
run: |
DEPLOYMENT_ID=${{ steps.find-deployment.outputs.DEPLOYMENT_ID }}
case "${{ github.event.action }}" in
PreSync)
STATUS="in_progress"
DESCRIPTION="Deployment is in progress (PreSync)"
;;
PostSync)
STATUS="success"
DESCRIPTION="Deployment succeeded (PostSync)"
;;
SyncFail)
STATUS="failure"
DESCRIPTION="Deployment failed (SyncFail)"
;;
*)
echo "Unsupported event type: ${{ github.event.action }}"
exit 1
;;
esac
# Use the DEPLOYMENT_ID to update the deployment status
curl -X POST "https://api.github.com/repos/${{ github.repository }}/deployments/$DEPLOYMENT_ID/statuses" \
-H "Authorization: Bearer ${{ github.token }}" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-d '{
"state": "'"$STATUS"'",
"description": "'"$DESCRIPTION"'",
"log_url": "${{ github.event.client_payload.log_url }}",
"environment_url": "${{ github.event.client_payload.environment_url }}"
}'