GitHub ActionsでPuppeteerを動かしたいなと思い試してみたメモです。
結果、特にそこまで気にせずに起動してくれました。
準備
割と普通ですね。
yarn add puppeteer
プログラム
GitHubのPuppeteer Headfulのページを見るとprocess.env.PUPPETEER_EXEC_PATH
でバイナリを指定しろみたいな記載があるけど、特に指定せずに普通インストールして使えました。
app.js
constpuppeteer=require('puppeteer');(async()=>{constURL=`https://twitter.com/n0bisuke`;constbrowser=awaitpuppeteer.launch({});constpage=awaitbrowser.newPage();awaitpage.goto(URL);//URLにアクセス// Get the "viewport" of the page, as reported by the page.constdimensions=awaitpage.evaluate(()=>{return{width:document.documentElement.clientWidth,height:document.documentElement.clientHeight,title:document.title,deviceScaleFactor:window.devicePixelRatio};});console.log('Dimensions:',dimensions);console.log('タイトル:',dimensions.title);awaitbrowser.close();})();
ちなみにローカルでもこのままnode app.jsで使えます。
ymlの記述
.github/workflows/run-puppeteer.yml
を作成
run-puppeteer.yml
name:RUN puppeteeron:push:branches:[master]jobs:build:runs-on:ubuntu-lateststrategy:matrix:node-version:[14.x]# 以下が実際のステップsteps:-uses:actions/checkout@v2-name:Use Node.js 14.xuses:actions/setup-node@v1with:node-version:'14.x'-name:install commandrun:yarn install-name:RUN app.jsrun:node app.js
デプロイして確認
ちゃんと動いてるっぽい
所感
Vercelなどserverless環境で動かすときとは変更あったけど、今回は特に無さそうですね。
ファイル書き込みがどうなるかとかは試せてないのでその辺気になりますね。
全体コードはこちらにまとめておきます。