set-commit-status.sh 1.16 KB
Newer Older
1
2
3
4
5
6
#!/bin/bash
#
# [description]
#     Set a status with a given name to the specified commit.
#
# [usage]
7
#     set-commit-status.sh <NAME> <STATUS> <SHA>
8
9
10
11
12
13
14
15
16
#
# NAME: Name of status.
#       Status with existing name overwrites a previous one.
#
# STATUS: Status to be set.
#         Can be "error", "failure", "pending" or "success".
#
# SHA: SHA of a commit to set a status on.

17
set -e -E -u -o pipefail
18
19
20

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

if [ $# -ne 3 ]; then
  echo "Usage: $0 <NAME> <STATUS> <SHA>"
26
  exit 1
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
fi

name=$1

status=$2
status=${status/error/failure}
status=${status/cancelled/failure}
status=${status/timed_out/failure}
status=${status/in_progress/pending}
status=${status/queued/pending}

sha=$3

data=$(
  jq -n \
42
    --arg state "${status}" \
43
    --arg url "${GITHUB_SERVER_URL}/microsoft/LightGBM/actions/runs/${GITHUB_RUN_ID}" \
44
    --arg name "${name}" \
45
46
47
48
49
50
51
52
53
    '{"state":$state,"target_url":$url,"context":$name}'
)

curl -sL \
  -X POST \
  -H "Accept: application/vnd.github.v3+json" \
  -H "Authorization: token $SECRETS_WORKFLOW" \
  -d "$data" \
  "${GITHUB_API_URL}/repos/microsoft/LightGBM/statuses/$sha"