ジンジャー研究室

長めのつぶやき。難しいことは書きません。

GitHub Actions で Puppeteer をインストールして実行

分かってみれば全く大したことはない...けど誰かの役にたつかもしれないので足跡を残しておく。

.github/main.workflow

ワークフロー定義。カスタム定義の Action を使う(作り方はこの辺)。 UI では以下が上から串刺しになって見える。

workflow "Build and Test" {
  on = "push"
  resolves = ["Test"]
}

action "Install" {
  uses = "./puppeteer"
  runs = "npm"
  args = "install"
}

action "Test" {
  uses = "./puppeteer"
  needs = ["Install"]
  runs = "npm"
  args = "test"
}

コンテナは Action ごとに作られるけど、前の結果は引き継がれているように見える。

puppeteer/Dockerfile

Puppeteer を docker で使うためのテンプレの前半をコピペ。 同じディレクトリに entrypoint.sh を定義してもいいけど任意っぽい。

# https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md#running-puppeteer-in-docker
FROM node:10

RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
    && apt-get update \
    && apt-get install -y google-chrome-unstable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst ttf-freefont \
    --no-install-recommends \
    && rm -rf /var/lib/apt/lists/* \
    && rm -rf /src/*.deb

Puppeteer を root で使う時に --no-sandbox フラグが必要になるので、元ネタだとこの続きで USER を使っているんだけど、 GitHub Actions の制約で USER が使えないのでその部分は諦める。

index.js

仕方がないから CI の時だけ --no-sandbox つけようかということで、適当な環境変数を探す。

// https://developer.github.com/actions/creating-github-actions/accessing-the-runtime-environment/#environment-variables
const ci = !!process.env.GITHUB_ACTION;

// https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md#setting-up-chrome-linux-sandbox
const options = ci
  ? { args: ["--no-sandbox", "--disable-setuid-sandbox"] }
  : {};
const browser = await puppeteer.launch(options);

おしまい。