Terraform で EC2 の user_data に突っ込んで、自動構築した。
gh-action.tf
resource "aws_security_group" "runner" {
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_instance" "runner" {
ami = "ami-0278fe6949f6b1a06" # ubuntu 18.04
instance_type = "t2.small"
vpc_security_group_ids = [aws_security_group.runner.id]
user_data = file("./ec2-scripts/init-gh-action-runner.sh")
}
./ec2-scripts/init-gh-action-runner.sh
#!/bin/bash
set -ue
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo usermod -aG docker ubuntu
mkdir actions-runner
cd actions-runner
curl -O -L https://github.com/actions/runner/releases/download/v2.169.1/actions-runner-linux-x64-2.169.1.tar.gz
tar xzf ./actions-runner-linux-x64-2.169.1.tar.gz
chown -R ubuntu: /actions-runner
sudo -i -u ubuntu /actions-runner/config.sh \
--url https://github.com/org/repo \
--token AAAAAAAAAAAAAAAAAAAAAAAAAAAAA \
--name $HOSTNAME \
--work _work \
--unattended \
--replace
/actions-runner/svc.sh install
/actions-runner/svc.sh start
TODO: GitHub Actions のトークンの作成を自動化したい
参考: https://github.com/myoung34/docker-github-actions-runner