👌

毎週bundle updateを行うGitHub Actions

に公開

背景

基本的には Dependabot があればそれを使えばいい。

しかし、プライベートリポジトリにある gem を使っていて良い感じにアクセス権限をつける事ができずに Dependabot が使えなかったので GitHub Actions でやってみた。

YAML

.github/workflows/bundle-update.yml

name: Weekly Bundle Update

on:
  schedule:
    # UTCで設定する
    # UTC:毎週金曜日12時 => JST:毎週金曜日21時
    - cron: '0 12 * * 5'
  workflow_dispatch: # 手動実行もできるようにしておくと便利

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  RUBY_VERSION: 3.4.5
  BUNDLER_CACHE_VERSION: 1

jobs:
  bundle-update:
    runs-on: ubuntu-latest
    timeout-minutes: 30

    steps:
      - uses: actions/checkout@v4

      - name: Restore bundle cache
        uses: actions/cache@v4
        with:
          path: vendor/bundle
          key: ${{ env.BUNDLER_CACHE_VERSION }}-bundle-${{ runner.os }}-${{ env.RUBY_VERSION }}-${{ hashFiles('Gemfile.lock') }}
          restore-keys: |
            ${{ env.BUNDLER_CACHE_VERSION }}-bundle-${{ runner.os }}-${{ env.RUBY_VERSION }}-
            ${{ env.BUNDLER_CACHE_VERSION }}-bundle-${{ runner.os }}-

      - name: Setup Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: ${{ env.RUBY_VERSION }}

      - name: Install bundle dependencies
        run: bundle install --path vendor/bundle

      - name: Store bundle cache
        uses: actions/cache@v4
        with:
          path: vendor/bundle
          key: ${{ env.BUNDLER_CACHE_VERSION }}-bundle-${{ runner.os }}-${{ env.RUBY_VERSION }}-${{ hashFiles('Gemfile.lock') }}

      - name: Run bundle update
        run: |
          cp Gemfile.lock Gemfile.lock.backup
          
          bundle update --verbose
          
          # 更新がある時のみPRを送りたいので差分チェック
          if ! diff -q Gemfile.lock.backup Gemfile.lock > /dev/null; then
            echo "Dependencies updated"
            echo "DEPENDENCIES_UPDATED=true" >> $GITHUB_ENV
          else
            echo "No dependencies to update"
            echo "DEPENDENCIES_UPDATED=false" >> $GITHUB_ENV
          fi

      - name: Generate date
        if: env.DEPENDENCIES_UPDATED == 'true'
        run: echo "DATE=$(date +'%Y-%m-%d')" >> $GITHUB_ENV

      - name: Create Pull Request
        if: env.DEPENDENCIES_UPDATED == 'true'
        uses: peter-evans/create-pull-request@v6
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: |
            👍 Weekly bundle update

            - gem依存関係を最新版に更新
          title: "👍 Weekly bundle update - ${{ env.DATE }}"
          body: |
            ## 概要
            定期的なGem依存関係の更新を実行しました。

            ## 変更内容
            - `bundle update`によるGem依存関係の更新

            ## 注意事項
            このPRは自動生成されています。マージ前に必ずテストの実行と動作確認を行ってください。

          branch: feature/bundle-update-${{ env.DATE }}
          delete-branch: true
          draft: false

Discussion