Skip to content

Commit

Permalink
add job rerun action (#27210)
Browse files Browse the repository at this point in the history
* add job rerun action

* Update .github/actions/rerun-job-action/action.yml

Typo

Co-authored-by: Danny McCormick <dannymccormick@google.com>

* header and a comment explaining the logic for going into re-run flow

* deduplicate API calls

* added some explanation within the action file

* Update .github/actions/rerun-job-action/action.yml

Typo

Co-authored-by: Danny McCormick <dannymccormick@google.com>

---------

Co-authored-by: vdjerek <Vlado Djerek>
Co-authored-by: Danny McCormick <dannymccormick@google.com>
  • Loading branch information
volatilemolotov and damccorm committed Jun 26, 2023
1 parent 76d5661 commit ed762d9
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions .github/actions/rerun-job-action/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.


#Action used to trigger a failed check re-run within a PR using a comment. Add this action to your workflow with an if condition
#to check if the comment is present
#If the check is failed this will trigger it again. If its not failed a new instance of workflow will run which will not show in the status box or checks tab in the PR and can be found in the actions tab https://github.com/apache/beam/actions

name: "Rerun Job Action"
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 and Conclusion for PR Job
shell: bash
run: |
URL="${{github.api_url}}/repos/${{inputs.github_repository}}/commits/${{env.prsha}}/check-runs"
read -r STATUS CONCLUSION 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}}") | "\(.status) \(.conclusion) \(.check_suite.id)"')
echo status=$STATUS >> $GITHUB_ENV
echo conclusion=$CONCLUSION >> $GITHUB_ENV
echo check_suite_id=$CHECK_SUITE_ID >> $GITHUB_ENV
- name: Enable Rerun
if: ${{env.status == 'completed' && (env.conclusion == 'success' || env.conclusion == 'skipped')}}
shell: bash
run: echo rerun=false >> $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_id }}
env:
GITHUB_TOKEN: ${{ inputs.github_token }}

0 comments on commit ed762d9

Please sign in to comment.