Skip to content

Execute privileged instructions #2

Execute privileged instructions

Execute privileged instructions #2

name: Execute privileged instructions
env:
GH_API_ACTIONS: https://api.github.com/repos/${{ github.repository }}/actions
on:
workflow_run:
workflows: ["Foo"]
types:
- completed
jobs:
execute-instruction:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Download instruction artifact
run: |
all_artifacts=$(curl -sSL "$GH_API_ACTIONS/runs/${{ github.event.workflow_run.id }}/artifacts")
forward_artifact=$(echo $all_artifacts | jq '.artifacts[] | select(.workflow_run.id == ${{ github.event.workflow_run.id }} and .name == "forwarded_instructions")')
id=$(echo $forward_artifact | jq -r '.id')
curl -sSLO -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "$GH_API_ACTIONS/artifacts/$id/zip" -D headers.txt
- name: Unzip artifact
run: |
unzip -q zip -d forward || (cat zip && cat headers.txt)
- name: Retreive instruction contents
id: instruction
run: |
instruction=$(jq -r '.instruction' forward/instruction.json)
echo "instruction=$instruction" | tee -a $GITHUB_OUTPUT
- name: Delete the label
if: ${{ steps.instruction.outputs.instruction == 'delete_label' }}
run: |
ENDPOINT=$(jq -r '.endpoint' forward/instruction.json)
LABEL_NAME=$(cat .github/workflows/fuzzy-ci-helpers/label_name.txt)
curl -sL -w "%{http_code}" -o output.txt -X DELETE -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "$ENDPOINT/$LABEL_NAME"
- name: Comment on PR
if: ${{ steps.instruction.outputs.instruction == 'comment' }}
run: |
export ARTIFACTS_URL=$(jq -r '.artifacts_url' forward/instruction.json)
export HASH=$(jq -r '.hash' forward/instruction.json)
msg=$(cat .github/workflows/fuzzy-ci-helpers/msg.txt | tr '\n' ' ' | tr '|' '\n' | envsubst)
jq -n --arg msg "$msg" '{ body: $msg }' | tee -a body.json
ENDPOINT=$(jq -r '.endpoint' forward/instruction.json)
curl -LsX POST -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" -d @body.json "$ENDPOINT"