ラズパイでGitHub ActionsのSelf-Hosted Runnerを稼働させる

## 概要
Raspberry PiでGitHub ActionsのSelf-Hosted Runnerを動かす記事です。
公式の[ヘルプガイド](https://docs.github.com/ja/actions/hosting-your-own-runners/adding-self-hosted-runners)に則っています。

:::note alert
安全のため、**必ずプライベートリポジトリ**で実践してください。
:::

## 準備
- Raspberry Piの初期設定済み
- GitHubにリポジトリがある

## 環境
- Raspberry Pi 4
- Ubuntu 20.04 LTS 64bit
```sh
yukad2@raspi-tv:~$ cat /proc/device-tree/model
Raspberry Pi 4 Model B Rev 1.2y

yukad2@raspi-tv:~$ uname -a
Linux raspi-tv 5.4.0-1068-raspi #78-Ubuntu SMP PREEMPT Mon Aug 8 05:29:27 UTC 2022 aarch64 aarch64 aarch64 GNU/Linux
```
録画サーバを流用しているので、ホスト名が`raspi-tv`になっています。

## 実践
### 準備
リポジトリのSettings>Actions>Runners>Create self-hosted runnerから

- Linux
- ARM64(32bit版のラズパイを使っている場合はARM)

を選択して、ダウンロードリンクを準備する。

### ダウンロード
GitHubページのコードをコピペしながら実行してください。
```sh
yukad2@raspi-tv:~$ cd ~ # 今回はHOME下にRunnerを置きます
yukad2@raspi-tv:~$ mkdir actions-runner && cd actions-runner
yukad2@raspi-tv:~/actions-runner$ curl -o actions-runner-linux-arm64-2.301.1.tar.gz -L https://github.com/actions/runner/releases/download/v2.301.1/actions-runner-linux-arm64-2.301.1.tar.gz
yukad2@raspi-tv:~/actions-runner$ echo "6b9ba0e7296b5d613dc5aaa0ca640c16b2122a7d42e4b5906c67d9b5c8847e10  actions-runner-linux-arm64-2.301.1.tar.gz" | shasum -a 256 -c
# actions-runner-linux-arm64-2.301.1.tar.gz: OK
yukad2@raspi-tv:~/actions-runner$ tar xzf ./actions-runner-linux-arm64-2.301.1.tar.gz
```
### 設定
```
yukad2@raspi-tv:~/actions-runner$ ./config.sh --url URL --token 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

Enter the name of runner: [press Enter for raspi-tv] raspi # 好きな名前を入れてEnter

This runner will have the following labels: 'self-hosted', 'Linux', 'ARM64'
Enter any additional labels (ex. label-1,label-2): [press Enter to skip] # Enter

√ Runner successfully added
√ Runner connection is good

# Runner settings

Enter name of work folder: [press Enter for _work] # Enter

√ Settings Saved.

yukad2@raspi-tv:~/actions-runner$ ./run.sh

√ Connected to GitHub

Current runner version: '2.301.1'
2023-02-25 08:18:40Z: Listening for Jobs
```

起動が確認できたら`Ctrl + C`で止める。

### 常駐化
サービス化のスクリプトまで用意されているのでそれを使います。([公式ヘルプ](https://docs.github.com/ja/actions/hosting-your-own-runners/configuring-the-self-hosted-runner-application-as-a-service))
```sh
yukad2@raspi-tv:~/actions-runner$ sudo ./svc.sh install
yukad2@raspi-tv:~/actions-runner$ sudo ./svc.sh start
```

### 確認
![Pasted image 20230225173505.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/499004/52381cd5-ca54-3113-48e5-148086cc6978.png)
一覧に追加されていたら、正常に追加できています。

## Actionsを走らせる
リポジトリのActionsから、`Simple workflow` を追加します。
コード内の`runs-on`を`self-hosted`に変更すると、今回追加したRunnerで実行させることができます。
```yaml
# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "main" branch
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: self-hosted # ubuntu-latest ここを変更

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3

      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo Hello, world!

      # Runs a set of commands using the runners shell
      - name: Run a multi-line script
        run: |
          echo Add other actions to build,
          echo test, and deploy your project.

```
コミットしたら動きます。導入はここまで。

## 片付け
### サービスの停止
```sh
yukad2@raspi-tv:~$ cd ~/actions-runner/
yukad2@raspi-tv:~/actions-runner$ sudo ./svc.sh stop
yukad2@raspi-tv:~/actions-runner$ sudo ./svc.sh uninstall
```
### Runnerの削除
Settings>Actions>Runnersから、Removeを選択すると、削除用のコマンドラインを確認できます。
```sh
yukad2@raspi-tv:~/actions-runner$ ./config.sh remove --token TOKEN
```