-
Notifications
You must be signed in to change notification settings - Fork 2
/
trigger-gh-actions.sh
executable file
·44 lines (36 loc) · 1.45 KB
/
trigger-gh-actions.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/sh -f
# Trigger GitHub Actions workflow.
# The trigger-gh-actions.sh script provides a programmatic
# way to trigger a new run.
# Usage:
# trigger-gh-actions.sh GITHUBID GITHUBPROJECT GITHUB_USERNAME GITHUB_ACCESS_TOKEN [EVENT_TYPE] [CLIENT_PAYLOAD]
# For example:
# trigger-gh-actions.sh metanorma metanorma-cli CAMOBAP795 xxxxx build_master
#
# where GITHUB_ACCESS_TOKEN is personal access token that you created (it needs repo level access).
if [ "$#" -lt 4 ] || [ "$#" -gt 6 ]; then
if [ "$1" = "--help" ] ; then
echo "Example:"
else
echo "Wrong number of arguments $# to trigger-gh-actions.sh; run like:"
fi
echo " trigger-gh-actions.sh GITHUBID GITHUBPROJECT GITHUB_USERNAME GITHUB_ACCESS_TOKEN [EVENT_TYPE] [CLIENT_PAYLOAD]" >&2
exit 1
fi
GITHUBID=$1
REPO=$2
USER=$3
ACCESS_TOKEN=$4
EVENT_TYPE=${5:-build_application}
CLIENT_PAYLOAD=${6:-null}
# https://stackoverflow.com/questions/9733338/shell-script-remove-first-and-last-quote-from-a-variable
CLIENT_PAYLOAD="${CLIENT_PAYLOAD%\"}"
CLIENT_PAYLOAD="${CLIENT_PAYLOAD#\"}"
body="{ \"event_type\": \"${EVENT_TYPE}\", \"client_payload\": ${CLIENT_PAYLOAD} }"
echo "Trigger repository_dispatch for ${GITHUBID}/${REPO} with ${body} client_payload=${CLIENT_PAYLOAD}"
curl -s -X POST \
-u "${USER}:${ACCESS_TOKEN}" \
-H "Accept: application/vnd.github.everest-preview+json" \
-H "Content-Type: application/json" \
-d "$body" \
"https://api.github.com/repos/${GITHUBID}/${REPO}/dispatches"