LoginSignup
13
8

More than 3 years have passed since last update.

新Github Actions を使って npm audit fix と PR 作成を自動化してみた

Last updated at Posted at 2019-11-10

Security Alerts に怒られる前になんとかしたい!

やりたいこと

Node.js であればnpm audit fixで行える依存ライブラリの更新のような保守作業…。
日頃意識していないとなかなか手につかないので自動で Pull Request 作成までできるようにしたい。
欲を言えば、Approve や Merge も自動でやってほしい。(今回はやらない)

Github Actions

11月13日ごろに正式リリースとなる Github Actions のβ版を試してみました。

なぜ Github Actions なのか

Github Marketplace で公開されているツールを使って Workflow が組めるので、他の CI/CD サービスよりも Github の操作がしやすいのではと思いました。

実装

今回は Create Pull Request を使って PR作成までを自動化することにしました。

Github Actions は Push や Create Pull Request がトリガーできる他、スケジュール起動ができるので今回はそれを使ってみます。
(Security Alerts で発火できるようにならないですかねー)

Push トリガーや、Pull Request トリガーはそのブランチ上で Workflow が実行されますが、 スケジュール起動の場合は master ブランチ上でのみ動作します。Workflow も master ブランチ上に置いてあるものが読み込まれます。

こんな感じで Workflow を用意し、master ブランチに展開します。
スケジュール設定はなんとなくで週一としました。

Create Pull Request は Github のアクセストークンを使うので、リポジトリの "Settings" -> "Secrets" から予め登録しておきます。

/.github/workflows/createPullRequest.yml
name: Create Security Fix Pull Request
on:
  schedule:
    - cron: '0 3 * * 3' # 毎週水曜日の正午に実行 ( 12+JST = 3+UTC )

jobs:
  patch:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/setup-node@v1 # Node.js 環境の構築
        with:
          node-version: '10.x'

      - uses: actions/checkout@master # リポジトリを読み込み

      - run: npm install -g npm

      - run: npm -v

      - run: npm audit fix

      - name: Create Pull Request # ブランチを作成して PullRequest を作成
        id: cpr
        uses: peter-evans/create-pull-request@v1.7.0
        with:
          title: Security Update into master.
          labels: Auto PR
          branch: securityFix #作成されるブランチは securityFix-xxxxxxx
          token: ${{ secrets.GITHUB }}

動作確認

こんな感じに Workflow が動き、
image.png

PR が作成されています。(Approve、Merge は手動です)
image.png

感想

今回は Pull Request 作成まででしたが、Auto ApproveMerge pull requests を組み合わせればリリース作業も完全に自動化できそうです。

13
8
2

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
13
8