Jenkins Groovy ScriptでThrottle Concurrent Builds Pluginのカテゴリーを設定する

ジョブ設定が自動化されててもjenkins設定自体が自動化されてないと意味がない!ということでちょっと自動化してみました。

対象

今回の対象は Throttle Concurrent Builds Plugin - Jenkins - Jenkins Wiki 用のシステム設定で、以下のように表示される部分です。 ※ pluginは v2.0.1で確認しています。

f:id:matsu_chara:20171221132459p:plain

スクリプト

スクリプトコンソールで以下のスクリプトを実行するとカテゴリーや最大実効数が設定されます。

// jenkinsのスクリプトコンソールで実行する場合はimportは省略可能
import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*

def instance = Jenkins.getInstance()
def throttlePluginDescriptor = instance.getDescriptor("hudson.plugins.throttleconcurrents.ThrottleJobProperty")
def deployAllCategory = new hudson.plugins.throttleconcurrents.ThrottleJobProperty.ThrottleCategory("foo", 4, 4, [])

throttlePluginDescriptor.setCategories([deployAllCategory])
throttlePluginDescriptor.save()
throttlePluginDescriptor.load()

setCategoriesなどのメソッドはソース ( https://github.com/jenkinsci/throttle-concurrent-builds-plugin/blob/throttle-concurrents-2.0.1/src/main/java/hudson/plugins/throttleconcurrents/ThrottleJobProperty.java ) から探すのが良さそうです。(descriptorを呼んでやればよいということを探し当てるのに時間がかかった・・。)

プラグイン設定の自動化は他にも https://github.com/glenjamin/jenkins-groovy-examples のような方法もありそうでした。

まとめ

実行はansibleのjenkins_scriptプラグインjenkins_script - Executes a groovy script in the jenkins instance — Ansible Documentationなんかを使えそうです。

ansibleならpluginは jenkins_plugin - Add or remove Jenkins plugin — Ansible Documentation を使えば自動化できますし、 環境変数やクレデンシャルなども Jenkinsの構築それ全部自動でできるよ - Qiita を参考にjenkins_scriptプラグインで自動化できそうなので、だいたいのものは設定できそうです。