diff --git a/README.md b/README.md index 493c705..97c56b3 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,10 @@ docker compose up -d - **RUNNER_NAME** - Runner name. Uses a random instance ID. - **WORK_DIR** - Work directory. Uses `_work` - **LABELS** - Runner labels (comma separated). Uses `"docker-node,${os_name}-${architecture}"` +- **REUSE_EXISTING** - Re-use existing configuration. Defaults to `false` + +> `REUSE_EXISTING` can come in handy when a container restarts due to a problem, +> or when a container is deleted without gracefully shutting down. ## Development diff --git a/docker-compose.yml b/docker-compose.yml index 2e3d7a1..852a9d6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,5 +14,6 @@ services: RUNNER_NAME: ${RUNNER_NAME} WORK_DIR: ${WORK_DIR} LABELS: ${LABELS} + REUSE_EXISTING: true container_name: github-runner-linux working_dir: /home/docker diff --git a/scripts/start.sh b/scripts/start.sh index b69ee0d..00248c3 100644 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -24,6 +24,7 @@ RUNNER_NAME="${RUNNER_NAME:-"$(instance_id)"}" RUNNER_GROUP="${RUNNER_GROUP:-"default"}" WORK_DIR="${WORK_DIR:-"_work"}" LABELS="${LABELS:-"docker-node,$os_name-$architecture"}" +REUSE_EXISTING="${REUSE_EXISTING:-"false"}" repo_level_runner() { # https://docs.github.com/en/rest/actions/self-hosted-runners#create-a-registration-token-for-a-repository @@ -61,12 +62,19 @@ org_level_runner() { --url "https://github.com/${GIT_OWNER}" } -if [ -n "${GIT_REPOSITORY}" ]; then - echo "Creating a repository level self-hosted runner ['${RUNNER_NAME}'] for ${GIT_REPOSITORY}" - repo_level_runner +if [[ "$REUSE_EXISTING" == "true" || "$REUSE_EXISTING" == "1" ]] && + [[ -d "/home/docker/actions-runner" ]] && + [[ -f "/home/docker/actions-runner/config.sh" ]] && + [[ -f "/home/docker/actions-runner/run.sh" ]]; then + echo "Existing configuration found. Re-using it..." else - echo "Creating an organization level self-hosted runner '${RUNNER_NAME}'" - org_level_runner + if [[ -n "$GIT_REPOSITORY" ]]; then + echo "Creating a repository level self-hosted runner ['${RUNNER_NAME}'] for ${GIT_REPOSITORY}" + repo_level_runner + else + echo "Creating an organization level self-hosted runner '${RUNNER_NAME}'" + org_level_runner + fi fi cleanup() {