【Github】Github Actions ~ Self-hosted runners / 入門編 ~

■ はじめに

https://dk521123.hatenablog.com/entry/2021/11/04/142835
https://dk521123.hatenablog.com/entry/2022/06/16/151443
https://dk521123.hatenablog.com/entry/2022/06/21/143624

の続き。

CI/CDをやることになり、Github Actionsを使うらしい。
で、セルフホステッドランナー(Self-hosted runners)ってのが
でてきたので、メモる。

目次

【1】GitHub Actions の Host種類
 1)GitHub hosted runners
 2)Self-hosted runners
【2】設定手順
 1)Github 上での操作
 2)自サーバ(self-hosted)上での操作
 3)ワークフローの実行
【3】使用上の注意
 1)Public Repositoryで利用しない
【4】トラブルシュート
 1)config.sh実行後、エラー「HTTP response code: NotFound from ...」が表示

【1】GitHub Actions の Host種類

* 全然難しい話じゃない。
 => 要は、CI/CDを行うサーバを、
  Githubデフォルトサーバを使うか
  それとも自分で用意するかって話だけ。

1)GitHub hosted runners

* GitHub Actions ワークフローでセットアップする方式

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

2)Self-hosted runners

* 自身でサーバを用意してセットアップする方式

【2】設定手順

1)Github 上での操作

[1] GitHub上の対象リポジトリを選択又は作成する
[2] Settings > Actions > Runners に移動
[3] 「New self-hosted runner」ボタン押下
[4] 対象の環境を選択する(今回は以下を選択)
 * Runner image: Linux
 * Architecture: x64
[5] 以下のような情報が出てくるので、各値を控えておく
 * Download: ダウンロード先(e.g. https://github.com/actions/runner/releases/download/v2.311.0/actions-runner-linux-x64-2.311.0.tar.gz)
 * Configure: --urlと--token

Download

# Create a folder
$ mkdir actions-runner && cd actions-runner

# Download the latest runner package
$ curl -o actions-runner-linux-x64-2.311.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.311.0/actions-runner-linux-x64-2.311.0.tar.gz

# Optional: Validate the hash
$ echo "29fc8cf2dab4c195bb147384e7e2c94cfd4d4022c793b346a6175435265aa278  actions-runner-linux-x64-2.311.0.tar.gz" | shasum -a 256 -c

# Extract the installer
$ tar xzf ./actions-runner-linux-x64-2.311.0.tar.gz

config

# Create the runner and start the configuration experience
$ ./config.sh --url https://github.com/your-user/your-repo --token {YOUR_TOKEN}

# Last step, run it!
$ ./run.sh

2)自サーバ(self-hosted)上での操作

[1] Action Runner の インストール

# Move home
$ cd ~

# Create a folder
$ mkdir /opt/actions-runner && cd /opt/actions-runner

# Download the latest runner package
$ curl -o actions-runner.tar.gz -L https://github.com/actions/runner/releases/download/v2.311.0/actions-runner-linux-x64-2.311.0.tar.gz

# Optional: Validate the hash
$ echo "29fc8cf2dab4c195bb147384e7e2c94cfd4d4022c793b346a6175435265aa278  actions-runner.tar.gz" | shasum -a 256 -c
actions-runner-linux-x64-2.303.0.tar.gz: OK

# Extract the installer
$ tar xzf ./actions-runner.tar.gz

[2] Action Runner の 設定

# ランナーの設定
# --url / --token: 先ほど控えておいたものを指定する
$ ./config.sh --url https://github.com/your-user/your-repo --token XXXXXXXX
# => インタラクティブに質問される
# [1]
# Enter the name of the runner group to add this runner to: [press Enter for Default]
# ランナーグループを指定。デフォルトは「Default」。 => 今回は、そのまま Enter 
# [2]
# Enter the name of runner: [press Enter for {{Hostname}}]
# ランナー名を指定。デフォルトはランナーが動くホストの「ホスト名」。 => 今回は、そのまま Enter 
# [3]
# This runner will have the following labels: 'self-hosted', 'Linux', 'X64'
# Enter any additonal labels (ex. label-,label-2): [press Enter to skip]
# self-hosted runnerにつけるラベル追加。 => 今回は、 「hello-world-01」を入力し、Enter
# [4]
# Enter name of work folder: [press Enter for _work]
# ランナーのワーキングディレクトリを指定。デフォルトは「_work」。 => 今回は、そのまま Enter 

# 実行
$ ./run.sh

[3] GitHub上でAction Runner の 確認

[1] Settings > Actions > Runners を開く
[2] Statusで状態を把握できる
 * Status: Idleの場合:ランナーが正常に実行できており、疎通も取れている場合
 * Status: Offlineの場合:ネットワークに問題がある場合
 * Status: Activeの場合:ランナーがワークフローを実行中の場合

3)ワークフローの実行

* 作成したリポジトリの.github/workflows/配下に
 適当なワークフローを定義する
 => Push したら動く

.github/workflows/main.yaml

# git push時に「Hello World!」をコンソール上に出力する設定

name: Demo

on:
  - push

jobs:
  hello:
    runs-on: self-hosted
    steps:
      - name: Hello world
        run: echo 'Hello World!'

【3】使用上の注意

1)Public Repositoryで利用しない

https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#hardening-for-self-hosted-runners

より抜粋
~~~~~~~~~~
As a result, self-hosted runners should almost
 never be used for public repositories on GitHub,
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
because any user can open pull requests
 against the repository and compromise the environment.
~~~~~~~~~~

【4】トラブルシュート

1)config.sh実行後、エラー「HTTP response code: NotFound from ...」が表示

手順「[2] Action Runner の 設定」の「ランナーの設定」で
~~~~~
./config.sh --url https://github.com/your-user/your-repo --token XXXXXXXX
~~~~~
を実行した後に、以下「エラー内容」が表示された。
ちなみに、curl で対象URLは取得できるので、ネットワーク疎通は問題なさそう。

エラー内容

Http response code: NotFound from 'POST https://api.github.com/actions/runner-registration
' (Request Id: XXXXXXXXXX:XXXXXXXXXX:XXXXXXXXXX:XXXXXXXXXX:XXXXXXXXXX)
{"message":"Not Found","documentation_url":"https://docs.github.com/rest"}
Response status code does not indicate success: 404 (Not Found).

解決案

Tokenを取り直すために、再度、手順「1)Github 上での操作」を行い
新しいTokenで再実行したらうまくいった。

参考文献

https://qiita.com/h_tyokinuhata/items/7a9297f75d0513572f4a
https://devops-blog.virtualtech.jp/entry/20220926/1664160391

関連記事

Github ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/07/18/234652
Github Actions ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2021/11/04/142835
Github Actions ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2022/06/16/151443
Github Actions ~ Python関連 ~
https://dk521123.hatenablog.com/entry/2022/06/21/143624
Github Actions ~ Self-hosted runners / あれこれ編 ~
https://dk521123.hatenablog.com/entry/2024/02/07/002736
Github ~ Webhook 編 ~
https://dk521123.hatenablog.com/entry/2020/01/25/224402
Github Actionsで「Waiting for a runner to pick up this job...」から進まない
https://dk521123.hatenablog.com/entry/2024/01/10/195350