Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add job rerun action #27210

Merged
merged 6 commits into from
Jun 26, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions .github/actions/rerun-job-action/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
name: "Rerun Job Action"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will need the Apache header (can copy/paste from any workflow).

Also, could you add a brief comment description of the purpose of this composite action, including how to use it (ideally with a code snippet) and required permissions (again ideally with a code snippet). I think the permissions should be repo: read (even if we don't need this, virtually every job will), pull-requests: write, and checks: read (might actually need to be statuses instead of checks?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot the header, Will add it.

Regarding permissions i was thinking to add that and explain in some doc showing implementation of the action. Or i can do it here. Put README.MD here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine leaving it for the doc. When we add that, we should add a link from here. I'd still like to have a basic 1 sentence summary of this action in the file though.

description: Re-runs a job that is attached to the PR as Check Run
inputs:
pull_request_url:
description: "The URL of the PR"
required: true
github_repository:
description: "The GitHub repository"
required: true
github_token:
description: "The GitHub token"
required: true
github_job:
description: "The GitHub job"
required: true
github_current_run_id:
description: "The GitHub current run id. Not the same that is fetched in this action"
required: true


runs:
using: composite
steps:
- name: Get Last Commit SHA
shell: bash
run: |
URL=${{inputs.pull_request_url}}/commits
PRSHA=$(curl \
-H 'Authorization: Bearer ${{inputs.github_token}}' \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-s $URL | jq -r '.[-1].sha' )
echo prsha=$PRSHA >> $GITHUB_ENV

- name: Get Status for PR Job
id: get_status
shell: bash
run: |
URL="${{github.api_url}}/repos/${{inputs.github_repository}}/commits/${{env.prsha}}/check-runs"
STATUS=$(curl \
-H 'Authorization: Bearer ${{inputs.github_token}}' \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-s $URL | jq -r '.check_runs | .[] | select(.name=="${{inputs.github_job}}") | .status')
echo status=$STATUS >> $GITHUB_ENV

- name: Get Conclusion for PR Job
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we consolidate this with the last one since we're making the same request twice and just pulling out a different field?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover from PoC, will consolidate it

id: get_conclusion
shell: bash
run: |
URL="${{github.api_url}}/repos/${{inputs.github_repository}}/commits/${{env.prsha}}/check-runs"
CONCLUSION=$(curl \
-H 'Authorization: Bearer ${{inputs.github_token}}' \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-s $URL | jq -r '.check_runs | .[] | select(.name=="${{inputs.github_job}}") | .conclusion')
echo conclusion=$CONCLUSION >> $GITHUB_ENV

- name: Enable Rerun
if: ${{env.status == 'completed' && (env.conclusion == 'success' || env.conclusion == 'skipped')}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment explaining our logic here would probably help (basically, we can't use the rerun api if the status is completed and it succeeded or was skipped).

I guess this does mean that trigger phrases won't status correctly for jobs that have already succeeded or are skipped, I think that's fine though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Succeeded or Skipped will not be re-run , but a new instance of Job will appear in actions.

Ill leave a comment

shell: bash
run: echo rerun=false >> $GITHUB_ENV

- name: Get Check Suide ID
volatilemolotov marked this conversation as resolved.
Show resolved Hide resolved
if: ${{env.rerun != 'false' }}
shell: bash
run: |
URL="${{github.api_url}}/repos/${{inputs.github_repository}}/commits/${{env.prsha}}/check-runs"
CHECK_SUITE_ID=$(curl \
-H 'Authorization: Bearer ${{inputs.github_token}}' \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-s $URL | jq -r '.check_runs | .[] | select(.name=="${{inputs.github_job}}") | .check_suite.id')
echo check_suite_id=$CHECK_SUITE_ID >> $GITHUB_ENV

- name: Get Run ID
if: ${{env.rerun != 'false' }}
shell: bash
run: |
URL="${{github.api_url}}/repos/${{inputs.github_repository}}/actions/runs?check_suite_id=${{env.check_suite_id}}"
RUN_ID=$(curl \
-H 'Authorization: Bearer ${{inputs.github_token}}' \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-s $URL | jq -r '.workflow_runs | .[0] | .id')
echo run_id=$RUN_ID >> $GITHUB_ENV

- name: Get Job ID
if: ${{env.rerun != 'false' }}
shell: bash
run: |
URL="${{github.api_url}}/repos/${{inputs.github_repository}}/actions/runs/${{env.run_id}}/jobs"
JOB_ID=$(curl \
-H 'Authorization: Bearer ${{inputs.github_token}}' \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-s $URL | jq -r '.jobs | .[] | select(.name=="${{inputs.github_job}}") | .id ')
echo job_id=$JOB_ID >> $GITHUB_ENV

- name: Trigger Re-run
if: ${{env.rerun != 'false' }}
shell: bash
run: |
URL="${{github.api_url}}/repos/${{inputs.github_repository}}/actions/jobs/${{env.job_id}}/rerun"
curl -X POST \
-H 'Authorization: Bearer ${{inputs.github_token}}' \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-s $URL

- name: Exit rest of the run
if: ${{env.rerun != 'false' }}
shell: bash
run: |
gh run cancel ${{ inputs.github_current_run_id }}
gh run watch ${{ inputs.github_current_run_i }}
env:
GITHUB_TOKEN: ${{ inputs.github_token }}