LoginSignup
2
0

GitHub Actionsでself-hosted runnersをDockerで作る

Posted at

諸事情あってセルフホステッドランナーをDockerから作る機会があったので

Dockerfileを作るついでにやったことをまとめました。
使用するDockerfileは以下です。

Dockerfile
FROM ubuntu:23.04

ARG PERSONAL_ACCESS_TOKEN
ARG HOST=https://github.com
ARG ORGANIZATION
ARG REPOSITORY

ENV BINARY_URL=https://github.com/actions/runner/releases/download/v2.296.3/actions-runner-linux-x64-2.296.3.tar.gz

ENV RUNNER_NAME=myrunner
ENV RUNNER_GROUP=Default
ENV RUNNER_LABELS="self-hosted,Linux,X64"
ENV RUNNER_WORKDIR=_work

RUN apt-get update && \
    apt-get install -y dotnet-sdk-6.0 curl sudo && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN useradd runner && \
    echo "runner:runner" | chpasswd && \
    chsh -s /usr/bin/bash runner && \
    usermod -aG sudo runner && \
    mkdir /actions-runner && \

    chown runner:runner /actions-runner

USER runner
WORKDIR /actions-runner

RUN curl -fsSL -o actions-runner.tar.gz -L $BINARY_URL && \
    tar xf actions-runner.tar.gz && \
    rm actions-runner.tar.gz && \
    echo $PERSONAL_ACCESS_TOKEN && \
    ./config.sh \
        --unattended \
        --url $HOST/$ORGANIZATION/$REPOSITORY \
        --pat $PERSONAL_ACCESS_TOKEN \
        --name $RUNNER_NAME \
        --runnergroup $RUNNER_GROUP \
        --labels $RUNNER_LABELS \
        --work $RUNNER_WORKDIR

CMD ["./run.sh"]

Ubuntuから作っています。

そのまま使う場合はパーソナルアクセストークンを発行して環境変数を付与してください。

docker build . -t self-hosted-runner \
  --build-arg PERSONAL_ACCESS_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \
  --build-arg HOST=https://github.com \
  --build-arg ORGANIZATION=octocat \
  --build-arg REPOSITORY=Hello-World

イメージを作った後はそのまま実行することでself-hosted runnersを待機状態にすることができます。

docker run self-hosted-runner

困ったこと

Ubuntuのデフォルトのユーザーはrootで実行できない

デフォルトの状態(rootユーザー)でconfig.shを実行すると怒られる

# ./config.sh

Must not run with sudo

#./run.sh

Must not run interactively with sudo
Exiting runner...

のでユーザーを作る。

useradd runner && \
  echo "runner:runner" | chpasswd && \
  chsh -s /usr/bin/bash runner && \
  usermod -aG sudo runner && \

.netが必要と書いてない。

self-hosted runnersの追加の仕方は以下ですが、dotnetのことは言及されてません。動かす時になってようやくわかります。

しかも.net自体のインストールも分かりづらい不親切っぷり
https://learn.microsoft.com/ja-jp/dotnet/core/install/windows?tabs=net70

結局.net 6が必要になりました。

apt install dotnet-sdk-6.0

tokenが使えない

config.shを動かす時にtokenが必要なのですがこのtokenを発行する方法が見つかりませんでした。
そういうAPIがあったりスクレイピングすればいけるか?と思いいろいろ調べたところ以下のissueに当たりました。

config.shにpatを使えるオプションがあったのでそれを使うと無事GitHub Actionsに登録することができました。

./config.sh \
    --unattended \
    --url $HOST/$ORGANIZATION/$REPOSITORY \
    --pat $PERSONAL_ACCESS_TOKEN \
    --name $RUNNER_NAME \
    --runnergroup $RUNNER_GROUP \
    --labels $RUNNER_LABELS \
    --work $RUNNER_WORKDIR
2
0
0

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
2
0