CI/CD時間: x分/月
プライベートリポジトリではプランに応じた時間以上のCI/CDはできなくなってしまうので、セルフホステッド ランナーを追加したい
環境
AWSにAmazon Linux 2023のEC2インスタンスを起動
アーキテクチャ:Arm(64bit) ※安いGravitonインスタンスタイプを選択したいため
AMI ID:ami-0d60652e0357ad94f
AMI 名:al2023-ami-2023.6.20250218.2-kernel-6.1-arm64
手順
Amazon Linux 2023に不足しているパッケージを先にインストールする
※ubuntuなどでホストする場合はプリインストールされているため不要
sudo yum install perl-Digest-SHA -y
sudo yum install libicu -y
パッケージがインストールされていない場合、後項の手順でコマンド実行時以下のエラーになる
$ echo "a96b0cec7b0237ca5e4210982368c6f7d8c2ab1e5f6b2604c1ccede9cedcb143 actions-runner-linux-arm64-2.322.0.tar.gz" | shasum -a 256 -c
-bash: shasum: command not found
$ ./config.sh --url https://github.com/example-org/example-app --token ABCDEFGHIJKLMNOPQRSTUVWXYZ123
Libicu's dependencies is missing for Dotnet Core 6.0
Execute sudo ./bin/installdependencies.sh to install any missing Dotnet Core 6.0 dependencies.
ここからは、以下の手順通りに進める※設定するリポジトリの「設定」ページを開く権限が必要
まずは、GitHub上でリポジトリの設定ページを開き、「New self-hosted runner」
をクリックした後、OSとアーキテクチャを選択すると、一時トークンを含むコマンドが表示されるので順番に実行する
※今回はコンフィグを全てデフォルトで設定
$ mkdir actions-runner && cd actions-runner
$ curl -o actions-runner-linux-arm64-2.322.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.322.0/actions-runner-linux-arm64-2.322.0.tar.gz
$ echo "a96b0cec7b0237ca5e4210982368c6f7d8c2ab1e5f6b2604c1ccede9cedcb143 actions-runner-linux-arm64-2.322.0.tar.gz" | shasum -a 256 -c
actions-runner-linux-arm64-2.322.0.tar.gz: OK
$ tar xzf ./actions-runner-linux-arm64-2.322.0.tar.gz
$ ./config.sh --url https://github.com/example-org/example-app --token ABCDEFGHIJKLMNOPQRSTUVWXYZ123
--------------------------------------------------------------------------------
| ____ _ _ _ _ _ _ _ _ |
| / ___(_) |_| | | |_ _| |__ / \ ___| |_(_) ___ _ __ ___ |
| | | _| | __| |_| | | | | '_ \ / _ \ / __| __| |/ _ \| '_ \/ __| |
| | |_| | | |_| _ | |_| | |_) | / ___ \ (__| |_| | (_) | | | \__ \ |
| \____|_|\__|_| |_|\__,_|_.__/ /_/ \_\___|\__|_|\___/|_| |_|___/ |
| |
| Self-hosted runner registration |
| |
--------------------------------------------------------------------------------
# Authentication
√ Connected to GitHub
# Runner Registration
Enter the name of the runner group to add this runner to: [press Enter for Default]
Enter the name of runner: [press Enter for ip-10-0-0-5]
This runner will have the following labels: 'self-hosted', 'Linux', 'ARM64'
Enter any additional labels (ex. label-1,label-2): [press Enter to skip]
√ Runner successfully added
√ Runner connection is good
# Runner settings
Enter name of work folder: [press Enter for _work]
√ Settings Saved.
なお、一時トークンの有効期限はこのページを開いて新規に作成されてから1時間なようで、1時間以上経過したトークンでコンフィグ実行しようとすると404エラーが発生する
$ ./config.sh --url https://github.com/example-org/example-app --token ABCDEFGHIJKLMNOPQRSTUVWXYZ123
Http response code: NotFound from 'POST https://api.github.com/actions/runner-registration'
{"message":"Not Found","documentation_url":"https://docs.github.com/rest"}
Response status code does not indicate success: 404 (Not Found).
./run.sh
を実行し、接続が表示されればOK
$ ./run.sh
√ Connected to GitHub
Current runner version: '2.322.0'
2025-02-28 10:41:49Z: Listening for Jobs
GitHub上のRunners画面に戻るとランナーが追加されていることが確認できる
次に、./run.sh
ではターミナルが切れると使えないので、サービス化する
インスタンスを起動停止しても自動起動されるよう設定し、起動
sudo ./svc.sh install
sudo ./svc.sh start
ステータスの確認
$ sudo ./svc.sh status
/etc/systemd/system/actions.runner.example-org-example-app.ip-10-0-0-5.service
● actions.runner.example-org-example-app.ip-10-0-0-5.service - GitHub Actions Runner (example-org-example-app.ip-10-0-0-5)
Loaded: loaded (/etc/systemd/system/actions.runner.example-org-example-app.ip-10-0-0-5.service; enabled; preset: disabled)
Active: active (running) since Fri 2025-02-28 11:06:03 UTC; 28s ago
Main PID: 26785 (runsvc.sh)
Tasks: 21 (limit: 1002)
Memory: 46.1M
CPU: 2.118s
CGroup: /system.slice/actions.runner.example-org-example-app.ip-10-0-0-5.service
tq26785 /bin/bash /home/ec2-user/actions-runner/runsvc.sh
tq26787 ./externals/node20/bin/node ./bin/RunnerService.js
mq26795 /home/ec2-user/actions-runner/bin/Runner.Listener run --st …
Feb 28 11:06:03 ip-10-0-0-5.ap-northeast-1.compute.internal systemd[1]: Start …
Feb 28 11:06:03 ip-10-0-0-5.ap-northeast-1.compute.internal runsvc.sh[26785]: ...
Feb 28 11:06:03 ip-10-0-0-5.ap-northeast-1.compute.internal runsvc.sh[26787]: ...
Feb 28 11:06:03 ip-10-0-0-5.ap-northeast-1.compute.internal runsvc.sh[26787]: ...
Feb 28 11:06:03 ip-10-0-0-5.ap-northeast-1.compute.internal runsvc.sh[26787]: ...
Feb 28 11:06:06 ip-10-0-0-5.ap-northeast-1.compute.internal runsvc.sh[26787]: ...
Feb 28 11:06:07 ip-10-0-0-5.ap-northeast-1.compute.internal runsvc.sh[26787]: ...
Feb 28 11:06:07 ip-10-0-0-5.ap-northeast-1.compute.internal runsvc.sh[26787]: ...
Hint: Some lines were ellipsized, use -l to show in full.
最後に、セルフホステッド ランナーの準備は出来たが、Github Actionsで実際に使うためにはリポジトリ内のワークフローファイルもセルフホステッド ランナーを使うよう指定する必要があるので、追記してリポジトリにpushする
# Use this YAML in your workflow file for each job
runs-on: self-hosted