This repository has been archived by the owner on Jan 30, 2025. It is now read-only.
forked from buildkite/trigger-pipeline-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·71 lines (59 loc) · 1.85 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
#!/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" "${CUSTOM_EVENT_PATH:-${GITHUB_EVENT_PATH}}")
EMAIL=$(jq -r ".pusher.email" "${CUSTOM_EVENT_PATH:-${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 \
--fail \
-X POST \
-H "Authorization: Bearer ${BUILDKITE_API_ACCESS_TOKEN}" \
"https://api.buildkite.com/v2/organizations/${ORG_SLUG}/pipelines/${PIPELINE_SLUG}/builds" \
-d "$JSON"
)
echo ""
echo "Build created:"
echo "$RESPONSE" | jq --raw-output ".web_url"
# Save output for downstream actions
echo "${RESPONSE}" > "${HOME}/${GITHUB_ACTION}.json"
echo ""
echo "Saved build JSON to:"
echo "${HOME}/${GITHUB_ACTION}.json"