|
| 1 | +name: "Await Workflow" |
| 2 | +description: "Waits until a workflow run completes." |
| 3 | + |
| 4 | +inputs: |
| 5 | + run-id: |
| 6 | + description: "The id of the workflow run to wait for." |
| 7 | + required: true |
| 8 | + poll-interval: |
| 9 | + description: "The interval (in seconds) to poll for the workflow run status." |
| 10 | + required: false |
| 11 | + default: "60" |
| 12 | + commit-status: |
| 13 | + description: "The commit status message. Leave empty to not create a commit status." |
| 14 | + required: false |
| 15 | + |
| 16 | +outputs: |
| 17 | + succeeded: |
| 18 | + description: "Whether the triggered run succeeded." |
| 19 | + value: ${{ steps.wait-for-workflow.outputs.RUN_SUCCEEDED }} |
| 20 | + conclusion: |
| 21 | + description: "The conclusion of the triggered workflow run." |
| 22 | + value: ${{ steps.wait-for-workflow.outputs.CONCLUSION }} |
| 23 | + |
| 24 | +runs: |
| 25 | + using: composite |
| 26 | + steps: |
| 27 | + - name: Print Action Input |
| 28 | + run: | |
| 29 | + echo "[DEBUG] Starting 'Await Workflow' Action; inputs = ${{ toJson(inputs) }}" |
| 30 | + shell: bash |
| 31 | + |
| 32 | + - name: View Run |
| 33 | + if: ${{ inputs.commit-status != '' }} |
| 34 | + id: view-run |
| 35 | + env: |
| 36 | + GH_TOKEN: ${{ github.token }} |
| 37 | + run: | |
| 38 | + JSON=$(gh run view ${{ inputs.run-id }} --json url,headSha) |
| 39 | + echo "URL=$(echo $JSON | jq -r '.url')" >> $GITHUB_OUTPUT |
| 40 | + echo "HEAD_SHA=$(echo $JSON | jq -r '.headSha')" >> $GITHUB_OUTPUT |
| 41 | + shell: bash |
| 42 | + |
| 43 | + - name: Create Commit Status |
| 44 | + if: ${{ inputs.commit-status != '' }} |
| 45 | + uses: actions/github-script@v7 |
| 46 | + with: |
| 47 | + script: | |
| 48 | + github.rest.repos.createCommitStatus({ |
| 49 | + owner: context.repo.owner, |
| 50 | + repo: context.repo.repo, |
| 51 | + sha: '${{ steps.view-run.outputs.HEAD_SHA }}', |
| 52 | + state: 'pending', |
| 53 | + target_url: '${{ steps.view-run.outputs.URL }}', |
| 54 | + context: '${{ inputs.commit-status }}' |
| 55 | + }) |
| 56 | +
|
| 57 | + - name: Wait for Workflow to Complete |
| 58 | + id: wait-for-workflow |
| 59 | + env: |
| 60 | + GH_TOKEN: ${{ github.token }} |
| 61 | + run: | |
| 62 | + echo "[DEBUG] Waiting for run '${{ inputs.run-id }}' to complete..." |
| 63 | + gh run watch ${{ inputs.run-id }} --interval ${{ inputs.poll-interval }} > /dev/null |
| 64 | + CONCLUSION=$(gh run view ${{ inputs.run-id }} --json conclusion | jq -r '.conclusion') |
| 65 | + |
| 66 | + echo "CONCLUSION=$CONCLUSION" >> $GITHUB_OUTPUT |
| 67 | + echo "[DEBUG] Run '${{ inputs.run-id }}' finished with conclusion '$CONCLUSION'." |
| 68 | + |
| 69 | + if [[ "$CONCLUSION" != "success" ]]; then |
| 70 | + echo "RUN_SUCCEEDED=false" >> $GITHUB_OUTPUT |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + |
| 74 | + echo "RUN_SUCCEEDED=true" >> $GITHUB_OUTPUT |
| 75 | + shell: bash |
| 76 | + |
| 77 | + - name: Determine Final Commit Status |
| 78 | + id: determine-final-commit-status |
| 79 | + if: ${{ always() && inputs.commit-status != '' }} |
| 80 | + run: | |
| 81 | + if [[ "${{ steps.wait-for-workflow.outputs.CONCLUSION }}" == "success" ]]; then |
| 82 | + echo "FINAL_COMMIT_STATUS=success" >> $GITHUB_OUTPUT |
| 83 | + else |
| 84 | + echo "FINAL_COMMIT_STATUS=failure" >> $GITHUB_OUTPUT |
| 85 | + fi |
| 86 | + shell: bash |
| 87 | + |
| 88 | + - name: Update Commit Status |
| 89 | + if: ${{ always() && inputs.commit-status != '' }} |
| 90 | + uses: actions/github-script@v7 |
| 91 | + with: |
| 92 | + script: | |
| 93 | + github.rest.repos.createCommitStatus({ |
| 94 | + owner: context.repo.owner, |
| 95 | + repo: context.repo.repo, |
| 96 | + sha: '${{ steps.view-run.outputs.HEAD_SHA }}', |
| 97 | + state: '${{ steps.determine-final-commit-status.outputs.FINAL_COMMIT_STATUS }}', |
| 98 | + target_url: '${{ steps.view-run.outputs.URL }}', |
| 99 | + context: '${{ inputs.commit-status }}' |
| 100 | + }) |
0 commit comments