Skip to content

Commit 856296f

Browse files
committed
Select scope in a single variable
The backwards compatibility with ORG_RUNNER is ensured. If RUNNER_SCOPE is present, it overrides what is imposed by ORG_RUNNER.
1 parent e4a4891 commit 856296f

File tree

3 files changed

+61
-42
lines changed

3 files changed

+61
-42
lines changed

README.md

+10-11
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ These containers are built via Github actions that [copy the dockerfile](https:/
3838
| `RUNNER_NAME` | The name of the runner to use. Supercedes (overrides) `RUNNER_NAME_PREFIX` |
3939
| `RUNNER_NAME_PREFIX` | A prefix for a randomly generated name (followed by a random 13 digit string). You must not also provide `RUNNER_NAME`. Defaults to `github-runner` |
4040
| `ACCESS_TOKEN` | A [github PAT](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) to use to generate `RUNNER_TOKEN` dynamically at container start. Not using this requires a valid `RUNNER_TOKEN` |
41-
| `ORG_RUNNER` | Only valid if using `ACCESS_TOKEN`. This will set the runner to an org runner. Default is 'false'. Valid values are 'true' or 'false'. If this is set to true you must also set `ORG_NAME` and makes `REPO_URL` unneccesary |
42-
| `ORG_NAME` | The organization name for the runner to register under. Requires `ORG_RUNNER` to be 'true'. No default value. |
43-
| `ENTERPRISE_RUNNER` | Only valid if using `ACCESS_TOKEN`. This will set the runner to an enterprise runner. Default is 'false'. Valid values are 'true' or 'false'. If this is set to true you must also set `ENTERPRISE_NAME` and makes `REPO_URL` unneccesary |
44-
| `ENTERPRISE_NAME` | The enterprise name for the runner to register under. Requires `ENTERPRISE_RUNNER` to be 'true'. No default value. |
41+
| `RUNNER_SCOPE` | The scope the runner will be registered on. Valid values are `repo`, `org` and `ent`. For 'org' and 'enterprise', `ACCESS_TOKEN` is required and `REPO_URL` is unneccesary. If 'org', requires `ORG_NAME`; if 'enterprise', requires `ENTERPRISE_NAME`. Default is 'repo'. |
42+
| `ORG_NAME` | The organization name for the runner to register under. Requires `RUNNER_SCOPE` to be 'org'. No default value. |
43+
| `ENTERPRISE_NAME` | The enterprise name for the runner to register under. Requires `RUNNER_SCOPE` to be 'enterprise'. No default value. |
4544
| `LABELS` | A comma separated string to indicate the labels. Default is 'default' |
4645
| `REPO_URL` | If using a non-organization runner this is the full repository url to register under such as 'https://github.com/myoung34/repo' |
4746
| `RUNNER_TOKEN` | If not using a PAT for `ACCESS_TOKEN` this will be the runner token provided by the Add Runner UI (a manual process). Note: This token is short lived and will change frequently. `ACCESS_TOKEN` is likely preferred. |
@@ -65,7 +64,7 @@ docker run -d --restart always --name github-runner \
6564
-e ACCESS_TOKEN="footoken" \
6665
-e RUNNER_WORKDIR="/tmp/github-runner-your-repo" \
6766
-e RUNNER_GROUP="my-group" \
68-
-e ORG_RUNNER="true" \
67+
-e RUNNER_SCOPE="org" \
6968
-e ORG_NAME="octokode" \
7069
-e LABELS="my-label,other-label" \
7170
-v /var/run/docker.sock:/var/run/docker.sock \
@@ -122,7 +121,7 @@ services:
122121
RUNNER_TOKEN: someGithubTokenHere
123122
RUNNER_WORKDIR: /tmp/runner/work
124123
RUNNER_GROUP: my-group
125-
ORG_RUNNER: 'false'
124+
RUNNER_SCOPE: 'repo'
126125
LABELS: linux,x64,gpu
127126
security_opt:
128127
# needed on SELinux systems to allow docker container to manage other docker containers
@@ -150,7 +149,7 @@ job "github_runner" {
150149
RUNNER_NAME_PREFIX = "myrunner"
151150
RUNNER_WORKDIR = "/tmp/github-runner-your-repo"
152151
RUNNER_GROUP = "my-group"
153-
ORG_RUNNER = "true"
152+
RUNNER_SCOPE = "org"
154153
ORG_NAME = "octokode"
155154
LABELS = "my-label,other-label"
156155
}
@@ -201,8 +200,8 @@ spec:
201200
env:
202201
- name: ACCESS_TOKEN
203202
value: foo-access-token
204-
- name: ORG_RUNNER
205-
value: "true"
203+
- name: RUNNER_SCOPE
204+
value: "org"
206205
- name: ORG_NAME
207206
value: octokode
208207
- name: LABELS
@@ -256,7 +255,7 @@ docker run -d --restart always --name github-runner \
256255
-e RUNNER_NAME="foo-runner" \
257256
-e RUNNER_WORKDIR="/tmp/github-runner-your-repo" \
258257
-e RUNNER_GROUP="my-group" \
259-
-e ORG_RUNNER="true" \
258+
-e RUNNER_SCOPE="org" \
260259
-e ORG_NAME="octokode" \
261260
-e LABELS="my-label,other-label" \
262261
-v /var/run/docker.sock:/var/run/docker.sock \
@@ -287,7 +286,7 @@ docker run -d --restart always --name github-runner \
287286
-e RUNNER_NAME="foo-runner" \
288287
-e RUNNER_WORKDIR="/tmp/github-runner-your-repo" \
289288
-e RUNNER_GROUP="my-group" \
290-
-e ENTERPRISE_RUNNER="true" \
289+
-e RUNNER_SCOPE="enterprise" \
291290
-e ENTERPRISE_NAME="my-enterprise" \
292291
-e LABELS="my-label,other-label" \
293292
-v /var/run/docker.sock:/var/run/docker.sock \

entrypoint.sh

+29-6
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,42 @@ _RUNNER_NAME=${RUNNER_NAME:-${RUNNER_NAME_PREFIX:-github-runner}-$(head /dev/ura
1717
_RUNNER_WORKDIR=${RUNNER_WORKDIR:-/_work}
1818
_LABELS=${LABELS:-default}
1919
_RUNNER_GROUP=${RUNNER_GROUP:-Default}
20-
_SHORT_URL=${REPO_URL}
2120
_GITHUB_HOST=${GITHUB_HOST:="github.com"}
2221

23-
if [[ ${ORG_RUNNER} == "true" ]]; then
24-
_SHORT_URL="https://${_GITHUB_HOST}/${ORG_NAME}"
25-
elif [[ ${ENTERPRISE_RUNNER} == "true" ]]; then
26-
_SHORT_URL="https://${_GITHUB_HOST}/enterprises/${ENTERPRISE_NAME}"
22+
# ensure backwards compatibility
23+
if [[ -z $RUNNER_SCOPE ]]; then
24+
if [[ ${ORG_RUNNER} == "true" ]]; then
25+
export RUNNER_SCOPE="org"
26+
else
27+
export RUNNER_SCOPE="repo"
28+
fi
2729
fi
2830

31+
RUNNER_SCOPE="${RUNNER_SCOPE,,}" # to lowercase
32+
33+
case ${RUNNER_SCOPE} in
34+
org*)
35+
[[ -z ${ORG_NAME} ]] && ( echo "ORG_NAME required for org runners"; exit 1 )
36+
_SHORT_URL="https://${_GITHUB_HOST}/${ORG_NAME}"
37+
RUNNER_SCOPE="org"
38+
;;
39+
40+
ent*)
41+
[[ -z ${ENTERPRISE_NAME} ]] && ( echo "ENTERPRISE_NAME required for enterprise runners"; exit 1 )
42+
_SHORT_URL="https://${_GITHUB_HOST}/enterprises/${ENTERPRISE_NAME}"
43+
RUNNER_SCOPE="enterprise"
44+
;;
45+
46+
*)
47+
[[ -z ${REPO_URL} ]] && ( echo "REPO_URL required for repo runners"; exit 1 )
48+
_SHORT_URL=${REPO_URL}
49+
RUNNER_SCOPE="repo"
50+
;;
51+
esac
52+
2953
if [[ -n "${ACCESS_TOKEN}" ]]; then
3054
_TOKEN=$(bash /token.sh)
3155
RUNNER_TOKEN=$(echo "${_TOKEN}" | jq -r .token)
32-
_SHORT_URL=$(echo "${_TOKEN}" | jq -r .short_url)
3356
fi
3457

3558
echo "Configuring"

token.sh

+22-25
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,42 @@
11
#!/bin/bash
22

3-
_ORG_RUNNER=${ORG_RUNNER:-false}
4-
_ENTERPRISE_RUNNER=${ENTERPRISE_RUNNER:-false}
53
_GITHUB_HOST=${GITHUB_HOST:="github.com"}
64

75
# If URL is not github.com then use the enterprise api endpoint
86
if [[ ${GITHUB_HOST} = "github.com" ]]; then
9-
URI="https://api.${_GITHUB_HOST}"
7+
URI="https://api.${_GITHUB_HOST}"
108
else
11-
URI="https://${_GITHUB_HOST}/api/v3"
9+
URI="https://${_GITHUB_HOST}/api/v3"
1210
fi
1311

1412
API_VERSION=v3
1513
API_HEADER="Accept: application/vnd.github.${API_VERSION}+json"
1614
AUTH_HEADER="Authorization: token ${ACCESS_TOKEN}"
1715

18-
REPO_URL=${REPO_URL:-${URI}}
19-
_PROTO="$(echo "${REPO_URL}" | grep :// | sed -e's,^\(.*://\).*,\1,g')"
20-
# shellcheck disable=SC2116
21-
_URL="$(echo "${REPO_URL/${_PROTO}/}")"
22-
_PATH="$(echo "${_URL}" | grep / | cut -d/ -f2-)"
23-
_ACCOUNT="$(echo "${_PATH}" | cut -d/ -f1)"
24-
_REPO="$(echo "${_PATH}" | cut -d/ -f2)"
25-
26-
_FULL_URL="${URI}/repos/${_ACCOUNT}/${_REPO}/actions/runners/registration-token"
27-
if [[ ${_ORG_RUNNER} == "true" ]]; then
28-
[[ -z ${ORG_NAME} ]] && ( echo "ORG_NAME required for org runners"; exit 1 )
29-
_FULL_URL="${URI}/orgs/${ORG_NAME}/actions/runners/registration-token"
30-
_SHORT_URL="${_PROTO}${_GITHUB_HOST}/${ORG_NAME}"
31-
elif [[ ${_ENTERPRISE_RUNNER} == "true" ]]; then
32-
[[ -z ${ENTERPRISE_NAME} ]] && ( echo "ENTERPRISE_NAME required for enterprise runners"; exit 1 )
33-
_FULL_URL="${URI}/enterprises/${ENTERPRISE_NAME}/actions/runners/registration-token"
34-
_SHORT_URL="${_PROTO}${_GITHUB_HOST}/enterprises/${ENTERPRISE_NAME}"
35-
else
36-
_SHORT_URL=$REPO_URL
37-
fi
16+
case ${RUNNER_SCOPE} in
17+
org*)
18+
_FULL_URL="${URI}/orgs/${ORG_NAME}/actions/runners/registration-token"
19+
;;
20+
21+
ent*)
22+
_FULL_URL="${URI}/enterprises/${ENTERPRISE_NAME}/actions/runners/registration-token"
23+
;;
24+
25+
*)
26+
_PROTO="https://"
27+
# shellcheck disable=SC2116
28+
_URL="$(echo "${REPO_URL/${_PROTO}/}")"
29+
_PATH="$(echo "${_URL}" | grep / | cut -d/ -f2-)"
30+
_ACCOUNT="$(echo "${_PATH}" | cut -d/ -f1)"
31+
_REPO="$(echo "${_PATH}" | cut -d/ -f2)"
32+
_FULL_URL="${URI}/repos/${_ACCOUNT}/${_REPO}/actions/runners/registration-token"
33+
;;
34+
esac
3835

3936
RUNNER_TOKEN="$(curl -XPOST -fsSL \
4037
-H "${AUTH_HEADER}" \
4138
-H "${API_HEADER}" \
4239
"${_FULL_URL}" \
4340
| jq -r '.token')"
4441

45-
echo "{\"token\": \"${RUNNER_TOKEN}\", \"short_url\": \"${_SHORT_URL}\", \"full_url\": \"${_FULL_URL}\"}"
42+
echo "{\"token\": \"${RUNNER_TOKEN}\", \"full_url\": \"${_FULL_URL}\"}"

0 commit comments

Comments
 (0)