rerun-workflow.sh 1.44 KB
Newer Older
1
2
3
4
5
6
#!/bin/bash
#
# [description]
#     Rerun specified workflow for given pull request.
#
# [usage]
7
#     rerun-workflow.sh <WORKFLOW_ID> <PR_NUMBER> <PR_BRANCH>
8
9
10
11
12
13
14
#
# WORKFLOW_ID: Identifier (config name of ID) of a workflow to be rerun.
#
# PR_NUMBER: Number of pull request for which workflow should be rerun.
#
# PR_BRANCH: Name of pull request's branch.

15
set -e -E -u -o pipefail
16
17
18

if [ -z "$GITHUB_ACTIONS" ]; then
  echo "Must be run inside GitHub Actions CI"
19
  exit 1
20
21
22
23
fi

if [ $# -ne 3 ]; then
  echo "Usage: $0 <WORKFLOW_ID> <PR_NUMBER> <PR_BRANCH>"
24
  exit 1
25
26
27
28
29
30
31
32
33
fi

workflow_id=$1
pr_number=$2
pr_branch=$3

runs=$(
  curl -sL \
    -H "Accept: application/vnd.github.v3+json" \
34
    -H "Authorization: token ${GITHUB_TOKEN}" \
35
36
37
    "${GITHUB_API_URL}/repos/microsoft/LightGBM/actions/workflows/${workflow_id}/runs?event=pull_request&branch=${pr_branch}" | \
  jq '.workflow_runs'
)
38
39
runs=$(echo "${runs}" | jq --arg pr_number "${pr_number}" --arg pr_branch "${pr_branch}" 'map(select(.event == "pull_request" and ((.pull_requests | length) != 0 and (.pull_requests[0].number | tostring) == $pr_number or .head_branch == $pr_branch)))')
runs=$(echo "${runs}" | jq 'sort_by(.run_number) | reverse')
40

41
if [[ $(echo "${runs}" | jq 'length') -gt 0 ]]; then
42
43
44
  curl -sL \
    -X POST \
    -H "Accept: application/vnd.github.v3+json" \
45
    -H "Authorization: token ${GITHUB_TOKEN}" \
46
    "${GITHUB_API_URL}/repos/microsoft/LightGBM/actions/runs/$(echo "${runs}" | jq '.[0].id')/rerun"
47
fi