Skip to content

Commit 1884b7e

Browse files
authored
Feature/docker container (#3)
Allow the script to run in containers.
1 parent 2f12c23 commit 1884b7e

4 files changed

Lines changed: 104 additions & 24 deletions

File tree

.github/workflows/demo.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Demo
2+
on:
3+
push:
4+
branches: [ "feature/docker-container" ]
5+
workflow_dispatch:
6+
7+
jobs:
8+
demo:
9+
name: Demonstrate runner
10+
runs-on: runner@localhost.runner
11+
steps:
12+
- name: Debug
13+
run: |
14+
echo "Who am I: $(whoami)"
15+
echo "Hostname: $(hostname -f)"
16+
echo "Environment:"
17+
env

Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# This is for testing and development only.
2+
# SSHD must be installed before running the playbook.
3+
FROM geerlingguy/docker-${MOLECULE_DISTRO:-rockylinux8}-ansible:latest
4+
5+
RUN useradd runner
6+
RUN yum install -y \
7+
git \
8+
jq
9+
10+
# This codebase. The runner wrapper script.
11+
COPY ./ /github-runner
12+
WORKDIR /github-runner
13+
RUN chown runner:runner . -R
14+
15+
# GitHub Runner code.
16+
# Install runner to a path that won't ever be in a volume.
17+
ENV RUNNER_PATH /usr/share/github-runner
18+
# We are installing as root then switching back because we need to use the install-dependencies script.
19+
RUN ./github-runner --no-run --no-config --runner-path=${RUNNER_PATH}
20+
RUN ${RUNNER_PATH}/bin/installdependencies.sh
21+
RUN chown runner:runner ${RUNNER_PATH} -R
22+
23+
USER runner

docker-compose.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# For testing and development.
2+
3+
---
4+
services:
5+
runner:
6+
# image: geerlingguy/docker-${MOLECULE_DISTRO:-rockylinux8}-ansible:latest
7+
image: jonpugh/github-runner
8+
hostname: ${DOCKER_HOSTNAME:-localhost.runner}
9+
volumes:
10+
- ./:/github-runner
11+
environment:
12+
GITHUB_TOKEN: ${GITHUB_TOKEN}
13+
GITHUB_REPOSITORY: jonpugh/github-runner
14+
entrypoint: /github-runner/github-runner
15+
build: ./

github-runner

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,21 @@ start() {
2222
say Launching runner with run.sh ...
2323
line
2424
cd $RUNNER_FOLDER
25-
./run.sh
25+
26+
# Run.sh in a new process.
27+
./run.sh &
28+
RUNNER_PID=$!
29+
say "Run.sh PID: $RUNNER_PID"
30+
wait $RUNNER_PID
31+
2632
else
2733
say Launching runner with run.sh ... SKIPPED.
2834
say Exiting github-runner.
2935
fi
3036

3137
}
3238
welcome() {
33-
say "GitHub Runner | ${RUNNER_SCRIPT_PATH}"
39+
say "GitHub Runner"
3440
say Welcome! This script will:
3541
say 1. Install Runner
3642
say 2. Configure Runner
@@ -67,11 +73,17 @@ verify() {
6773
RUNNER_SCRIPT_ENV_FILE_STATUS="(Not found)"
6874
fi
6975

70-
# Detect composer usage.
76+
# Detect path to this script.
77+
# @TODO: Rename RUNNER_SCRIPT_ to reduce confusion with github runner CLI
7178
if [[ -z "$COMPOSER_RUNTIME_BIN_DIR" ]]; then
72-
RUNNER_SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
79+
RUNNER_SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/github-runner"
7380
else
74-
RUNNER_SCRIPT_PATH="$COMPOSER_RUNTIME_BIN_DIR"
81+
RUNNER_SCRIPT_PATH="$COMPOSER_RUNTIME_BIN_DIR/github-runner"
82+
fi
83+
if [[ ! -f "${RUNNER_SCRIPT_PATH}" ]]; then
84+
say "Path detection failed."
85+
say "No file found at ${RUNNER_SCRIPT_PATH}"
86+
exit 1
7587
fi
7688
RUNNER_SCRIPT_DIR=$(dirname $RUNNER_SCRIPT_PATH)
7789

@@ -140,27 +152,35 @@ verify() {
140152
line
141153
welcome
142154

143-
if [[ -z "${GITHUB_REPOSITORY}" ]]; then
144-
line
145-
say GITHUB_REPOSITORY is required. Use --repo or set the environment variable.
146-
exit 1
147-
fi
148-
149-
# Add name to labels.
150-
RUNNER_CONFIG_LABELS="${RUNNER_CONFIG_LABELS},${RUNNER_CONFIG_NAME}"
151-
152-
# Gather API token to create runners.
153-
if [[ -z "${GITHUB_TOKEN}" ]]; then
154-
line
155-
say GITHUB_TOKEN is required. Use --token or set the environment variable.
156-
say Create a 'Personal Access Token' owned by the organization, with 'admin:write' access on this repository
157-
say https://github.com/settings/personal-access-tokens/new
158-
line
159-
exit 1
155+
if [[ -n "${RUNNER_CONFIG}" ]]; then
156+
if [[ -z "${GITHUB_REPOSITORY}" ]]; then
157+
line
158+
say GITHUB_REPOSITORY is required. Use --repo or set the environment variable.
159+
exit 1
160+
fi
161+
162+
# Add name to labels.
163+
RUNNER_CONFIG_LABELS="${RUNNER_CONFIG_LABELS},${RUNNER_CONFIG_NAME}"
164+
165+
# Gather API token to create runners.
166+
if [[ -z "${GITHUB_TOKEN}" ]]; then
167+
line
168+
say GITHUB_TOKEN is required. Use --token or set the environment variable.
169+
say Create a 'Personal Access Token' owned by the organization, with 'admin:write' access on this repository
170+
say https://github.com/settings/personal-access-tokens/new
171+
line
172+
exit 1
173+
fi
160174
fi
161175

162176
# Prepare runner dir.
163177
line
178+
say "User: $(whoami)"
179+
say "Host: $(hostname -f)"
180+
say "Current directory: $(pwd)"
181+
say "Runner download path: $RUNNER_PATH"
182+
line
183+
164184
if [[ ! -d $RUNNER_PATH ]]; then
165185
mkdir -p $RUNNER_PATH
166186
RUNNER_FOLDER=$(realpath $RUNNER_PATH)
@@ -170,6 +190,9 @@ verify() {
170190
say Found folder: $RUNNER_FOLDER
171191
fi
172192

193+
line
194+
info
195+
173196
# getNewToken.
174197
line
175198
RESPONSE=$(curl -L \
@@ -248,13 +271,15 @@ configure() {
248271
}
249272

250273
stop() {
251-
say "Stopped."
274+
say "Stopped. Run.sh PID: $RUNNER_PID"
275+
kill $RUNNER_PID
252276
./config.sh remove --token ${RUNNER_CONFIG_TOKEN} || say "config remove didn't work."
253277

254278
exit 0
255279
}
256280
cancel() {
257-
echo
281+
say "Cancelled. Run.sh PID: $RUNNER_PID"
282+
kill $RUNNER_PID
258283
if [[ "${RUNNER_CLEANUP}" == "yes" ]]; then
259284
say "Cancelled. Removing $RUNNER_FOLDER..."
260285
rm -rf $RUNNER_FOLDER

0 commit comments

Comments
 (0)