大根's ITブログ

ITとか開発ツールとかビジネスとか色々

GitHub Actions self hosted runnerをやる

やります。

環境作り

GitHubリポジトリSettings > Actions > Runners > New self-hosted runner を選択。

WSL2のUbuntuに入れるのでLinuxを選択。基本は指示にしたがって構築していく。

# Create a folder
$ mkdir actions-runner && cd actions-runner
# Download the latest runner package
$ curl -o actions-runner-linux-x64-2.295.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.295.0/actions-runner-linux-x64-2.295.0.tar.gz
# Optional: Validate the hash
$ echo "a80c1ab58be3cd4920ac2e51948723af33c2248b434a8a20bd9b3891ca4000b6  actions-runner-linux-x64-2.295.0.tar.gz" | shasum -a 256 -c
# Extract the installer
$ tar xzf ./actions-runner-linux-x64-2.295.0.tar.gz

config.shはすべてデフォルト設定とした。

$ ./config.sh --url https://github.com/xxx/yyy --token ******************

--------------------------------------------------------------------------------
|        ____ _ _   _   _       _          _        _   _                      |
|       / ___(_) |_| | | |_   _| |__      / \   ___| |_(_) ___  _ __  ___      |
|      | |  _| | __| |_| | | | | '_ \    / _ \ / __| __| |/ _ \| '_ \/ __|     |
|      | |_| | | |_|  _  | |_| | |_) |  / ___ \ (__| |_| | (_) | | | \__ \     |
|       \____|_|\__|_| |_|\__,_|_.__/  /_/   \_\___|\__|_|\___/|_| |_|___/     |
|                                                                              |
|                       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 DESKTOP-*******]

This runner will have the following labels: 'self-hosted', 'Linux', 'X64'
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.

runする。

$ ./run.sh

√ Connected to GitHub

Current runner version: '2.295.0'
2022-08-20 04:25:51Z: Listening for Jobs

これでRunnersに追加される。

一度runを終了するとstatusがofflineになる。

とりあえずlsしてみる。

$ pwd
/home/<username>/actions-runner
$ ls
_diag                                    config.sh  run-helper.cmd.template  run.sh
actions-runner-linux-x64-2.295.0.tar.gz  env.sh     run-helper.sh            safe_sleep.sh
bin                                      externals  run-helper.sh.template   svc.sh

GitHub側でいったんRunnerをRemoveしてみる。

こんな画面が出てくる。どうもコマンドで削除した方が良いらしい。

画面に出てきたコマンドを投入。

./config.sh remove --token *********************

# Runner removal


√ Runner removed successfully
√ Removed .credentials
√ Removed .runner

$ ls
_diag                                    config.sh  run-helper.cmd.template  run.sh
actions-runner-linux-x64-2.295.0.tar.gz  env.sh     run-helper.sh            safe_sleep.sh
bin                                      externals  run-helper.sh.template   svc.sh

なんか見た目は変わっていない。.runnerとかの隠しフォルダが消えたのかな。

試しにrunしてみる。予定通りエラーとなる。

$ ./run.sh
An error occurred: Not configured. Run config.(sh/cmd) to configure the runner.
Runner listener exit with terminated error, stop the service, no retry needed.
Exiting runner...

GitHub側も消えている。

一度全消しして世界をもう一度始めよう。

$ mkdir actions-runner && cd actions-runner
$ ls -a

$ curl -o actions-runner-linux-x64-2.295.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.295.0/actions-runner-linux-x64-2.295.0.tar.gz
$ ls
actions-runner-linux-x64-2.295.0.tar.gz

$ tar xzf ./actions-runner-linux-x64-2.295.0.tar.gz
$ ls -a
.   actions-runner-linux-x64-2.295.0.tar.gz  config.sh  externals                run-helper.sh.template  safe_sleep.sh
..  bin                                      env.sh     run-helper.cmd.template  run.sh

$ ./config.sh --url https://github.com/xxxx/yyyy --token *****************
(省略)//全部デフォルト

$ ls -a
.                       .env     actions-runner-linux-x64-2.295.0.tar.gz  externals                safe_sleep.sh
..                      .path    bin                                      run-helper.cmd.template  svc.sh
.credentials            .runner  config.sh                                run-helper.sh.template
.credentials_rsaparams  _diag    env.sh                                   run.sh

config.shを叩くと svc.sh や隠しファイルが出力される。

そしてGitHub側でRunnerが出てくる。

とりあえずrunを叩く。

$ ./run.sh

√ Connected to GitHub

Current runner version: '2.295.0'
2022-08-20 05:01:15Z: Listening for Jobs

この状態で動くか確認する。.github/workflows の yml の runs-onself-hosted に変更してpush。

    #runs-on: ubuntu-latest
    runs-on: self-hosted

無事動作した。

$ ls
_diag                                    bin        externals                run-helper.sh.template  svc.sh
_work                                    config.sh  run-helper.cmd.template  run.sh
actions-runner-linux-x64-2.295.0.tar.gz  env.sh     run-helper.sh            safe_sleep.sh

サービス化

サービス化する場合はsvc.shを用いる。config.sh

coming soon