diff --git a/.github/workflows/test-runner.yml b/.github/workflows/test-runner.yml index a11b8ed..6d55e60 100644 --- a/.github/workflows/test-runner.yml +++ b/.github/workflows/test-runner.yml @@ -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 diff --git a/.gitignore b/.gitignore index 64cd408..ed374a2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,10 @@ +# IntelIJ .DS_Store .idea -#Docker variables +# Docker variables *.env .env -#VSCODE +# VSCode .vscode/* - -# Built Visual Studio Code Extensions -*.vsix diff --git a/Dockerfile b/Dockerfile index b9c751a..0f5d4d7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 \ @@ -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 @@ -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 diff --git a/README.md b/README.md index 8850a42..4716d68 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml index ebfddf5..5d21175 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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} @@ -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 diff --git a/samples/docker-compose.yml b/samples/docker-compose.yml deleted file mode 100644 index ecec652..0000000 --- a/samples/docker-compose.yml +++ /dev/null @@ -1,12 +0,0 @@ ---- -version: '1.0' - -services: - runner: - image: runner:latest - build: - context: . - args: - RUNNER_VERSION: '2.319.1' - env_file: - - ./.env diff --git a/scripts/detector.sh b/scripts/detector.sh new file mode 100644 index 0000000..c608dce --- /dev/null +++ b/scripts/detector.sh @@ -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}" diff --git a/scripts/start.sh b/scripts/start.sh index f7ab73b..b69ee0d 100644 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -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"}" @@ -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 @@ -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 @@ -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 @@ -105,4 +77,4 @@ cleanup() { trap 'cleanup; exit 130' INT trap 'cleanup; exit 143' TERM -./run.sh & wait $! \ No newline at end of file +./run.sh & wait $!