くりにっき

フルスタックキュアエンジニアです

rbs_collection.lock.yamlをGitHub Actionsで自動updateする

ruby-jp slackでそういう需要があるので作った

ワークフローの設定

最低限いるのはこの辺。必要に応じてslack通知とか足してください *1

name: rbs-collection-updater

on:
  schedule:
    - cron: "0 0 1 * *" # Run monthly
  workflow_dispatch: # Run manually

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - uses: ruby/setup-ruby@v1
        with:
          # TODO: 適宜バージョンを指定する。(リポジトリに.ruby-versionや.tool-versionsをコミットしているなら不要)
          # ruby-version: "3.2" 

          bundler-cache: true

      - run: bundle exec rbs collection update

      - uses: actions/create-github-app-token@v1
        id: app-token
        with:
          app-id: ${{ secrets.GH_APP_ID }}
          private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}

      - uses: peter-evans/create-pull-request@v5
        with:
          token: ${{ steps.app-token.outputs.token }}
          committer: GitHub <noreply@github.com>
          title: "Update rbs_collection.lock.yaml"
          commit-message: "Run `bundle exe rbs collection update`"

実際に作られたPR

github.com

botの名前がsue445-bundle-update-prになっているのはbundle update PR用途で使ってたGitHub Appを流用しているため

事前に必要なもの

GitHub App

GitHub Actionsでデフォルトで使える secrets.GITHUB_TOKEN でPRを作成するとそのPRに対するjobが実行されない*2ので、PRを作りたい時だけは使わない方がいいです。

そこで別のtokenを使うことになるのですがGitHub ActionsでPersonal Access Token(PAT)を使うのはセキュアじゃない*3のでGitHub App経由でTokenを発行する(通称App Token)のが定番です。

github.com

docs.github.com

GitHub Actions上でPRを作るためにはGitHub Appに下記のパーミッションがあるといいでしょう

  • Contents: Read and write
  • Pull requests: Read and write

作成したAppのIDと秘密鍵はsecrets経由で渡しています。

*1: https://github.com/sue445/doorkeeper_jp/blob/main/.github/workflows/rbs-collection-updater.yml だとslack通知を入れている

*2:jobの無限実行を防ぐためのGitHub Actionsの仕様。c.f. https://sue445.hatenablog.com/entry/2020/08/31/081447

*3:アクセス可能なリポジトリや有効期限が無制限のPATが漏れると死