💡

[Self-hosted Runners] Private な Repoでも 無料で長時間 Github Actions をぶん回したい

2023/09/25に公開

書かないこと

Github Actions Self-hosted runnersの概要は書かない。詳細は下記参照のこと。

https://docs.github.com/ja/actions/hosting-your-own-runners/managing-self-hosted-runners/about-self-hosted-runners

はじめに

あんまり言及されている記事を見かけなかったので書きます。

Github Actionsのself-hosted runnersは、PrivateなRepoであっても無料なんです!

スクレイピングとか時間かかりますよね。
Self-hosted Runnersを使えば、上限時間にビクビクしなくて済みます!

言いたかったことはこれで全部!🤣

というと寂しいので、使い方を。

docker + docker composeでworkerを作る

調べたところ、myoung34/docker-github-actions-runner が一番有名っぽいので、これを使います。
https://github.com/myoung34/docker-github-actions-runner

docker-compose.yml

ほとんどDocのままですが、RUNNER_TOKENではなくACCESS_TOKENを使うことにした。

version: '2.3'

services:
  worker:
    image: myoung34/github-runner
    restart: always
    environment:
      REPO_URL: ${RUNNER_REPO}
      RUNNER_NAME: ${RUNNER_NAME}
      ACCESS_TOKEN: ${RUNNER_ACCESS_TOKEN}
      RUNNER_WORKDIR: /tmp/runner/work
      ORG_RUNNER: 'false'
      LABELS: linux,x64,gpu
    security_opt:
      - label:disable
    volumes:
      - '/var/run/docker.sock:/var/run/docker.sock'
      - '/tmp/runner:/tmp/runner'

.env

RUNNER_REPO=https://github.com/TakashiAihara/***
RUNNER_ACCESS_TOKEN=ghp_******
RUNNER_NAME=dev

実行

docker compose up -d
docker compose logs --follow
selfhosted-runner-worker-1  | Runner reusage is disabled
selfhosted-runner-worker-1  | Obtaining the token of the runner
selfhosted-runner-worker-1  | Configuring
selfhosted-runner-worker-1  | 
selfhosted-runner-worker-1  | --------------------------------------------------------------------------------
selfhosted-runner-worker-1  | |        ____ _ _   _   _       _          _        _   _                      |
selfhosted-runner-worker-1  | |       / ___(_) |_| | | |_   _| |__      / \   ___| |_(_) ___  _ __  ___      |
selfhosted-runner-worker-1  | |      | |  _| | __| |_| | | | | '_ \    / _ \ / __| __| |/ _ \| '_ \/ __|     |
selfhosted-runner-worker-1  | |      | |_| | | |_|  _  | |_| | |_) |  / ___ \ (__| |_| | (_) | | | \__ \     |
selfhosted-runner-worker-1  | |       \____|_|\__|_| |_|\__,_|_.__/  /_/   \_\___|\__|_|\___/|_| |_|___/     |
selfhosted-runner-worker-1  | |                                                                              |
selfhosted-runner-worker-1  | |                       Self-hosted runner registration                        |
selfhosted-runner-worker-1  | |                                                                              |
selfhosted-runner-worker-1  | --------------------------------------------------------------------------------
selfhosted-runner-worker-1  | 
selfhosted-runner-worker-1  | # Authentication
selfhosted-runner-worker-1  | 
selfhosted-runner-worker-1  | 
selfhosted-runner-worker-1  | √ Connected to GitHub
selfhosted-runner-worker-1  | 
selfhosted-runner-worker-1  | # Runner Registration
selfhosted-runner-worker-1  | 
selfhosted-runner-worker-1  | 
selfhosted-runner-worker-1  | 
selfhosted-runner-worker-1  | 
selfhosted-runner-worker-1  | √ Runner successfully added
selfhosted-runner-worker-1  | √ Runner connection is good
selfhosted-runner-worker-1  | 
selfhosted-runner-worker-1  | # Runner settings
selfhosted-runner-worker-1  | 
selfhosted-runner-worker-1  | 
selfhosted-runner-worker-1  | √ Settings Saved.
selfhosted-runner-worker-1  | 
selfhosted-runner-worker-1  | 
selfhosted-runner-worker-1  | √ Connected to GitHub
selfhosted-runner-worker-1  | 

Connected to Github が出たら待機状態に。

リポジトリ > Actions > Runners > Self-hosted Runners
にも出てきます。

使い方

runs-on に指定してあげるだけ。

    # runs-on: ubuntu-latest
    runs-on: [self-hosted, linux, x64]

その他

https://docs.github.com/ja/billing/managing-billing-for-github-actions/about-billing-for-github-actions

GitHub Actions の使用は、パブリック リポジトリの標準の GitHub ホステッド ランナーとセルフホステッド ランナーの場合は無料です。

この書き方、日本語的に若干読み取りづらい部分ですよね。
日本語ムツカシイ

Discussion