mito’s blog

IT技術メインの雑記。思い立ったが吉日。

[GitHub Actions] セルフホストランナーをCloud9で試す

はじめに

GitHub ActionsをCloud9に構築したセルフホストランナーで動かしてみます。
GitHubホストランナーは利用時間の制限(無料/有料)があったり、OSの種類が決まっていますが、
セルフホストランナーは自身で用意するため、制限はなくカスタマイズも自由です。

GitHub Actions を理解する - GitHub Docs セルフホステッド ランナーの概要 - GitHub Docs



セルフホストランナーの追加手順

1. プライベートリポジトリの作成

適当なリポジトリをPrivateで作成します。

2. Agentマシンの構築

今回はCloud9を使用します。
Cloud9をインスタンスタイプ「t2.micro」、Platform「Ubuntu Server 18.04 LTS」で立てます。
セキュリティグループはデフォルトそのままでも大丈夫です。インバウンドルール、アウトバウンドルールともに何か追加する必要はありません。

セルフホステッド ランナーの概要 - GitHub Docs


3. セルフホストランナーの追加

追加したいリポジトリのメニューをたどっていくと、OSごとにセルフホストランナーを追加するコマンド手順が表示されます。
コマンドにはトークンも記載されているため、改変する必要はなく、コピペで済みます。

上部メニュー[Settings] -> 左サイドメニュー[Actions] -> [Runners] -> [New self-hosted runner]

自己ホストランナーの追加 - GitHub Docs

Linuxの例

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

**Configure**
# Create the runner and start the configuration experience
$ ./config.sh --url https://github.com/mito-201/リポジトリ名 --token ******************
# Last step, run it!
$ ./run.sh


run.shを実行すると、以下のようにGitHubと接続されたメッセージが表示され、[Runners]にも追加されます。

$ ./run.sh 

√ Connected to GitHub

Current runner version: '2.303.0'
2023-05-05 16:35:39Z: Listening for Jobs


ランナーのステータス

ステータスは次のいずれかです。

  • Idle
    • ランナーはGitHubに接続されており、ジョブを実行する準備ができています。
  • Active
    • ランナーは現在ジョブを実行しています。
  • Offline
    • ランナーは GitHub に接続されていません。
    • マシンがオフライン、セルフホストランナー上でアプリケーションが実行されていない、アプリケーションがGitHubと通信できない可能性があります。


ワークフローの作成手順

GitHub Actions を理解する - GitHub Docs

1. ディレクトリの作成

ワークフローファイルを格納するためのディレクト.github/workflowsを作成します。


2. ワークフローファイルの作成

ディレクト.github/workflowsに、以下のファイル内容でgithub-actions-demo.ymlを作成します。

  • ワークフローのトリガー
    • branches: [ "main" ] へのpush
    • branches: [ "main" ] へのpull_request
  • runs-on: [self-hosted]
    • ワークフローを実行するランナーのラベルを指定します。

GitHub Actions のワークフロー構文 - GitHub Docs


# 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]
    # 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: git --version

      # 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.


3. ワークフローの実行

github-actions-demo.ymlをmainブランチにプッシュした際、すでにワークフローが実行されています。
上部メニュー[Actions]より、ワークフローのログが確認できます。


その他

セルフホストランナー上のコンテナでワークフローを動かしたい

GitHub Actions のワークフロー構文 - GitHub Docs

以下のように、container: image: ****と動かしたいコンテナイメージを記載します。

    runs-on: [self-hosted]
    container:
      image: bitnami/git


step単位でコンテナを指定することもできます。

    steps:
      - name: My first step
        uses: docker://alpine:3.8


セルフホストランナーのアプリケーションをサービスとして設定する

セルフホストランナーアプリケーションをサービスとして設定する - GitHub Docs

run.shと同じフォルダにあるスクリプトsvc.shを、目的に合う引数で実行します。

# インストール
$ sudo ./svc.sh install

# スタート
$ sudo ./svc.sh start

# ステータス確認
$ sudo ./svc.sh status


GitHubで利用されているIPアドレスを確認する

以下のメタ情報で確認できます。

About GitHub's IP addresses - GitHub Docs

https://api.github.com/meta