Skip to content

Commit c220f3e

Browse files
committed
Add support for pre-start hook script
1 parent e6deb56 commit c220f3e

2 files changed

Lines changed: 37 additions & 11 deletions

File tree

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ Environment variables:
2727
MR_GITHUB_API_BASEURL=https://api.github.com
2828
MR_RELEASE_URL=<latest on github.com/actions/runner/releases>
2929
MR_USER_BASE=<default in /etc/default/useradd>
30-
MR_GITHUB_PAT=***
30+
MR_USER_PREFIX=runner-
31+
MR_GITHUB_PAT=ghp_***
3132
3233
Sub-commands:
3334
add Add one self-hosted runner on this host
@@ -185,3 +186,24 @@ TZ=Asia/Shanghai
185186
PATH=$PATH:/mybin
186187
all_proxy=socks5h://localhost:1080
187188
```
189+
190+
### Inject hook script before starting the runner service
191+
192+
This application also supports to inject a hook script before starting the [runner service](https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/configuring-the-self-hosted-runner-application-as-a-service) via `MR_CMD_SVC_PRE_START` environment variable.
193+
194+
For example, you can limit the CPU and RAM usage of the runner service by modifying the unit file of the runner service. Add the following lines to the `.env` file under the directory of this application. Then this script will be executed just before starting the runner service - `./svc.sh start`.
195+
196+
```bash
197+
MR_CMD_SVC_PRE_START="$(cat <<-'__HEREDOC__'
198+
echo "🚀 Running custom commands before starting runner service."
199+
name="$(<mr.d/name)"
200+
echo "🚀 Runner: $name | User: $USER | Dir: $PWD"
201+
source <( sed -n "1,$(grep -n '^UNIT_PATH=' svc.sh | cut -d: -f1)p" svc.sh )
202+
echo "🚀 UNIT_PATH: $UNIT_PATH"
203+
sudo sed -i -e '/^\[Service\]/a CPUQuota=50%' -e '/^\[Service\]/a MemoryMax=512M' "$UNIT_PATH"
204+
sudo systemctl daemon-reload
205+
echo "🚀 Updated unit file:"
206+
cat "$UNIT_PATH"
207+
__HEREDOC__
208+
)"
209+
```

mr.bash

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,22 @@ declare -rg DIR_THIS FILE_THIS
1616
[[ -r "$DIR_THIS/.env" ]] && source "$DIR_THIS/.env"
1717

1818
# environment variables for customization
19-
# Github personal access token
20-
declare -rg MR_GITHUB_PAT
21-
# download URL of actions runner release, defaults to latest release on GitHub.com
22-
declare -rg MR_RELEASE_URL
23-
# baseurl of GitHub API, defaults to https://api.github.com
24-
declare -rg MR_GITHUB_API_BASEURL="${MR_GITHUB_API_BASEURL:-https://api.github.com}"
19+
# URL of this application
20+
declare -rg MR_URL='https://github.com/vbem/multi-runners'
2521
# baseurl of GitHub service, defaults to https://github.com
2622
declare -rg MR_GITHUB_BASEURL="${MR_GITHUB_BASEURL:-https://github.com}"
27-
# runners' local username prefix, defaults to `runner-`
28-
declare -rg MR_USER_PREFIX="${MR_USER_PREFIX:-runner-}"
23+
# baseurl of GitHub API, defaults to https://api.github.com
24+
declare -rg MR_GITHUB_API_BASEURL="${MR_GITHUB_API_BASEURL:-https://api.github.com}"
25+
# download URL of actions runner release, defaults to latest release on GitHub.com
26+
declare -rg MR_RELEASE_URL
2927
# runners' local users base directory, overrides the `HOME` setting in `/etc/default/useradd`
3028
declare -rg MR_USER_BASE
31-
# URL of this application
32-
declare -rg MR_URL='https://github.com/vbem/multi-runners'
29+
# runners' local username prefix, defaults to `runner-`
30+
declare -rg MR_USER_PREFIX="${MR_USER_PREFIX:-runner-}"
31+
# custom commands to run before starting the runner service
32+
declare -rg MR_CMD_SVC_PRE_START
33+
# Github personal access token
34+
declare -rg MR_GITHUB_PAT
3335

3436
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
3537
# stdlib
@@ -305,6 +307,7 @@ function mr::addRunner {
305307
if [[ "$(getenforce 2>/dev/null)" == "Enforcing" ]]; then
306308
chcon -t bin_t ./runsvc.sh # https://github.com/vbem/multi-runners/issues/9
307309
fi
310+
$MR_CMD_SVC_PRE_START # https://github.com/vbem/multi-runners/issues/23
308311
sudo ./svc.sh start
309312
__
310313
log::failed $? "Failed installing runner $i in local user '$user' for $url!" || return $?
@@ -393,6 +396,7 @@ Environment variables:
393396
MR_GITHUB_API_BASEURL=$MR_GITHUB_API_BASEURL
394397
MR_RELEASE_URL=${MR_RELEASE_URL:-<latest on github.com/actions/runner/releases>}
395398
MR_USER_BASE=${MR_USER_BASE:-<default in /etc/default/useradd>}
399+
MR_USER_PREFIX=${MR_USER_PREFIX}
396400
MR_GITHUB_PAT=${MR_GITHUB_PAT::11}${MR_GITHUB_PAT:+***}
397401
398402
Sub-commands:

0 commit comments

Comments
 (0)