forked from buildkite/trigger-pipeline-action
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathentrypoint.sh
executable file
·84 lines (71 loc) · 2.25 KB
/
entrypoint.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
set -euo pipefail
if [[ -z "${BUILDKITE_API_ACCESS_TOKEN:-}" ]]; then
echo "You must set the BUILDKITE_API_ACCESS_TOKEN environment variable (e.g. BUILDKITE_API_ACCESS_TOKEN = \"xyz\")"
exit 1
fi
if [[ -z "${PIPELINE:-}" ]]; then
echo "You must set the PIPELINE environment variable (e.g. PIPELINE = \"my-org/my-pipeline\")"
exit 1
fi
ORG_SLUG=$(echo "${PIPELINE}" | cut -d'/' -f1)
PIPELINE_SLUG=$(echo "${PIPELINE}" | cut -d'/' -f2)
COMMIT="${COMMIT:-${GITHUB_SHA}}"
BRANCH="${BRANCH:-${GITHUB_REF#"refs/heads/"}}"
MESSAGE="${MESSAGE:-}"
NAME=$(jq -r ".pusher.name" "$GITHUB_EVENT_PATH")
EMAIL=$(jq -r ".pusher.email" "$GITHUB_EVENT_PATH")
# Use jq’s --arg properly escapes string values for us
JSON=$(
jq -c -n \
--arg COMMIT "$COMMIT" \
--arg BRANCH "$BRANCH" \
--arg MESSAGE "$MESSAGE" \
--arg NAME "$NAME" \
--arg EMAIL "$EMAIL" \
'{
"commit": $COMMIT,
"branch": $BRANCH,
"message": $MESSAGE,
"author": {
"name": $NAME,
"email": $EMAIL
}
}'
)
# Merge in the build environment variables, if they specified any
if [[ "${BUILD_ENV_VARS:-}" ]]; then
if ! JSON=$(echo "$JSON" | jq -c --argjson BUILD_ENV_VARS "$BUILD_ENV_VARS" '. + {env: $BUILD_ENV_VARS}'); then
echo ""
echo "Error: BUILD_ENV_VARS provided invalid JSON: $BUILD_ENV_VARS"
exit 1
fi
fi
RESPONSE=$(
curl \
--silent \
-X POST \
-H "Authorization: Bearer ${BUILDKITE_API_ACCESS_TOKEN}" \
"https://api.buildkite.com/v2/organizations/${ORG_SLUG}/pipelines/${PIPELINE_SLUG}/builds" \
-d "$JSON" | tr -d '\n'
)
if [[ $(jq .id <<< "$RESPONSE") == null ]]; then
echo -n "Buildkite API call failed: "
jq .message <<< "$RESPONSE"
exit 1
fi
echo ""
echo "Build created:"
URL=$(jq --raw-output ".web_url" <<< "$RESPONSE")
echo $URL
# Provide JSON and Web URL as outputs for downstream actions
# use environment variable $GITHUB_OUTPUT, or fall back to deprecated set-output command
# https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
if [[ -n "${GITHUB_OUTPUT}" ]]
then
echo "json=$RESPONSE" >> ${GITHUB_OUTPUT}
echo "url=$URL" >> ${GITHUB_OUTPUT}
else
echo "::set-output name=json::$RESPONSE"
echo "::set-output name=url::$URL"
fi