forked from myoung34/docker-github-actions-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.sh
More file actions
66 lines (55 loc) · 1.76 KB
/
token.sh
File metadata and controls
66 lines (55 loc) · 1.76 KB
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
#!/bin/bash
normalize_host() {
local host="${1#http://}"
host="${host#https://}"
echo "${host%%/}"
}
normalize_api_path() {
local path="${1:-}"
if [[ -z ${path} ]] || [[ ${path} == "/" ]]; then
echo ""
return
fi
path="/${path#/}"
echo "${path%/}"
}
_GITHUB_HOST=$(normalize_host "${GITHUB_HOST:="github.com"}")
if [[ -n ${GITHUB_API_HOST} ]]; then
_GITHUB_API_HOST=$(normalize_host "${GITHUB_API_HOST}")
_GITHUB_API_PATH=$(normalize_api_path "${GITHUB_API_PATH:-/api/v3}")
elif [[ ${_GITHUB_HOST} = "github.com" ]]; then
_GITHUB_API_HOST="api.${_GITHUB_HOST}"
_GITHUB_API_PATH=$(normalize_api_path "${GITHUB_API_PATH:-/}")
else
_GITHUB_API_HOST="${_GITHUB_HOST}"
_GITHUB_API_PATH=$(normalize_api_path "${GITHUB_API_PATH:-/api/v3}")
fi
URI="https://${_GITHUB_API_HOST}${_GITHUB_API_PATH}"
API_VERSION=v3
API_HEADER="Accept: application/vnd.github.${API_VERSION}+json"
AUTH_HEADER="Authorization: token ${ACCESS_TOKEN}"
CONTENT_LENGTH_HEADER="Content-Length: 0"
case ${RUNNER_SCOPE} in
org*)
_FULL_URL="${URI}/orgs/${ORG_NAME}/actions/runners/registration-token"
;;
ent*)
_FULL_URL="${URI}/enterprises/${ENTERPRISE_NAME}/actions/runners/registration-token"
;;
*)
_PROTO="https://"
# shellcheck disable=SC2116
_URL="$(echo "${REPO_URL/${_PROTO}/}")"
_PATH="$(echo "${_URL}" | grep / | cut -d/ -f2-)"
_ACCOUNT="$(echo "${_PATH}" | cut -d/ -f1)"
_REPO="$(echo "${_PATH}" | cut -d/ -f2)"
_FULL_URL="${URI}/repos/${_ACCOUNT}/${_REPO}/actions/runners/registration-token"
;;
esac
RUNNER_TOKEN="$(curl -XPOST -fsSL \
-H "${CONTENT_LENGTH_HEADER}" \
-H "${AUTH_HEADER}" \
-H "${API_HEADER}" \
"${_FULL_URL}" \
| jq -r '.token')"
echo "{\"token\": \"${RUNNER_TOKEN}\", \"full_url\": \"${_FULL_URL}\"}"