10
3

More than 1 year has passed since last update.

GitHub Actions SelfHostedを試してみた

Last updated at Posted at 2019-12-09

この記事はフラー Advent Calendar 2019の 10 日目の記事です。9日目@seto_inugami さんで「(ネタがなかったので)2日ほどかけて社内向け端末管理アプリを作ってみた」でした。

やっとGithubActionsがGAになりましたね。弊社でも9月終わりぐらいから触っていました。
個人的にすごい気になってた発表があったんです(下記URL
Self-hosted runners for GitHub Actions is now in beta

自宅でサーバーを持て余していたので動かしてみたくなってしまいました。
ということで以下実際に動かした記録です

インストール環境

OSはCentOS7(Minimal)を使用します
今回利用するスペックは下記の通りです
スクリーンショット 2019-12-09 20.49.34.png
※OSインストールは割愛

1. GitHubでrunnerを追加する

Setting -> Actions を開いてAdd runnerをクリックします

image.png

  • OSを選択します(今回はLinux x64)
    image.png

  • モーダルに書いてある通りにコマンドを実際のサーバーで動かして行きます
    モーダルからクリップボードにコピーできるので簡単に入力できます

// Create a folder
$ mkdir actions-runner && cd actions-runner
// Download the latest runner package
$ curl -O https://githubassets.azureedge.net/runners/2.162.0/actions-runner-linux-x64-2.162.0.tar.gz
// Extract the installer
$ tar xzf ./actions-runner-linux-x64-2.162.0.tar.gz

上記コマンドでDownloadの項目が終わったのでGithubと接続するためConfigureの項目のコマンドを実行します

// Create the runner and start the configuration experience
$ ./config.sh --url https://github.com/ManabuSeki/github-actions-test --token ###TOKEN###
// Last step, run it!
$ ./run.sh

なんだ 簡単じゃんと思っていたときもありました
はい、動きません!

以下のようなエラーメッセージが

Libicu's dependencies is missing for Dotnet Core 3.0
Execute ./bin/installdependencies.sh to install any missing Dotnet Core 3.0 dependencies.

メッセージ通りに./bin/installdependencies.sh を実行します

Need to run with sudo privilege

【#・∀・】ムカムカ(ねぇ最初に言って)

sudoを実行できる用にOS側を設定します。(割愛
再度 sudo をつけて実行します

$ sudo ./bin/installdependencies.sh
--------OS Information--------
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

------------------------------
The current OS is Fedora based
--------Redhat Version--------
CentOS Linux release 7.7.1908 (Core)
------------------------------
/bin/yum
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
パッケージ lttng-ust は利用できません。
パッケージ 1:openssl-libs-1.0.2k-19.el7.x86_64 はインストール済みか最新バージョンです
パッケージ krb5-libs-1.15.1-37.el7_7.2.x86_64 はインストール済みか最新バージョンです
パッケージ zlib-1.2.7-18.el7.x86_64 はインストール済みか最新バージョンです
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ libicu.x86_64 0:50.2-3.el7 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

=============================================================================================================
 Package                  アーキテクチャー         バージョン                   リポジトリー            容量
=============================================================================================================
インストール中:
 libicu                   x86_64                   50.2-3.el7                   base                   6.9 M

トランザクションの要約
=============================================================================================================
インストール  1 パッケージ

総ダウンロード容量: 6.9 M
インストール容量: 24 M
Downloading packages:
libicu-50.2-3.el7.x86_64.rpm                                                          | 6.9 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : libicu-50.2-3.el7.x86_64                                                     1/1
  検証中                  : libicu-50.2-3.el7.x86_64                                                     1/1

インストール:
  libicu.x86_64 0:50.2-3.el7

完了しました!
-----------------------------
 Finish Install Dependencies
-----------------------------

はい、インストールはできました。
再度 config.sh を実行します

$ ./config.sh --url https://github.com/ManabuSeki/github-actions-test --token ###TOKEN###

--------------------------------------------------------------------------------
|        ____ _ _   _   _       _          _        _   _                      |
|       / ___(_) |_| | | |_   _| |__      / \   ___| |_(_) ___  _ __  ___      |
|      | |  _| | __| |_| | | | | '_ \    / _ \ / __| __| |/ _ \| '_ \/ __|     |
|      | |_| | | |_|  _  | |_| | |_) |  / ___ \ (__| |_| | (_) | | | \__ \     |
|       \____|_|\__|_| |_|\__,_|_.__/  /_/   \_\___|\__|_|\___/|_| |_|___/     |
|                                                                              |
|                       Self-hosted runner registration                        |
|                                                                              |
--------------------------------------------------------------------------------

# Authentication


√ Connected to GitHub

# Runner Registration

Enter the name of runner: [press Enter for github-actions]

√ Runner successfully added
√ Runner connection is good

# Runner settings

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

√ Settings Saved.

runner nameとwork folderを聞かれましたが、デフォルトで進みます

あとは ./run.sh を実行する


$ ./run.sh

√ Connected to GitHub

2019-12-09 12:35:38Z: Listening for Jobs

image.png

無事つながりましたね
ではyamlファイルを置いて実際に動くか確かめてみましょう

2. github-actionsを利用するためのyamlファイルを作成

github-actionsを利用する為のyamlファイルを作成し masterにpushします

$ mkdir -p .github/workflows
$ touch .github/workflows/hello_world.yml
hello_world.yml
name: hello_world
on: push
jobs:
  build:
    name: hello
    runs-on: self-hosted
    steps:
      - name: Hello World
        run: echo "Hello World"

1.で./run.sh を実行したターミナルを見てみると

2019-12-09 13:09:31Z: Running job: hello
2019-12-09 13:09:38Z: Job hello completed with result: Succeeded

という風にログが流れていました。

Actionsタブを開いて見てもちゃんと実行できているようです。
image.png
めでたしめでたし

おまけ(サービスとして起動)

./run.shを実行しただけの状態だとターミナルを閉じたら利用できなくなってしまいます(Offline)
image.png

そこでサービスとして立ち上げることにしました。

1. サービスのインストール

actions-runnerをインストールしたフォルダに移動し、./svc.sh を実行します

$ cd actions-runner/
$ ./svc.sh
Must run as sudo

【#・∀・】ムカムカ
sudoをつけて再度実行します

$ sudo ./svc.sh

Usage:
./svc.sh [install, start, stop, status, uninstall]
Commands:
   install [user]: Install runner service as Root or specified user.
   start: Manually start the runner service.
   stop: Manually stop the runner service.
   status: Display status of runner service.
   uninstall: Uninstall runner service.

svc.shには上記のコマンドが利用できるらしい
とりあえず install してみます

$ sudo ./svc.sh install
Creating launch runner in /etc/systemd/system/actions.runner.ManabuSeki-github-actions-test.github-actions.service
Run as user: github
Run as uid: 1000
gid: 1000
Created symlink from /etc/systemd/system/multi-user.target.wants/actions.runner.ManabuSeki-github-actions-test.github-actions.service to /etc/systemd/system/actions.runner.ManabuSeki-github-actions-test.github-actions.service.

できたようだ

2. サービスの起動と停止

1.で作ったサービスの起動と停止も svc.sh経由で行う

起動

sudo ./svc.sh start

/etc/systemd/system/actions.runner.ManabuSeki-github-actions-test.github-actions.service
● actions.runner.ManabuSeki-github-actions-test.github-actions.service - GitHub Actions Runner (ManabuSeki-github-actions-test.github-actions)
   Loaded: loaded (/etc/systemd/system/actions.runner.ManabuSeki-github-actions-test.github-actions.service; enabled; vendor preset: disabled)
   Active: active (running) since 月 2019-12-09 22:29:49 JST; 7ms ago
 Main PID: 28504 (runsvc.sh)
   CGroup: /system.slice/actions.runner.ManabuSeki-github-actions-test.github-actions.service
           ├─28504 /bin/bash /home/github/actions-runner/runsvc.sh
           └─28507 ./externals/node12/bin/node ./bin/RunnerService.js

12月 09 22:29:49 github-actions.zitaku.net systemd[1]: Started GitHub Actions Runner (ManabuSeki-githu...s).
12月 09 22:29:49 github-actions.zitaku.net runsvc.sh[28504]: .path=/usr/local/bin:/bin:/usr/bin:/usr/lo...in
Hint: Some lines were ellipsized, use -l to show in full.

停止

$ sudo ./svc.sh stop

/etc/systemd/system/actions.runner.ManabuSeki-github-actions-test.github-actions.service
● actions.runner.ManabuSeki-github-actions-test.github-actions.service - GitHub Actions Runner (ManabuSeki-github-actions-test.github-actions)
   Loaded: loaded (/etc/systemd/system/actions.runner.ManabuSeki-github-actions-test.github-actions.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 月 2019-12-09 22:31:40 JST; 5ms ago
  Process: 28504 ExecStart=/home/github/actions-runner/runsvc.sh (code=exited, status=0/SUCCESS)
 Main PID: 28504 (code=exited, status=0/SUCCESS)

12月 09 22:29:50 github-actions.zitaku.net runsvc.sh[28504]: Started running service
12月 09 22:29:53 github-actions.zitaku.net runsvc.sh[28504]: √ Connected to GitHub
12月 09 22:29:54 github-actions.zitaku.net runsvc.sh[28504]: 2019-12-09 13:29:54Z: Listening for Jobs
12月 09 22:31:40 github-actions.zitaku.net systemd[1]: Stopping GitHub Actions Runner (ManabuSeki-gith......
12月 09 22:31:40 github-actions.zitaku.net runsvc.sh[28504]: Shutting down runner listener
12月 09 22:31:40 github-actions.zitaku.net runsvc.sh[28504]: Sending SIGINT to runner listener to stop
12月 09 22:31:40 github-actions.zitaku.net runsvc.sh[28504]: Exiting...
12月 09 22:31:40 github-actions.zitaku.net runsvc.sh[28504]: Runner listener exited with error code 0
12月 09 22:31:40 github-actions.zitaku.net runsvc.sh[28504]: Runner listener exit with 0 return code, s...d.
12月 09 22:31:40 github-actions.zitaku.net systemd[1]: Stopped GitHub Actions Runner (ManabuSeki-githu...s).
Hint: Some lines were ellipsized, use -l to show in full.

これでターミナルを閉じても裏で実行し続けられますね

最後に

publicリポジトリでは決して使用しないでください。悪意のあるコードがpushされて、実行環境で実行されてしまう可能性があります。
publicリポジトリの環境ではself hostedを利用せずとも無料で利用できるので そちらを利用しましょう

明日のフラーAdvent Calendar 2019 11日目はokuzawatsさんでフラー株式会社でもバグバッシュ大会をやってみたです

10
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
3