Skip to content

Commit

Permalink
Split shell scripts based on usage
Browse files Browse the repository at this point in the history
  • Loading branch information
dormant-user committed Sep 1, 2024
1 parent 9daa458 commit 0f9504c
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 68 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/test-runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ jobs:
- name: Verify 'python'
run: python -V
shell: bash
- name: Verify 'rust'
- name: Verify 'rustc'
run: rustc --version
shell: bash
- name: Verify 'cargo'
run: cargo --version
shell: bash
- name: Verify 'rustfmt'
run: rustfmt --version
shell: bash
- name: Verify 'go'
run: go version
shell: bash
Expand Down
8 changes: 3 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# IntelIJ
.DS_Store
.idea

#Docker variables
# Docker variables
*.env
.env

#VSCODE
# VSCode
.vscode/*

# Built Visual Studio Code Extensions
*.vsix
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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)
# Install the packages and dependencies
RUN apt-get install -y --no-install-recommends \
curl \
wget \
Expand All @@ -32,7 +32,7 @@ RUN apt-get install -y --no-install-recommends \
python3.10-dev \
python3-pip \
nodejs \
npm \
npm \
golang-go

# Create a symbolic link for python pointing to python3.10
Expand All @@ -52,7 +52,9 @@ RUN chown -R docker ~docker && /home/docker/actions-runner/bin/installdependenci

# Add start script and make it executable
ADD scripts/start.sh start.sh
ADD scripts/detector.sh detector.sh
RUN chmod +x start.sh
RUN chmod +x detector.sh

# Set the user to "docker" so all subsequent commands are run as the docker user
USER docker
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Build a self-hosted GitHub action runner as an Ubuntu linux container
### Build

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

### Run
Expand Down
5 changes: 2 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ services:
args:
RUNNER_VERSION: '2.319.1'
restart: always
env_file:
- './.env'
env_file: '.env'
environment:
GIT_TOKEN: ${GIT_TOKEN}
GIT_OWNER: ${GIT_OWNER}
Expand All @@ -17,5 +16,5 @@ services:
RUNNER_NAME: ${RUNNER_NAME}
WORK_DIR: ${WORK_DIR}
LABELS: ${LABELS}
container_name: docker-github-runner-linux
container_name: github-runner-linux
working_dir: /home/docker
12 changes: 0 additions & 12 deletions samples/docker-compose.yml

This file was deleted.

42 changes: 42 additions & 0 deletions scripts/detector.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

# NOTE: `uname -m` is more accurate and universal than `arch`
# See https://en.wikipedia.org/wiki/Uname
unamem="$(uname -m)"
case $unamem in
*aarch64*|arm64)
architecture="arm64";;
*64*)
architecture="amd64";;
*86*)
architecture="386";;
*armv5*)
architecture="armv5";;
*armv6*)
architecture="armv6";;
*armv7*)
architecture="armv7";;
*)
echo "Unknown architecture: $unamem"
;;
esac
export architecture="${architecture}"

unameu="$(tr '[:lower:]' '[:upper:]' <<< "$(uname)")"
if [[ $unameu == *DARWIN* ]]; then
os_name="darwin"
elif [[ $unameu == *LINUX* ]]; then
os_name="linux"
elif [[ $unameu == *FREEBSD* ]]; then
os_name="freebsd"
elif [[ $unameu == *NETBSD* ]]; then
os_name="netbsd"
elif [[ $unameu == *OPENBSD* ]]; then
os_name="openbsd"
elif [[ $unameu == *WIN* || $unameu == MSYS* ]]; then
# Should catch cygwin
os_name="windows"
else
echo "Unknown OS: $(uname)"
fi
export os_name="${os_name}"
60 changes: 16 additions & 44 deletions scripts/start.sh
Original file line number Diff line number Diff line change
@@ -1,54 +1,26 @@
#!/bin/bash
# 'set -e' stops the execution of a script if a command or pipeline has an error.
# This is the opposite of the default shell behaviour, which is to ignore errors in scripts.
set -e

# NOTE: `uname -m` is more accurate and universal than `arch`
# See https://en.wikipedia.org/wiki/Uname
unamem="$(uname -m)"
case $unamem in
*aarch64*|arm64)
architecture="arm64";;
*64*)
architecture="amd64";;
*86*)
architecture="386";;
*armv5*)
architecture="armv5";;
*armv6*)
architecture="armv6";;
*armv7*)
architecture="armv7";;
*)
echo "Unknown architecture: $unamem"
;;
esac
# Set defaults
os_name=""
architecture=""

unameu="$(tr '[:lower:]' '[:upper:]' <<< "$(uname)")"
if [[ $unameu == *DARWIN* ]]; then
os_name="darwin"
elif [[ $unameu == *LINUX* ]]; then
os_name="linux"
elif [[ $unameu == *FREEBSD* ]]; then
os_name="freebsd"
elif [[ $unameu == *NETBSD* ]]; then
os_name="netbsd"
elif [[ $unameu == *OPENBSD* ]]; then
os_name="openbsd"
elif [[ $unameu == *WIN* || $unameu == MSYS* ]]; then
# Should catch cygwin
os_name="windows"
else
echo "Unknown OS: $(uname)"
fi
# Get to the current directory
current_dir="$(dirname "$(realpath "$0")")"
source "${current_dir}/detector.sh"

instance_id() {
# Use randomly generated instance IDs (AWS format) as default runner names
letters=$(tr -dc '[:lower:]' < /dev/urandom | head -c 4)
digits=$(tr -dc '0-9' < /dev/urandom | head -c 12)
eid=$(echo "$letters$digits" | fold -w1 | shuf | tr -d '\n')
echo "0$eid"
echo "i-0$eid"
}

# Env vars (docker-compose.yml)
RUNNER_NAME="${RUNNER_NAME:-"i-$(instance_id)"}"
RUNNER_NAME="${RUNNER_NAME:-"$(instance_id)"}"
RUNNER_GROUP="${RUNNER_GROUP:-"default"}"
WORK_DIR="${WORK_DIR:-"_work"}"
LABELS="${LABELS:-"docker-node,$os_name-$architecture"}"
Expand All @@ -57,7 +29,7 @@ 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}" \
-H "Authorization: Bearer ${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
Expand All @@ -76,7 +48,7 @@ org_level_runner() {
# 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}" \
-H "Authorization: Bearer ${GIT_TOKEN}" \
"https://api.github.com/orgs/${GIT_OWNER}/actions/runners/registration-token" \
| jq .token --raw-output)
cd "/home/docker/actions-runner" || exit 1
Expand All @@ -90,10 +62,10 @@ org_level_runner() {
}

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

Expand All @@ -105,4 +77,4 @@ cleanup() {
trap 'cleanup; exit 130' INT
trap 'cleanup; exit 143' TERM

./run.sh & wait $!
./run.sh & wait $!

0 comments on commit 0f9504c

Please sign in to comment.