LoginSignup
3
0

自動生成/更新されるファイルの差分チェックをGitHub Actionsでする

Last updated at Posted at 2022-12-16

どういったときに使えるか

  • コマンドなどによって自動で生成、更新されるファイルの漏れを防ぐことができる
    • 後から別Pull Request で修正する〜といった工数を減らせる
    • レビュー前後に検知しやすくなる

どうやるか

わかりやすく、package.json を更新した際に、yarn.lock の更新漏れがないかを確認するためのワークフローを作成する。
流れは下記のように考えている

  • nodeのセットアップ
  • ソースコード上の yarn.lockを退避させる
  • yarn install を実行し、yarn.lock を更新する
  • 退避したファイルと、更新したファイルのdiff を取る
.github/workflows/check_diff_yarn.yml
name: "Check diff yarn install"
on:
  pull_request:
    types: [opened, synchronize, reopened]

jobs:
  check_diff_yarn:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18]
    timeout-minutes: 5
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: ${{ inputs.node-version }}
      - name: copy current yarn.lock
        shell: bash
        run: mkdir tmp && cp yarn.lock tmp/yarn-temp.lock
      - run: yarn install
      - name: compare yarn.lock file
        shell: bash
        run: diff -u tmp/yarn-tmp.lock yarn.lock

これで、下記画像のように、更新漏れを検知できるようになります:thumbsup:

GitHub Actions が失敗することを確認できる画像

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