Skip to content

Instantly share code, notes, and snippets.

@dahoba
Last active January 10, 2024 09:26
Show Gist options
  • Save dahoba/7604222e437c9817a6258c4c5111d7e2 to your computer and use it in GitHub Desktop.
Save dahoba/7604222e437c9817a6258c4c5111d7e2 to your computer and use it in GitHub Desktop.
Github runner self-hosted userdata for ec2 AL2023 AMI
#!/bin/bash
# userdata.sh for ec2 AL2023 AMI
# to create an instance of Github self-hosted runner under ${GITHUB_ORG}
RCLONE_VERSION=1.62.2
ACTION_RUNNER_VERSION=2.311.0
TARGET_ARCH=arm64
GITHUB_ORG="my-org"
# user must be owner of the org
PAT="ghp_XX"
yum update -y
yum -y install yum-utils fuse unzip dotnet
# Download docker engine community
systemctl docker start
usermod -a -G docker ec2-user
systemctl enable docker
# Setup docker rclone plugin
curl -SLO https://downloads.rclone.org/v$RCLONE_VERSION/rclone-v$RCLONE_VERSION-linux-$TARGET_ARCH.zip
unzip rclone-v$RCLONE_VERSION-linux-$RCLONE_ARCH.zip
cd rclone-v$RCLONE_VERSION-linux-$RCLONE_ARCH
sudo cp rclone /usr/bin/
sudo chmod 755 /usr/bin/rclone
mkdir -p /var/lib/docker-volumes/rclone
mkdir -p /var/lib/docker-plugins/rclone/config
mkdir -p /var/lib/docker-plugins/rclone/cache
cat <<EOF | sudo tee /etc/systemd/system/docker-volume-rclone.service
[Unit]
Description=Docker Volume Plugin for rclone
Requires=docker.service
Before=docker.service
After=network.target
Requires=docker-volume-rclone.socket
After=docker-volume-rclone.socket
[Service]
ExecStart=/usr/bin/rclone serve docker
ExecStartPre=/bin/mkdir -p /var/lib/docker-volumes/rclone
ExecStartPre=/bin/mkdir -p /var/lib/docker-plugins/rclone/config
ExecStartPre=/bin/mkdir -p /var/lib/docker-plugins/rclone/cache
Environment=RCLONE_CONFIG=/var/lib/docker-plugins/rclone/config/rclone.conf
Environment=RCLONE_CACHE_DIR=/var/lib/docker-plugins/rclone/cache
Environment=RCLONE_VERBOSE=1
Environment=RCLONE_S3_ENV_AUTH=false
[Install]
WantedBy=multi-user.target
EOF
cat <<EOF | sudo tee /etc/systemd/system/docker-volume-rclone.socket
[Unit]
Description=Docker Volume Plugin for rclone
[Socket]
ListenStream=/run/docker/plugins/rclone.sock
[Install]
WantedBy=sockets.target
EOF
# remove when deployment
cat <<EOF | sudo tee /var/lib/docker-plugins/rclone/config/rclone.conf
[s3]
type = s3
provider = AWS
access_key_id = <replace-me>
secret_access_key = <replace-me>
region = ap-southeast-1
location_constraint = ap-southeast-1
acl = private
no_check_bucket = true
EOF
docker plugin install rclone/docker-volume-rclone:$TARGET_ARCH-$RCLONE_VERSION --grant-all-permissions --alias rclone
systemctl daemon-reload
systemctl start docker-volume-rclone.service
systemctl enable docker-volume-rclone.socket
systemctl restart docker
# End setup docker rclone plugin
# Setup NodeJS v16 LTS;
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
. ~/.nvm/nvm.sh
nvm install --lts=Gallium
# Setup Github runner and join the self-hosted runner pools
#
# Create and move to the working directory
mkdir /actions-runner && cd /actions-runner
# Download the latest runner package
curl -o actions-runner-linux-${TARGET_ARCH}-${ACTION_RUNNER_VERSION}.tar.gz -L https://github.com/actions/runner/releases/download/v${ACTION_RUNNER_VERSION}/actions-runner-linux-${TARGET_ARCH}-${ACTION_RUNNER_VERSION}.tar.gz
# Extract the installer
tar xzf ./actions-runner-linux-${TARGET_ARCH}-${ACTION_RUNNER_VERSION}.tar.gz
# Change the owner of the directory to ec2-user
chown ec2-user -R /actions-runner
# Get the runner's token
token=$(curl -s -X POST -H "authorization: token $PAT" https://api.github.com/orgs/$GITHUB_ORG/actions/runners/registration-token | jq -r .token)
# Create the runner and start the configuration experience
sudo -u ec2-user ./config.sh --url https://github.com/$GITHUB_ORG --token $token --name "ec2-spot-runner" --replace --unattended
# Create the runner's service
./svc.sh install
# Start the service
./svc.sh start
# Install the docker compose,buildx plugin manually
DOCKER_CONFIG=/home/ec2-user/.docker
# DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
# sudo -u mkdir -p $DOCKER_CONFIG/cli-plugins
# sudo -u curl -SL https://github.com/docker/compose/releases/download/v2.18.1/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose
# sudo -u curl -SL https://github.com/docker/buildx/releases/download/v0.11.0/buildx-v0.11.0.linux-amd64 -o $DOCKER_CONFIG/cli-plugins/docker-buildx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment