一个通用的 github runner 定义。可按需定制为自己想要的

# hostname
$host_name = "XXX"
$host_ip = "XXX"
$runner_token = "XXX"

Vagrant.configure("2") do |config|
  
  config.vm.box = "ubuntu/jammy64"
  config.vm.box_url = "https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/jammy/current/jammy-server-cloudimg-amd64-vagrant.box"

  config.vm.hostname = $host_name
  config.vm.network "private_network", ip: $host_ip

  # spec config
  config.vm.provider :virtualbox do |vbox|
    vbox.name    = $host_name
    vbox.cpus    = 4
    vbox.memory  = 8000
  end

  # init shell, run as root
  config.vm.provision "shell", path: "run_as_root.sh"
  # init shell, run as user
  config.vm.provision "shell", privileged: false, path: "run_as_user.sh", args: [$runner_token]
end

以root身份执行的动作:

# 启用ssh密码认证
echo "[Step 1] Enable ssh password authentication"
sed -i 's/^PasswordAuthentication .*/PasswordAuthentication yes/' /etc/ssh/sshd_config
echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config
systemctl reload sshd

# ssh以root用户登陆,需要重置root密码
echo "[Step 2] change root password"
echo "root:123456" | sudo chpasswd

# apt install apps
echo "[Step 3] apt install apps"
sed -i '1i deb http://cn.archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse' /etc/apt/sources.list
sed -i '1i deb http://cn.archive.ubuntu.com/ubuntu/ jammy-updates multiverse' /etc/apt/sources.list
sed -i '1i deb http://cn.archive.ubuntu.com/ubuntu/ jammy multiverse' /etc/apt/sources.list
sed -i '1i deb http://cn.archive.ubuntu.com/ubuntu/ jammy-updates universe' /etc/apt/sources.list
sed -i '1i deb http://cn.archive.ubuntu.com/ubuntu/ jammy universe' /etc/apt/sources.list
sed -i '1i deb http://cn.archive.ubuntu.com/ubuntu/ jammy-updates main restricted' /etc/apt/sources.list
sed -i '1i deb http://cn.archive.ubuntu.com/ubuntu/ jammy main restricted' /etc/apt/sources.list
apt-get update
apt-get install -y \
    ca-certificates \
    curl \
    gnupg \
    lsb-release \
    mysql-client
mkdir -m 0755 -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# add docker registry mirror
# echo "[Step 4] add docker registry mirror"
tee /etc/docker/daemon.json <<-EOF
{
    "registry-mirrors": ["https://registry.docker-cn.com","http://hub-mirror.c.163.com"]
}
EOF

# docker cmd need no root
echo "[Step 5] docker cmd need no root"
usermod -aG docker vagrant

以普通用户身份执行的动作:

# install and config github action runner
echo "[Step 6] install and config github action runner"
mkdir actions-runner && cd actions-runner
curl -o ./actions-runner-linux-x64-2.302.1.tar.gz -L https://github.com/actions/runner/releases/download/v2.302.1/actions-runner-linux-x64-2.302.1.tar.gz
tar xzf ./actions-runner-linux-x64-2.302.1.tar.gz
./config.sh --url https://github.com/AutoMQ --token $1 --unattended
nohup ./run.sh > runner.log 2>&1 &