Skip to main content Link Menu Expand (external link) Document Search Copy Copied

created at 2024-12-30

보통 하나의 instance 에서 하나의 runner 만 추가하는 경우가 많습니다. runner 은 하나의 job 만 처리가능해서 matrix 를 사용하여 여러개의 job 을 병렬처리할 수 없죠.

디렉토리가 다르면 runner 를 여러개 추가할 수 있습니다. job 병렬수행을 위해 디렉토리 나누고 설치하고 등등 수동으로 하기 귀찮아서 스크립트로 작성된 거 가져왔습니다.

Table of contents

  1. 1. 하나의 빌드머신에 여러개 Self-Hosted Runner 추가하는 스크립트
  2. 2. self-hosted 지우는 스크립트

1. 하나의 빌드머신에 여러개 Self-Hosted Runner 추가하는 스크립트

ubuntu@ip-10-70-14-134:~$ cat add-runner.sh 
#!/bin/bash
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 <runner-number> <github-token>"
    exit 1
fi
RUNNER_NUMBER=$1
TOKEN=$2
RUNNER_DIR="/home/$USER/actions-runner-${RUNNER_NUMBER}"
SERVICE_FILE="/tmp/github-actions-${RUNNER_NUMBER}.service"

# Remove existing directory if exists
rm -rf "$RUNNER_DIR"

# Create fresh runner directory
mkdir -p "$RUNNER_DIR"
cd "$RUNNER_DIR" || exit

# Download and extract runner
curl -o actions-runner-linux-x64-2.321.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.321.0/actions-runner-linux-x64-2.321.0.tar.gz
tar xzf ./actions-runner-linux-x64-2.321.0.tar.gz

# Configure runner
./config.sh --url 'git repo 주소' \
    --token "$TOKEN" \
    --name "runner-${RUNNER_NUMBER}" \
    --unattended \
    --replace

# Create service file in temp location
cat > "$SERVICE_FILE" << EOL
[Unit]
Description=GitHub Actions Runner ${RUNNER_NUMBER}
After=network.target

[Service]
Type=simple
User=${USER}
Group=${USER}
WorkingDirectory=${RUNNER_DIR}
ExecStart=${RUNNER_DIR}/run.sh
Restart=always
RestartSec=10
KillSignal=SIGTERM

[Install]
WantedBy=multi-user.target
EOL

# Move service file and configure systemd (these need sudo)
sudo mv "$SERVICE_FILE" "/etc/systemd/system/"
sudo systemctl daemon-reload
sudo systemctl enable "github-actions-${RUNNER_NUMBER}"
sudo systemctl start "github-actions-${RUNNER_NUMBER}"

echo "Runner ${RUNNER_NUMBER} has been created and started"

위의 스크립트 작성 후 아래와 같이 실행하면 하나둘씩 추가됩니다.

sh add-runner.sh 1 {github-config-token}
sh add-runner.sh 2 {github-config-token}
...

img.png

2. self-hosted 지우는 스크립트

ubuntu@ip-10-70-14-134:~$ cat remove-runner.sh 
#!/bin/bash

if [ "$#" -ne 2 ]; then
    echo "Usage: $0 <runner-number> <github-token>"
    exit 1
fi

RUNNER_NUMBER=$1
TOKEN=$2
RUNNER_DIR="/home/$USER/actions-runner-${RUNNER_NUMBER}"
SERVICE_FILE="/etc/systemd/system/github-actions-${RUNNER_NUMBER}.service"

# Stop and disable systemd service
echo "Stopping and disabling service for runner-${RUNNER_NUMBER}..."
sudo systemctl stop "github-actions-${RUNNER_NUMBER}"
sudo systemctl disable "github-actions-${RUNNER_NUMBER}"
sudo rm "$SERVICE_FILE"
sudo systemctl daemon-reload

# Remove GitHub Runner registration
if [ -d "$RUNNER_DIR" ]; then
    echo "Removing GitHub Actions Runner registration..."
    cd "$RUNNER_DIR" || exit
    ./config.sh remove --token "$TOKEN"
fi

# Remove runner directory
echo "Deleting runner directory: $RUNNER_DIR"
rm -rf "$RUNNER_DIR"

echo "Runner ${RUNNER_NUMBER} has been successfully removed."

img_1.png