Skip to content

Commit 2e45e15

Browse files
committed
feat(docker): enhance GitHub runner configuration and documentation
- Added support for dynamic runner names using environment variable expansion. - Updated README with examples of configuring Docker containers and environment variables. - Improved the safety of running configuration commands in entrypoint script by using array arguments instead of eval. - Added gettext-base package to Dockerfile for environment variable substitution capabilities.
1 parent f589bf6 commit 2e45e15

File tree

4 files changed

+76
-30
lines changed

4 files changed

+76
-30
lines changed

.claude/settings.local.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
"Bash(git add:*)",
2525
"Bash(git commit:*)",
2626
"Bash(git rm:*)",
27-
"Bash(grep:*)"
27+
"Bash(grep:*)",
28+
"WebSearch",
29+
"Bash(docker compose:*)",
30+
"Bash(docker ps:*)"
2831
],
2932
"deny": []
3033
}

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,46 @@ docker run -d \
8282
emberstack/github-actions-runner:latest
8383
```
8484

85+
#### Dynamic Runner Naming with Environment Variables
86+
The `GITHUB_RUNNER_NAME` supports safe environment variable expansion, allowing dynamic runner names based on any runtime environment variable:
87+
88+
```bash
89+
# Use container hostname (container ID in Docker)
90+
docker run -d \
91+
--name github-runner \
92+
-e GITHUB_RUNNER_URL="https://github.com/your-org/your-repo" \
93+
-e GITHUB_RUNNER_PAT="your-personal-access-token" \
94+
-e GITHUB_RUNNER_NAME='$HOSTNAME' \
95+
emberstack/github-actions-runner:latest
96+
97+
# Combine with prefix/suffix
98+
docker run -d \
99+
--name github-runner \
100+
-e GITHUB_RUNNER_URL="https://github.com/your-org/your-repo" \
101+
-e GITHUB_RUNNER_PAT="your-personal-access-token" \
102+
-e GITHUB_RUNNER_NAME='runner-$HOSTNAME' \
103+
emberstack/github-actions-runner:latest
104+
105+
# Use in Docker Compose with custom hostname
106+
services:
107+
runner:
108+
image: emberstack/github-actions-runner:latest
109+
hostname: worker-node-1
110+
environment:
111+
GITHUB_RUNNER_URL: "https://github.com/your-org/your-repo"
112+
GITHUB_RUNNER_PAT: "your-personal-access-token"
113+
GITHUB_RUNNER_NAME: 'runner-$HOSTNAME' # Will be "runner-worker-node-1"
114+
```
115+
116+
**Examples of Supported Variables:**
117+
- `$HOSTNAME` or `${HOSTNAME}` - The container's hostname (container ID by default in Docker)
118+
- `$USER` or `${USER}` - The current user (typically "runner")
119+
- `$HOME` or `${HOME}` - The user's home directory
120+
- `$PATH` - System PATH
121+
- Any custom environment variable you define
122+
123+
**Note:** Use single quotes (`'`) to prevent variable expansion on the host shell, allowing expansion inside the container. Variable expansion is performed safely using `envsubst`, preventing code injection.
124+
85125
In ephemeral mode, the runner will:
86126
- Process only one job and then automatically deregister
87127
- Provide a clean, isolated environment for each workflow run
@@ -91,8 +131,7 @@ In ephemeral mode, the runner will:
91131
#### Environment Variables
92132
- `GITHUB_RUNNER_URL` (required): Repository, organization, or enterprise URL
93133
- `GITHUB_RUNNER_PAT` or `GITHUB_RUNNER_TOKEN` (required): Authentication token
94-
- `GITHUB_RUNNER_NAME` (optional): Runner name (defaults to hostname, can be overridden by GITHUB_RUNNER_USE_HOSTNAME)
95-
- `GITHUB_RUNNER_USE_HOSTNAME` (optional): Set to "true" to always use container hostname as runner name, overriding GITHUB_RUNNER_NAME
134+
- `GITHUB_RUNNER_NAME` (optional): Runner name (defaults to hostname). Supports safe environment variable expansion using standard shell syntax
96135
- `GITHUB_RUNNER_LABELS` (optional): Comma-separated list of labels
97136
- `GITHUB_RUNNER_GROUP` (optional): Runner group name
98137
- `GITHUB_RUNNER_WORKDIR` (optional): Working directory for jobs

src/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ RUN apt-get update && \
88
apt-get install -y --no-install-recommends \
99
wget \
1010
ca-certificates \
11+
gettext-base \
1112
&& rm -rf /var/lib/apt/lists/*
1213

1314
# Install yq with architecture detection

src/entrypoint.sh

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -77,55 +77,58 @@ cleanup_runner() {
7777
# Function to configure runner
7878
configure_runner() {
7979
echo "Configuring GitHub Actions runner..."
80-
81-
# Build configuration command
82-
CONFIG_CMD="./config.sh --url \"${GITHUB_RUNNER_URL}\" --unattended --replace"
83-
80+
81+
# Build configuration command using array for safety (no eval needed)
82+
CONFIG_ARGS=(
83+
"--url" "${GITHUB_RUNNER_URL}"
84+
"--unattended"
85+
"--replace"
86+
)
87+
8488
# Add authentication (prefer PAT over TOKEN)
8589
if [ -n "${GITHUB_RUNNER_PAT}" ]; then
86-
CONFIG_CMD="${CONFIG_CMD} --pat \"${GITHUB_RUNNER_PAT}\""
90+
CONFIG_ARGS+=("--pat" "${GITHUB_RUNNER_PAT}")
8791
elif [ -n "${GITHUB_RUNNER_TOKEN}" ]; then
88-
CONFIG_CMD="${CONFIG_CMD} --token \"${GITHUB_RUNNER_TOKEN}\""
92+
CONFIG_ARGS+=("--token" "${GITHUB_RUNNER_TOKEN}")
8993
else
9094
echo "ERROR: Either GITHUB_RUNNER_PAT or GITHUB_RUNNER_TOKEN must be provided"
9195
exit 1
9296
fi
93-
94-
# Add runner name (with hostname preference option)
95-
if [ "${GITHUB_RUNNER_USE_HOSTNAME}" = "true" ]; then
96-
# When flag is true, always use hostname regardless of GITHUB_RUNNER_NAME
97-
CONFIG_CMD="${CONFIG_CMD} --name \"$(hostname)\""
98-
elif [ -n "${GITHUB_RUNNER_NAME}" ]; then
99-
# Use provided name if flag is not true and name is provided
100-
CONFIG_CMD="${CONFIG_CMD} --name \"${GITHUB_RUNNER_NAME}\""
97+
98+
# Add runner name (with safe environment variable expansion)
99+
if [ -n "${GITHUB_RUNNER_NAME}" ]; then
100+
# Safely expand environment variables using envsubst
101+
# This prevents code injection while allowing any env var to be used
102+
EXPANDED_NAME=$(echo "${GITHUB_RUNNER_NAME}" | envsubst)
103+
CONFIG_ARGS+=("--name" "${EXPANDED_NAME}")
101104
else
102-
# Fall back to hostname if no name provided and flag is not true
103-
CONFIG_CMD="${CONFIG_CMD} --name \"$(hostname)\""
105+
# Fall back to hostname if no name provided
106+
CONFIG_ARGS+=("--name" "$(hostname)")
104107
fi
105-
108+
106109
# Add labels if provided
107110
if [ -n "${GITHUB_RUNNER_LABELS}" ]; then
108-
CONFIG_CMD="${CONFIG_CMD} --labels \"${GITHUB_RUNNER_LABELS}\""
111+
CONFIG_ARGS+=("--labels" "${GITHUB_RUNNER_LABELS}")
109112
fi
110-
113+
111114
# Add runner group if provided
112115
if [ -n "${GITHUB_RUNNER_GROUP}" ]; then
113-
CONFIG_CMD="${CONFIG_CMD} --runnergroup \"${GITHUB_RUNNER_GROUP}\""
116+
CONFIG_ARGS+=("--runnergroup" "${GITHUB_RUNNER_GROUP}")
114117
fi
115-
118+
116119
# Add work directory if provided
117120
if [ -n "${GITHUB_RUNNER_WORKDIR}" ]; then
118-
CONFIG_CMD="${CONFIG_CMD} --work \"${GITHUB_RUNNER_WORKDIR}\""
121+
CONFIG_ARGS+=("--work" "${GITHUB_RUNNER_WORKDIR}")
119122
fi
120-
123+
121124
# Add ephemeral flag if requested
122125
if [ "${GITHUB_RUNNER_EPHEMERAL}" = "true" ]; then
123-
CONFIG_CMD="${CONFIG_CMD} --ephemeral"
126+
CONFIG_ARGS+=("--ephemeral")
124127
echo "Configuring runner in ephemeral mode (will process only one job)"
125128
fi
126-
127-
# Execute configuration
128-
eval ${CONFIG_CMD}
129+
130+
# Execute configuration safely without eval
131+
./config.sh "${CONFIG_ARGS[@]}"
129132
}
130133

131134
# Function to setup groups

0 commit comments

Comments
 (0)