From 8905d6d387d104d81ecf683c9260ba05e2b2eea9 Mon Sep 17 00:00:00 2001 From: Dimi Kot Date: Thu, 29 Feb 2024 17:11:32 -0800 Subject: [PATCH] Add Dockerfile for self-hosted runner bolierplate --- .github/workflows/ci.yml | 5 +++++ docker/Dockerfile | 45 +++++++++++++++++++++++++++++++++++++++ docker/docker-compose.yml | 11 ++++++++++ docker/entrypoint.sh | 30 ++++++++++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 docker/Dockerfile create mode 100644 docker/docker-compose.yml create mode 100644 docker/entrypoint.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 83e6846..f78cfdb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,3 +32,8 @@ jobs: set -e ls -la ~/ci-storage/dimikot/ci-storage [ "$(cat dummy.txt)" = "dummy" ] || { echo "dummy.txt was not restored"; exit 1; } + + example: + runs-on: self-hosted + steps: + - run: for i in $(seq 1 600); do echo $i; sleep 1; done diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..e113e8b --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,45 @@ +FROM ubuntu:22.04 + +ARG RUNNER_VERSION + +ENV GH_OWNER="" +ENV GH_REPO="" +ENV GH_TOKEN="" + +ENV DEBIAN_FRONTEND=noninteractive +RUN true \ + && apt-get update -y \ + && apt-get upgrade -y \ + && apt-get install -y --no-install-recommends \ + awscli jq gh \ + mc gcc git curl wget pv psmisc unzip vim nano telnet net-tools bash-completion \ + libssl-dev apt-transport-https build-essential ca-certificates locales pkg-config + +RUN true \ + && useradd -m ubuntu + +USER ubuntu +RUN true \ + && mkdir /home/ubuntu/actions-runner \ + && cd /home/ubuntu/actions-runner \ + && arch=$(dpkg --print-architecture) \ + && case "$arch" in \ + x86_64|amd64) arch=linux-x64 ;; \ + aarch64|arm64) arch=linux-arm64 ;; \ + *) echo >&2 "unsupported architecture: $arch"; exit 1 ;; \ + esac \ + && curl --no-progress-meter -L https://github.com/actions/runner/releases/download/v$RUNNER_VERSION/actions-runner-$arch-$RUNNER_VERSION.tar.gz | tar xz + +USER root +RUN /home/ubuntu/actions-runner/bin/installdependencies.sh \ + apt-get autoremove \ + && apt-get clean \ + && apt-get autoclean \ + && rm -rf /var/lib/apt/lists/* + +USER ubuntu +COPY --chmod=755 --chown=ubuntu:ubuntu entrypoint.sh /home/ubuntu + +WORKDIR /home/ubuntu +ENTRYPOINT ["./entrypoint.sh"] +CMD ["./run.sh"] diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml new file mode 100644 index 0000000..dc8abcc --- /dev/null +++ b/docker/docker-compose.yml @@ -0,0 +1,11 @@ +version: "3.4" +services: + ci-storage: + build: + dockerfile: ./Dockerfile + args: + - RUNNER_VERSION=2.314.1 + environment: + - GH_OWNER + - GH_REPO + - GH_TOKEN diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100644 index 0000000..5ded1f5 --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,30 @@ +#!/bin/bash +set -u -e -o xtrace + +: $GH_OWNER +: $GH_REPO +: $GH_TOKEN # used by gh cli + +cd ./actions-runner + +name="ci-storage-$(hostname)" + +token=$(gh api -X POST --jq .token "repos/$GH_OWNER/$GH_REPO/actions/runners/registration-token") + +config=$( + set +o xtrace + echo "{\"name\":\"$name\",\"labels\":[\"self-hosted\"],\"runner_group_id\":1}" \ + | gh api -X POST --input - \ + --jq .encoded_jit_config \ + "repos/$GH_OWNER/$GH_REPO/actions/runners/generate-jitconfig" +) + +cleanup() { + token=$(gh api -X POST --jq .token "repos/$GH_OWNER/$GH_REPO/actions/runners/remove-token") + ./config.sh remove --token "$token" +} + +trap "cleanup; exit 130" INT +trap "cleanup; exit 143" TERM + +eval "$@" --jitconfig "$config" & wait $!