Skip to content

Commit

Permalink
Add an option to load .env file
Browse files Browse the repository at this point in the history
Set default shell in Dockerfile
Set default python to 3.10
Update README.md and GH action workflow
  • Loading branch information
dormant-user committed Sep 1, 2024
1 parent c0d12b0 commit c8eef26
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
**
!./scripts
!dockerfile
!Dockerfile
22 changes: 20 additions & 2 deletions .github/workflows/test-runner.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
name: Local runner test

on:
workflow_dispatch:
push:
branches:
- main

jobs:
test-runner:
runs-on: [self-hosted]
runs-on: docker-node
steps:
- uses: actions/checkout@v4
- name: Verify 'curl'
run: curl --version
- name: Verify 'git'
run: git --version
- name: Verify 'jq'
run: jq --version
- name: Verify 'python'
run: python -V
- name: Verify 'rust'
run: rustc --version
- name: Verify 'go'
run: go version
- name: Verify 'node'
run: |
node --version
npm --version
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
# docker-github-runner-linux

Repository for building a self hosted GitHub runner as a ubuntu linux container
Repository for building a self-hosted GitHub runner as a ubuntu linux container

### Build

```
```shell
docker build --build-arg RUNNER_VERSION=2.319.1 -t runner .
```

### Run

Set latest `RUNNER_VERSION`

```shell
export RUNNER_VERSION=$(curl -sL \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/actions/runner/releases/latest | jq .tag_name --raw-output)
```

```shell
docker compose up -d
```

### Exec

```
```shell
docker exec -it container-name sh
```
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ services:
context: .
args:
RUNNER_VERSION: '2.319.1'
restart: always
env_file:
- './.env'
environment:
GIT_TOKEN: ${GIT_TOKEN}
GIT_OWNER: ${GIT_OWNER}
GIT_REPOSITORY: ${GIT_REPOSITORY}
RUNNER_GROUP: ${RUNNER_GROUP}
RUNNER_NAME: ${RUNNER_NAME}
WORK_DIR: ${WORK_DIR}
LABELS: ${LABELS}
container_name: docker-github-runner-linux
working_dir: /home/docker
43 changes: 27 additions & 16 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
# base image
FROM ubuntu:22.04

#input GitHub runner version argument
SHELL ["/bin/bash", "-c"]

ARG RUNNER_VERSION
ENV RUNNER_VERSION="${RUNNER_VERSION:-2.319.1}"
ENV DEBIAN_FRONTEND=noninteractive
ENV HOME="/home/docker"
ENV RELEASE_URL="https://github.com/actions/runner/releases"

LABEL RunnerVersion=${RUNNER_VERSION}
# Set top-level working directory
WORKDIR ${HOME}

# update the base packages + add a non-sudo user
RUN apt-get update -y && apt-get upgrade -y && useradd -m docker
# Update the base packages and add a non-sudo user
RUN apt-get update -y && \
apt-get upgrade -y && \
useradd -m docker

# install the packages and dependencies along with jq so we can parse JSON (add additional packages as necessary)
RUN apt-get install -y --no-install-recommends \
curl \
nodejs \
wget \
unzip \
vim \
Expand All @@ -25,24 +30,30 @@ RUN apt-get install -y --no-install-recommends \
python3.10 \
python3.10-venv \
python3.10-dev \
python3-pip
python3-pip \
nodejs \
golang-go

# cd into the user directory, download and unzip the github actions runner
RUN cd /home/docker && mkdir actions-runner && cd actions-runner \
&& curl -O -L https://github.com/actions/runner/releases/download/v${RUNNER_VERSION}/actions-runner-linux-x64-${RUNNER_VERSION}.tar.gz \
# Create a symbolic link for python pointing to python3.10
RUN ln -s /usr/bin/python3.10 /usr/bin/python

# https://github.com/rust-lang/rustup/issues/297#issuecomment-444818896
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y

# Download and unzip the github actions runner
RUN mkdir actions-runner && cd actions-runner \
&& curl -O -L ${RELEASE_URL}/download/v${RUNNER_VERSION}/actions-runner-linux-x64-${RUNNER_VERSION}.tar.gz \
&& tar xzf ./actions-runner-linux-x64-${RUNNER_VERSION}.tar.gz

# install some additional dependencies
# Install additional dependencies
RUN chown -R docker ~docker && /home/docker/actions-runner/bin/installdependencies.sh

# add over the start.sh script
# Add start script and make it executable
ADD scripts/start.sh start.sh

# make the script executable
RUN chmod +x start.sh

# set the user to "docker" so all subsequent commands are run as the docker user
# Set the user to "docker" so all subsequent commands are run as the docker user
USER docker

# set the entrypoint to the start.sh script
# Set the entrypoint to the start.sh script
ENTRYPOINT ["./start.sh"]
50 changes: 27 additions & 23 deletions scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ case $unamem in
;;
esac

unameu="$(tr '[:lower:]' '[:upper:]' <<<$(uname))"
unameu="$(tr '[:lower:]' '[:upper:]' <<< "$(uname)")"
if [[ $unameu == *DARWIN* ]]; then
os_name="darwin"
elif [[ $unameu == *LINUX* ]]; then
Expand All @@ -40,58 +40,62 @@ else
fi

DEFAULT_LABEL="$os_name-$architecture"

RUNNER_SUFFIX=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 5 | head -n 1)
RUNNER_NAME="docker-node-${RUNNER_SUFFIX}"
RUNNER_SUFFIX="$(head -c 20 /dev/urandom | tr -dc 'a-z0-9' | fold -w 5 | head -n 1)"
DEFAULT_RUNNER_NAME="docker-node-${RUNNER_SUFFIX}"

# Env vars (docker-compose.yml)
RUNNER_NAME="${RUNNER_NAME:-$DEFAULT_RUNNER_NAME}"
RUNNER_GROUP="${RUNNER_GROUP:-default}"
WORK_DIR="${WORK_DIR:-_work}"
LABELS="${LABELS:-$DEFAULT_LABEL}"

if [ -n "${GIT_REPOSITORY}" ]; then
echo "Creating repository level self-hosted runner ['${RUNNER_NAME}'] for ${GIT_REPOSITORY}"
repo_level_runner() {
# https://docs.github.com/en/rest/actions/self-hosted-runners#create-a-registration-token-for-a-repository
REG_TOKEN=$(curl -sX POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${GIT_TOKEN}" \
"https://api.github.com/repos/${GIT_OWNER}/${GIT_REPOSITORY}/actions/runners/registration-token" \
| jq .token --raw-output)

cd /home/docker/actions-runner

-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${GIT_TOKEN}" \
"https://api.github.com/repos/${GIT_OWNER}/${GIT_REPOSITORY}/actions/runners/registration-token" \
| jq .token --raw-output)
cd "/home/docker/actions-runner" || exit 1
./config.sh --unattended \
--work "${WORK_DIR}" \
--labels "${LABELS}" \
--token "${REG_TOKEN}" \
--name "${RUNNER_NAME}" \
--runnergroup "${RUNNER_GROUP}" \
--url "https://github.com/${GIT_OWNER}/${GIT_REPOSITORY}"
else
echo "Creating organization level self-hosted runner '${RUNNER_NAME}'"
}

org_level_runner() {
# https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/about-self-hosted-runners#restricting-the-use-of-self-hosted-runners
# https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/about-self-hosted-runners#self-hosted-runner-security
# https://docs.github.com/en/rest/actions/self-hosted-runners#create-a-registration-token-for-an-organization
REG_TOKEN=$(curl -sX POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${GIT_TOKEN}" \
"https://api.github.com/orgs/${GIT_OWNER}/actions/runners/registration-token" \
| jq .token --raw-output)

cd /home/docker/actions-runner

-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${GIT_TOKEN}" \
"https://api.github.com/orgs/${GIT_OWNER}/actions/runners/registration-token" \
| jq .token --raw-output)
cd "/home/docker/actions-runner" || exit 1
./config.sh \
--work "${WORK_DIR}" \
--labels "${LABELS}" \
--token "${REG_TOKEN}" \
--name "${RUNNER_NAME}" \
--runnergroup "${RUNNER_GROUP}" \
--url "https://github.com/${GIT_OWNER}"
}

if [ -n "${GIT_REPOSITORY}" ]; then
echo "Creating repository level self-hosted runner ['${RUNNER_NAME}'] for ${GIT_REPOSITORY}"
repo_level_runner
else
echo "Creating organization level self-hosted runner '${RUNNER_NAME}'"
org_level_runner
fi

cleanup() {
echo "Removing runner..."
./config.sh remove --unattended --token ${REG_TOKEN}
./config.sh remove --token "${REG_TOKEN}"
}

trap 'cleanup; exit 130' INT
Expand Down

0 comments on commit c8eef26

Please sign in to comment.