|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +RUNNER_NAME=${RUNNER_NAME:-$(hostname)} |
| 4 | +RUNNER_ORG=${RUNNER_ORG:-"org"} |
| 5 | +RUNNER_LABELS=${RUNNER_LABELS:-"runner"} |
| 6 | +RUNNER_TOKEN=${RUNNER_TOKEN:-"token"} |
| 7 | +RUNNER_GROUP=${RUNNER_GROUP:-"default"} |
| 8 | +RUNNER_WORKDIR=${RUNNER_WORKDIR:-"_work"} |
| 9 | +RUNNER_DOWNLOAD_URL=${RUNNER_DOWNLOAD_URL:-"https://github.com/actions/runner/releases/download/v2.308.0/actions-runner-linux-x64-2.308.0.tar.gz"} |
| 10 | +RUNNER_FILE=${RUNNER_FILE:-$(basename "${RUNNER_DOWNLOAD_URL}")} |
| 11 | +LSB_RELEASE_CS=${LSB_RELEASE_CS:-$(lsb_release -cs))} |
| 12 | + |
| 13 | +source /etc/os-release |
| 14 | +LINUX_OS=${ID} |
| 15 | +LINUX_OS_VERSION=$(echo "${VERSION_ID}" | sed -E 's/^([0-9]+)\..*$/\1/') |
| 16 | +DOCKER_SERVICE_START="yes" |
| 17 | + |
| 18 | +SSH_KEYS=${SSH_KEYS:-""} |
| 19 | + |
| 20 | +sudo groupadd -f docker |
| 21 | +sudo useradd -m actions |
| 22 | +sudo usermod -aG docker,root actions |
| 23 | +sudo bash -c "echo 'actions ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers" |
| 24 | +sudo -H -u actions bash -c 'mkdir -p /home/actions/.ssh' |
| 25 | +sudo -H -u actions bash -c 'echo "${SSH_KEYS}" >> /home/actions/.ssh/authorized_keys' |
| 26 | + |
| 27 | +if [[ ${LINUX_OS} == "ubuntu" ]]; then |
| 28 | + sudo apt-get -y update |
| 29 | + sudo DEBIAN_FRONTEND=noninteractive apt-get -y install apt-transport-https \ |
| 30 | + ca-certificates \ |
| 31 | + curl \ |
| 32 | + gnupg \ |
| 33 | + lsb-release |
| 34 | +elif [[ ${LINUX_OS} == "centos" ]] || [[ ${LINUX_OS} == "rocky" ]] || [[ ${LINUX_OS} == "almalinux" ]]; then |
| 35 | + sudo yum install -y bind-utils yum-utils |
| 36 | +elif [[ ${LINUX_OS} == "rhel" ]]; then |
| 37 | + sudo bash -c 'cat <<EOF > /etc/systemd/system/redhat_registration.service |
| 38 | +[Unit] |
| 39 | +Description=Redhat registration |
| 40 | +After=network-online.target |
| 41 | +
|
| 42 | +[Service] |
| 43 | +Type=oneshot |
| 44 | +RemainAfterExit=true |
| 45 | +TimeoutStartSec=300 |
| 46 | +ExecStart=/sbin/subscription-manager register --username={{ redhat_username }} --password={{ redhat_password }} --auto-attach |
| 47 | +TimeoutStopSec=300 |
| 48 | +ExecStop=-/sbin/subscription-manager unregister |
| 49 | +
|
| 50 | +[Install] |
| 51 | +WantedBy=multi-user.target |
| 52 | +EOF' |
| 53 | + sudo chmod 600 /etc/systemd/system/redhat_registration.service |
| 54 | + sudo systemctl daemon-reload |
| 55 | + sudo systemctl enable redhat_registration.service |
| 56 | + sudo systemctl start redhat_registration.service |
| 57 | +else |
| 58 | + echo "OS not managed by the runner-manager" |
| 59 | + exit 1 |
| 60 | +fi |
| 61 | + |
| 62 | +if [[ ! ${RUNNER_LABELS} =~ "no-docker" ]]; then |
| 63 | + |
| 64 | + if [[ ${LINUX_OS} == "ubuntu" ]]; then |
| 65 | + sudo apt-get -y update |
| 66 | + curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /tmp/docker.gpg |
| 67 | + sudo cat /tmp/docker.gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg || true |
| 68 | + echo \ |
| 69 | + "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ |
| 70 | + ${LSB_RELEASE_CS} stable" | sudo tee /etc/apt/sources.list.d/docker.list >/dev/null |
| 71 | + sudo apt-get update --yes --force-yes |
| 72 | + sudo apt-get install --yes --force-yes docker-ce docker-ce-cli containerd.io |
| 73 | + elif [[ ${LINUX_OS} == "centos" ]] || [[ ${LINUX_OS} == "rocky" ]] || [[ ${LINUX_OS} == "almalinux" ]]; then |
| 74 | + sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo |
| 75 | + sudo yum install -y epel-release docker-ce docker-ce-cli containerd.io |
| 76 | + elif [[ ${LINUX_OS} == "rhel" ]]; then |
| 77 | + if [[ ${LINUX_OS_VERSION} == "7" ]]; then |
| 78 | + # Enable repos to install docker |
| 79 | + sudo mkdir /etc/docker/ |
| 80 | + # TODO: make dns config a setting for the runner |
| 81 | + sudo bash -c 'cat > /etc/docker/daemon.json << EOF |
| 82 | + { |
| 83 | + "dns": ["10.100.1.1", "10.100.1.2", "10.100.1.3"] |
| 84 | + } |
| 85 | + EOF' |
| 86 | + |
| 87 | + sudo subscription-manager repos --enable=rhel-7-server-extras-rpms --enable=rhel-7-server-optional-rpms |
| 88 | + sudo yum install -y docker |
| 89 | + elif [[ ${LINUX_OS_VERSION} == "8" || ${LINUX_OS_VERSION} == "9" ]]; then |
| 90 | + sudo dnf install -y podman-docker podman |
| 91 | + DOCKER_SERVICE_START="no" |
| 92 | + else |
| 93 | + echo "RHEL version not managed by the runner-manager" |
| 94 | + exit 1 |
| 95 | + fi |
| 96 | + fi |
| 97 | + |
| 98 | + if [[ ${DOCKER_SERVICE_START} == "yes" ]]; then |
| 99 | + sudo systemctl start docker |
| 100 | + fi |
| 101 | +fi |
| 102 | + |
| 103 | +# Login as actions user so that all the following commands are executed as actions user |
| 104 | +sudo su - actions |
| 105 | +mkdir -p /home/actions/actions-runner |
| 106 | +cd /home/actions/actions-runner || exit |
| 107 | +# Download the runner package |
| 108 | +curl -L "${RUNNER_DOWNLOAD_URL}" -o "/tmp/${RUNNER_FILE}" |
| 109 | +tar xzf /tmp/"${RUNNER_FILE}" |
| 110 | +# install dependencies |
| 111 | +sudo ./bin/installdependencies.sh |
| 112 | +echo "[Unit] |
| 113 | +Description={{Description}} |
| 114 | +After=network.target |
| 115 | +
|
| 116 | +[Service] |
| 117 | +ExecStart=/bin/bash {{RunnerRoot}}/runsvc.sh |
| 118 | +User={{User}} |
| 119 | +WorkingDirectory={{RunnerRoot}} |
| 120 | +KillMode=process |
| 121 | +KillSignal=SIGTERM |
| 122 | +TimeoutStopSec=5min |
| 123 | +
|
| 124 | +[Install] |
| 125 | +WantedBy=multi-user.target" >/home/actions/actions-runner/bin/actions.runner.service.template |
| 126 | + |
| 127 | +./config.sh \ |
| 128 | + --url "https://github.com/${RUNNER_ORG}" \ |
| 129 | + --token "${RUNNER_TOKEN}" \ |
| 130 | + --name "${RUNNER_NAME}" \ |
| 131 | + --work "${RUNNER_WORKDIR}" \ |
| 132 | + --labels "${RUNNER_LABELS}" \ |
| 133 | + --runnergroup "${RUNNER_GROUP}" \ |
| 134 | + --replace \ |
| 135 | + --unattended \ |
| 136 | + --ephemeral |
| 137 | + |
| 138 | +if command -v systemctl; then |
| 139 | + sudo ./svc.sh install |
| 140 | + sudo ./svc.sh start |
| 141 | +else |
| 142 | + nohup /home/actions/actions-runner/run.sh 2>/home/actions/actions-runner/logs & |
| 143 | +fi |
0 commit comments