LoginSignup
2
1

More than 1 year has passed since last update.

CircleCIのみでbundle update〜プルリク作成までを自動化してみた

Last updated at Posted at 2019-06-03

Github上でbundle update用のgemはいくつか見かけたが、個人用のリポジトリだったりしてメンテ頻度不明なため、CircleCIのみでbundle updateできないか模索した。

CircleCI2.0よりcron機能が利用できるようになってるので、それを利用すれば可能と判明。
以下、毎月1日に動作させるように設定した(以下、必要箇所抜粋)

.circleci/config.yml

 bundle_update:
   docker:
     - image: 利用イメージ名記述
       environment:
         BUNDLE_JOBS: 3
         BUNDLE_RETRY: 3
         BUNDLE_PATH: vendor/bundle
   steps:
     - checkout
     - restore_cache:
         keys:
           - 接頭字-{{ .Environment.CACHE_VERSION }}-{{ checksum "Gemfile.lock" }}

     - run:
         name: Bundle Install
         command: bundle check || bundle install

     - run:
         name: Bundle Update
         command: bundle update

     # Create branch
     - run: git checkout -b bundle-update-$CIRCLE_BUILD_NUM
     - run: git config user.email "メールアドレス"
     - run: git config user.name "管理者名"
     - run: git add Gemfile Gemfile.lock
     - run: git commit -am '今月のGemfile更新用の自動作成プルリク'
     - run: git push origin bundle-update-$CIRCLE_BUILD_NUM
     - run:
         name: Create a pull request
         command: |
           curl \
             --header "Accept: application/vnd.github.v3+json" \
             --data "{\"title\": \"今月のbundle-update\", \"head\": \"ワークスペース名:bundle-update-$CIRCLE_BUILD_NUM\", \"base\":\"master\" }" \
             https://api.github.com/repos/githubアカウント/対象トリポジトリ名/pulls?access_token=${GITHUB_ACCESS_TOKEN}

workflows:
 version: 2
 monthly-bundle-update:
   triggers:
     - schedule:
         cron: "0 9 1 * *" ←UTC記述。毎月1日に動作させるよう記述
         filters:
           branches:
             only:
               - master
   jobs:
     - bundle_update

特徴としては、CircleCIの環境変数を利用してbranch名の重複を避けている点。
branch名の重複を避ける事でCircleCIをRerunしてもbranch名の重複でコケるのを避けられる。

$CIRCLE_BUILD_NUM: CircleCI側で準備されているビルド番号用の環境変数
${GITHUB_ACCESS_TOKEN}: こちら側で独自に準備したGithub連携用のアクセスtoken

※CircleCI側が提供している環境変数はこちら

注意点として、bundle updateする場合、Gemバージョンアップをどこまで許容するかが重要。

Gemfile内で、メジャーアップデートさせたいGem、マイナーバージョンのみアップデートさせたいGemなどを切り分ける必要がある。

Gemのメジャーバージョンのアップデート(X.X.Xの2桁目をアップデート)を行う記述は以下の通り

Gemfile
gem 'devise', '~> 4.6'

Gemのマイナーバージョンのアップデート(X.X.Xの3桁目をアップデート)を行う記述は以下の通り

Gemfile.lock
gem 'devise', '~> 4.6.0'

Railsであれば、Rspecなどのテストツールで書かれていない範囲にはサードパーティーのGem関連の機能が無いようにする事を忘れない事が重要。(Gemをバージョンアップした結果、動作しない事態を避けるため)

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