Skip to content

Commit ed762d9

Browse files
add job rerun action (apache#27210)
* add job rerun action * Update .github/actions/rerun-job-action/action.yml Typo Co-authored-by: Danny McCormick <[email protected]> * 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 <[email protected]> --------- Co-authored-by: vdjerek <Vlado Djerek> Co-authored-by: Danny McCormick <[email protected]>
1 parent 76d5661 commit ed762d9

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
19+
#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
20+
#to check if the comment is present
21+
#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
22+
23+
name: "Rerun Job Action"
24+
description: Re-runs a job that is attached to the PR as Check Run
25+
inputs:
26+
pull_request_url:
27+
description: "The URL of the PR"
28+
required: true
29+
github_repository:
30+
description: "The GitHub repository"
31+
required: true
32+
github_token:
33+
description: "The GitHub token"
34+
required: true
35+
github_job:
36+
description: "The GitHub job"
37+
required: true
38+
github_current_run_id:
39+
description: "The GitHub current run id. Not the same that is fetched in this action"
40+
required: true
41+
42+
43+
runs:
44+
using: composite
45+
steps:
46+
- name: Get Last Commit SHA
47+
shell: bash
48+
run: |
49+
URL=${{inputs.pull_request_url}}/commits
50+
PRSHA=$(curl \
51+
-H 'Authorization: Bearer ${{inputs.github_token}}' \
52+
-H "Accept: application/vnd.github+json" \
53+
-H "X-GitHub-Api-Version: 2022-11-28" \
54+
-s $URL | jq -r '.[-1].sha' )
55+
echo prsha=$PRSHA >> $GITHUB_ENV
56+
- name: Get Status and Conclusion for PR Job
57+
shell: bash
58+
run: |
59+
URL="${{github.api_url}}/repos/${{inputs.github_repository}}/commits/${{env.prsha}}/check-runs"
60+
read -r STATUS CONCLUSION CHECK_SUITE_ID<<< $(curl \
61+
-H 'Authorization: Bearer ${{inputs.github_token}}' \
62+
-H "Accept: application/vnd.github+json" \
63+
-H "X-GitHub-Api-Version: 2022-11-28" \
64+
-s $URL | jq -r '.check_runs | .[] | select(.name=="${{inputs.github_job}}") | "\(.status) \(.conclusion) \(.check_suite.id)"')
65+
echo status=$STATUS >> $GITHUB_ENV
66+
echo conclusion=$CONCLUSION >> $GITHUB_ENV
67+
echo check_suite_id=$CHECK_SUITE_ID >> $GITHUB_ENV
68+
69+
- name: Enable Rerun
70+
if: ${{env.status == 'completed' && (env.conclusion == 'success' || env.conclusion == 'skipped')}}
71+
shell: bash
72+
run: echo rerun=false >> $GITHUB_ENV
73+
74+
- name: Get Run ID
75+
if: ${{env.rerun != 'false' }}
76+
shell: bash
77+
run: |
78+
URL="${{github.api_url}}/repos/${{inputs.github_repository}}/actions/runs?check_suite_id=${{env.check_suite_id}}"
79+
RUN_ID=$(curl \
80+
-H 'Authorization: Bearer ${{inputs.github_token}}' \
81+
-H "Accept: application/vnd.github+json" \
82+
-H "X-GitHub-Api-Version: 2022-11-28" \
83+
-s $URL | jq -r '.workflow_runs | .[0] | .id')
84+
echo run_id=$RUN_ID >> $GITHUB_ENV
85+
- name: Get Job ID
86+
if: ${{env.rerun != 'false' }}
87+
shell: bash
88+
run: |
89+
URL="${{github.api_url}}/repos/${{inputs.github_repository}}/actions/runs/${{env.run_id}}/jobs"
90+
JOB_ID=$(curl \
91+
-H 'Authorization: Bearer ${{inputs.github_token}}' \
92+
-H "Accept: application/vnd.github+json" \
93+
-H "X-GitHub-Api-Version: 2022-11-28" \
94+
-s $URL | jq -r '.jobs | .[] | select(.name=="${{inputs.github_job}}") | .id ')
95+
echo job_id=$JOB_ID >> $GITHUB_ENV
96+
- name: Trigger Re-run
97+
if: ${{env.rerun != 'false' }}
98+
shell: bash
99+
run: |
100+
URL="${{github.api_url}}/repos/${{inputs.github_repository}}/actions/jobs/${{env.job_id}}/rerun"
101+
curl -X POST \
102+
-H 'Authorization: Bearer ${{inputs.github_token}}' \
103+
-H "Accept: application/vnd.github+json" \
104+
-H "X-GitHub-Api-Version: 2022-11-28" \
105+
-s $URL
106+
- name: Exit rest of the run
107+
if: ${{env.rerun != 'false' }}
108+
shell: bash
109+
run: |
110+
gh run cancel ${{ inputs.github_current_run_id }}
111+
gh run watch ${{ inputs.github_current_run_id }}
112+
env:
113+
GITHUB_TOKEN: ${{ inputs.github_token }}

0 commit comments

Comments
 (0)