LoginSignup
1
0

More than 3 years have passed since last update.

GitHub EnterpriseとJenkinsでもupdate_dependenciesを諦めない

Last updated at Posted at 2020-12-24

プロジェクトの依存関係 (Dependency) の更新は自動化しておきたいですよね

Dependabot 便利ですね、CocoaPodsならRenovateとかも使えるかと思います

GitHub使えたりクラウドサービス使える場合はいいんですが、案件によって制約があったりする事もあるのでJenkins+fastlaneでなんとなくやってみました

Fastfile

  desc "daily"
  lane :daily do
    bundle_update
    swiftlint_format
    pod_update
  end

  private_lane :bundle_update do
    output = sh('bundle update')
    create_pr(
      branch_name: "bundle-update",
      commit_message: "update: Gemfile.lock",
      pr_title: "[CI] bundle update",
      pr_body: output
    )
  end

  private_lane :swiftlint_format do
    swiftlint(
      mode: "autocorrect",
      format: true,
      ignore_exit_status: true
    )
    output = sh("git diff --name-only")
    create_pr(
      branch_name: "swiftlint-format",
      commit_message: "autocorrect: swiftlint-format",
      pr_title: "[CI] swiftlint format",
      pr_body: output
    )
  end

  private_lane :pod_update do
    output = sh('bundle exec pod update')
    Dir.chdir("#{ENV['PWD']}") do
      sh("./Scripts/license-plist.sh")
    end
    create_pr(
      branch_name: "pod-update",
      commit_message: "update: Podfile.lock",
      pr_title: "[CI] pod update",
      pr_body: output
    )
  end

  private_lane :create_pr do |options|
    next if system('git diff --quiet --exit-code')
    current_branch = git_branch
    date = Date.today.to_s
    branch_name = "ci/#{options[:branch_name]}-#{date}"
    sh("git checkout -b #{branch_name}")
    git_add
    git_commit(path: "./", message: options[:commit_message])
    push_to_git_remote
    create_pull_request(
      title: "#{options[:pr_title]} #{date}",
      body: options[:pr_body],
      base: options[:pr_base] || "develop"
    )
    sh("git checkout #{current_branch}")
  end

Jenkinsfile
pipeline {
    triggers {
        cron('H 1 * * 1-5')
    }
    stages {
        stage('daily') {
            environment {
                GITHUB_PULL_REQUEST_API_TOKEN = YOUR_GITHUB_API_TOKEN
            }
            when {
                triggeredBy "TimerTrigger"
            }
            steps {
                echo 'Daily..'
                sh 'bundle exec fastlane daily'
            }
        }
    }
}

コードをみて貰えばそれだけなんですが、一応解説を。

やっている事はツール(bundler)の更新、swiftlintのformat、ライブラリ(CocoaPods)の更新、PRの作成です

ツール(bundler)の更新

bundle updateをshで実行します

swiftlintのformat

fastlaneからswiftlintのformatとautocorrectを実行します
formatやautocorrectはローカル(開発環境)で実行してもいいんですが
開発者によって環境依存があったり、編集中にコンパイルしちゃった時の動きが不安定だったりするので、CI実行に寄せました
各自で実行してもらっていれば、CIで差分は出ないですし、実行してなかった場合も拾えるのでいいかなと。

ライブラリ(CocoaPods)の更新

pod updateとlicense-plistの更新をしています。

PRの作成

next if system('git diff --quiet --exit-code')で差分がなければ終了
元ブランチを取っておいて、CI用のブランチをcheckout
変更をcommitして、PRを作成します。
api_tokenやrepo、api_urlなどは環境変数で渡しています
PR作成後、元のブランチをcheckoutして戻しています

Jenkins pipeline

triggersにcronを指定、月〜金の1時に実行
whenにtriggeredByで"TimerTrigger"を指定する

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