12
2

More than 1 year has passed since last update.

GitHub Actions を利用し ".rubocop_todo.yml" を自動更新する

Last updated at Posted at 2023-03-07

はじめに

普段開発をしていると、定義ファイルや設定ファイルの更新を行う機会は少なくありません。
".rubocop_todo.yml" を更新するような単純作業は機械にまかせていきたいなと思いこの記事を書きました。

どのように実現するか

GitHub Actions には 定期実行を行う機能が存在します (on.schedule)
定期実行した job の中で rubocop を実行し生じた差分を利用し Pull request を作成します。

今回は peter-evans/create-pull-request を利用します。

GitHub Actions の設定例

.githu/workflows directory 以下に ファイルを作成します
yml に記載している 処理は以下の通りです。

  • repository を checkout
  • Ruby の install & bundle install
  • rubocop の実行
  • Pull request の作成
.githu/workflows/update_rubocop_todo.yml
name: Update .rubocop_todo.yml

on:
  schedule:
    - cron: "0 1 * * 1-5"
  workflow_dispatch:

permissions:
  contents: write
  pull-requests: write

jobs:
  export_emoji:
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
    steps:
      - uses: actions/checkout@v3
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.0'
          bundler-cache: true
      - run: bundle exec rubocop --auto-gen-config --auto-gen-only-exclude --no-exclude-limit
      - uses: peter-evans/create-pull-request@v4
        with:
          commit-message: Update .rubocop_todo.yml
          delete-branch: true
          title: Update .rubocop_todo.yml
          reviewers: mziyut

上記 yml を main (master) branch 等に push し、定義した時間まで待つか、 Actions 内から手動実行すると
以下のような Pull request が作成されます

image.png

さいごに

  • 今回は、 rubocop を例に記載しましたが、 rubocop 以外にも適用できます
  • 自動化して時間を使うべきところに使えるようにしたいですね

References

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