GitHubでセルフホステッドランナーを使いたい

記事タイトルとURLをコピーする

この記事は約3分で読めます。

こんばんは、SWX3人目の熊谷(悠)です。

弊社ではGithub Teamプランを契約しています。
プライベートリポジトリでは以下時間以上のCI/CDはできなくなってしまうので、セルフホステッド ランナーを追加しようと思います

CI/CD時間: 3,000分/月
https://github.co.jp/pricing.html

https://docs.github.com/ja/actions/using-github-hosted-runners/using-github-hosted-runners/about-github-hosted-runners

環境

AWSにubuntuのEC2インスタンスを起動します

アーキテクチャ:Arm(64bit) ※安いGravitonインスタンスタイプを選択したいため
AMI ID:ami-007bc34ad4cc33773
AMI 名:ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-arm64-server-20240801

手順

以下の手順通りに進めます※設定するリポジトリの、設定ページを開く権限が必要

https://docs.github.com/ja/actions/hosting-your-own-runners/managing-self-hosted-runners/adding-self-hosted-runners

GitHub上でリポジトリの設定ページを開き、New self-hosted runnerをクリックした後、OSとアーキテクチャを選択すると、一時トークンを含むコマンドが表示されるので順番に実行します
※今回はコンフィグを全てデフォルトで設定

$ mkdir actions-runner; cd actions-runner

$ curl -o actions-runner-linux-arm64-2.319.1.tar.gz -L https://github.com/actions/runner/releases/download/v2.319.1/actions-runner-linux-arm64-2.319.1.tar.gz

$ echo "03d993c65e0c4daa5e3bf5a5a35ba356f363bdb5ceb6b5808fd52fdb813dd8e8  actions-runner-linux-arm64-2.319.1.tar.gz" | shasum -a 256 -c
actions-runner-linux-arm64-2.319.1.tar.gz: OK

$ tar xzf ./actions-runner-linux-arm64-2.319.1.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-1-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).

https://github.com/actions/runner/issues/1882?ref=pangyoalto.com#issuecomment-1124701081

./run.shを実行し、接続が表示されればOKです

$ ./run.sh

√ Connected to GitHub

Current runner version: '2.319.1'
2024-09-11 06:24:47Z: Listening for Jobs

GitHub上のRunners画面に戻るとランナーが追加されていることが確認できます

image.png

次に、./run.shではターミナルが切れると使えないので、サービス化します

https://docs.github.com/ja/actions/hosting-your-own-runners/managing-self-hosted-runners/configuring-the-self-hosted-runner-application-as-a-service

インスタンスを起動停止しても自動起動されるよう設定します

sudo ./svc.sh install
sudo ./svc.sh start
sudo ./svc.sh enable

ステータスを確認します

$ sudo ./svc.sh status

/etc/systemd/system/actions.runner.example-org-example-app.ip-10-0-1-5.service
● actions.runner.example-org-example-app.ip-10-0-1-5.service - GitHub Actions Runner (example-org-example-app.ip-10-0-1-5)
     Loaded: loaded (/etc/systemd/system/actions.runner.example-org-example-app.ip-10-0-1-5.service; enabled; preset: enabled)
     Active: active (running) since Wed 2024-09-11 06:26:01 UTC; 14s ago
   Main PID: 1442 (runsvc.sh)
      Tasks: 23 (limit: 9302)
     Memory: 51.6M (peak: 51.8M)
        CPU: 2.074s
     CGroup: /system.slice/actions.runner.example-org-example-app.ip-10-0-1-5.service
             tq1442 /bin/bash /home/ubuntu/actions-runner/runsvc.sh
             tq1445 ./externals/node16/bin/node ./bin/RunnerService.js
             mq1452 /home/ubuntu/actions-runner/bin/Runner.Listener run --start …

Sep 11 06:26:01 ip-10-0-1-5 systemd[1]: Started actions.runner.serverworks…42).
Sep 11 06:26:01 ip-10-0-1-5 runsvc.sh[1442]: .path=/usr/local/sbin:/usr/loc…bin
Sep 11 06:26:01 ip-10-0-1-5 runsvc.sh[1445]: Starting Runner listener with …ice
Sep 11 06:26:01 ip-10-0-1-5 runsvc.sh[1445]: Started listener process, pid:…452
Sep 11 06:26:01 ip-10-0-1-5 runsvc.sh[1445]: Started running service
Sep 11 06:26:04 ip-10-0-1-5 runsvc.sh[1445]: √ Connected to GitHub
Sep 11 06:26:05 ip-10-0-1-5 runsvc.sh[1445]: Current runner version: '2.319.1'
Sep 11 06:26:05 ip-10-0-1-5 runsvc.sh[1445]: 2024-09-11 06:26:05Z: Listenin…obs
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

https://docs.github.com/ja/actions/hosting-your-own-runners/managing-self-hosted-runners/using-self-hosted-runners-in-a-workflow