使用 rubocop 自动格式化每次的代码提交
起因:经常在 push 代码前忘记格式化,所以干脆将该动作放在 CI 里自动格式化代码 ( 适合Github代码库 )
步骤:修改 rails 默认生成的 ci.yml 文件,将 lint job移到最上方,并修改如下:
步骤:修改 rails 默认生成的 ci.yml 文件,将 lint job移到最上方,并修改如下:
jobs: lint: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 - name: Set up Ruby uses: ruby/setup-ruby@v1 with: ruby-version: .ruby-version bundler-cache: true - name: Lint code for consistent style run: bin/rubocop -f github -A - uses: stefanzweifel/git-auto-commit-action@v5 with: commit_message: "style: auto fix by rubocop"
然后将 /vendor/bundle 加入到 .gitignore 中,防止 auto-commit 提交了 bundle 里的缓存文件。
最后,在 repo 的 Settings / Actions / General / Workflow permissions 中勾选 Read and write permissions 确保写入权限。
这样每次提交,即可自动使用 rubocop 格式化代码,非常方便
最后,在 repo 的 Settings / Actions / General / Workflow permissions 中勾选 Read and write permissions 确保写入权限。
这样每次提交,即可自动使用 rubocop 格式化代码,非常方便