diff --git a/github-action-runner-docker/Dockerfile b/github-action-runner-docker/Dockerfile new file mode 100644 index 000000000..c28faf44b --- /dev/null +++ b/github-action-runner-docker/Dockerfile @@ -0,0 +1,24 @@ +FROM ubuntu:18.04 + +ARG RUNNER_VERSION=2.287.1 +ARG TOKEN +ARG NAME=internal-runner + +USER root + +WORKDIR /actions-runner + +RUN apt-get update -qq \ + && apt-get install -y curl \ + && curl -o actions-runner-linux-x64-${RUNNER_VERSION}.tar.gz -L https://github.com/actions/runner/releases/download/v${RUNNER_VERSION}/actions-runner-linux-x64-${RUNNER_VERSION}.tar.gz \ + && tar xzf ./actions-runner-linux-x64-${RUNNER_VERSION}.tar.gz \ + && ./bin/installdependencies.sh \ + && rm ./actions-runner-linux-x64-${RUNNER_VERSION}.tar.gz \ + && apt-get purge -y \ + && chown 1000:1000 -R /actions-runner + +USER 1000 + +RUN ./config.sh --url https://github.com/EQ-Alpha/KeyDB --token ${TOKEN} logout --name ${NAME} --unattended + +ENTRYPOINT ["./run.sh"] diff --git a/github-action-runner-docker/README.md b/github-action-runner-docker/README.md new file mode 100644 index 000000000..3619051f3 --- /dev/null +++ b/github-action-runner-docker/README.md @@ -0,0 +1,44 @@ +# Github action runner. + +## Overview + All details are [here](https://docs.github.com/en/actions/hosting-your-own-runners/about-self-hosted-runners). + In brief overview, self-hosted action runner is running on our hosts, connects to the github and waits for the + jobs to execute. Bellow is instruction on how to create image for the runner and start it in our Kubernetes + cluster. + +## Creating a new runner. + +1. Got [github](https://github.com/EQ-Alpha/KeyDB/settings/actions/runners/new?arch=x64&os=linux) to create a new runner. +2. The link above should display a script for installing runner. It should be aligned with what we have in a +[Dockerfile](https://github.sc-corp.net/Snapchat/keydb-internal/github-action-runner-docker/Dockerfile). +3. Take the token from that script. Should be in "Configure" section: + ``` + ./config.sh --url https://github.com/EQ-Alpha/KeyDB --token AUQJRVZIQCLO4ZQOZAOC3L3B7RHIU + ``` +4. Take the version of the runner from the "Download" section from file name. Example is "2.287.1", +5. Build and public the image: + ``` + ./build-and-publish.sh --token --version + ``` + Docker will register runner in Github during the build. If you got an error +```A runner exists with the same name```, it means you've already built the image +for runner with that name. In this case you can either give a new name to the runner +(using ```--name ``` parameter to the build script) or delete existing runner in +[github](https://github.com/EQ-Alpha/KeyDB/settings/actions/runners). Deleting the +runner will break existing runners running in our infrastructure. You may want to +delete them. +6. The default image is "[gcr.io/caching-infra/keydb-github-action-runner](https://console.cloud.google.com/gcr/images/caching-infra/global/keydb-github-action-runner?project=caching-infra)" +but you can set any other image full name to the building script (```--image ```). +7. Deploy new image to cluster [caching-infra--t-us-east4--staging](https://switchboard.sc-corp.net/#/services/caching-infra/cloud-resource/caching-infra--t-us-east4--staging/manage?region=us-east4&provider=GOOGLE&project_id=caching-infra): + * Configure kubectl for the cluster. + * Delete existing runner using [deployment manifest](https://github.sc-corp.net/Snapchat/keydb-internal/github-action-runner-docker/deployment.yaml): + ``` + kubectl delete -f deployment.yaml + ``` + * Start a new runner: + ``` + kubectl apply -f deployment.yaml + ``` + Deployment manifest uses [gcr.io/caching-infra/keydb-github-action-runner](https://console.cloud.google.com/gcr/images/caching-infra/global/keydb-github-action-runner?project=caching-infra). + If you specified another image name in build script, update deployment manifest with an appropriate image. +8. Validate on [github](https://github.com/EQ-Alpha/KeyDB/settings/actions/runners) that new runner is online. \ No newline at end of file diff --git a/github-action-runner-docker/build-and-publish.sh b/github-action-runner-docker/build-and-publish.sh new file mode 100755 index 000000000..f94502523 --- /dev/null +++ b/github-action-runner-docker/build-and-publish.sh @@ -0,0 +1,80 @@ +#!/bin/bash +set -eu +set -o pipefail + +# Color for displaying error messages +red=`tput setaf 1` +reset=`tput sgr0` + +# Default value for the command line flag +DEFAULT_VERSION="2.287.1" +DEFAULT_IMG="gcr.io/caching-infra/keydb-github-action-runner:latest" +DEFAULT_NAME="internal-runner" +FLAGS_token="" +FLAGS_version=$DEFAULT_VERSION +FLAGS_img=$DEFAULT_IMG +FLAGS_name=$DEFAULT_NAME + + +timestamp() { + date "+%m/%d %H:%M:%S" +} + +print_usage() { + echo "Usage:" + echo " ./{script} [flags]" + echo "" + echo "Flags:" + echo " -t, --token: Token taken from onboarding script from github. (required)" + echo " -i, --image: Full docker image name you want to build and push. Default value is ${DEFAULT_IMG}" + echo " -v, --version: Action runner version. Can be taked from onboarding script from github. Default version is ${DEFAULT_VERSION}." + echo " -n, --name: Action runner name. Default name is ${DEFAULT_NAME}." +} + +# Processing flags +while [ ! $# -eq 0 ] +do + # The shift below ensures the unprocessed flag is always at $1 + case "$1" in + --help | -h) + print_usage + exit 0 + ;; + --image | -i) + FLAGS_img="$2" + shift + ;; + --token | -t) + FLAGS_token="$2" + shift + ;; + --version | -v) + FLAGS_version="$2" + shift + ;; + --name | -n) + FLAGS_name="$2" + shift + ;; + *) + echo "${red}" + echo "Unrecognized flag: $1." + echo "Run with '--help' flag to see the supported flags." + echo "${reset}" + exit 1 + ;; + esac + shift +done + +if [[ ${FLAGS_token} == "" ]]; then + echo "${red} ERROR:token is missing" + echo ${reset} + print_usage + exit 1 +fi + +echo "Building image ${FLAGS_img} ..." +docker build --build-arg TOKEN=${FLAGS_token} --build-arg RUNNER_VERSION=${FLAGS_version} --build-arg NAME=${FLAGS_name} -t ${FLAGS_img} . +echo "Pushing image ${FLAGS_img}...." +docker push ${FLAGS_img} diff --git a/github-action-runner-docker/deployment.yaml b/github-action-runner-docker/deployment.yaml new file mode 100644 index 000000000..0974fb00a --- /dev/null +++ b/github-action-runner-docker/deployment.yaml @@ -0,0 +1,19 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: keydb-runner +spec: + replicas: 1 + selector: + matchLabels: + app: keydb-runner + template: + metadata: + labels: + app: keydb-runner + spec: + containers: + - name: keydb-runner + image: gcr.io/caching-infra/keydb-github-action-runner:latest + # Since it's 'latest' tag we're reusing it. + imagePullPolicy: Always