LoginSignup
61
53

More than 5 years have passed since last update.

Jenkinsの構築それ全部自動でできるよ

Posted at

概要

Jenkinsのインストールまでは、ansible等を使って自動化されていると思いますが、その先のプラグインのインストールやジョブの設定等の作業はJenkinsのUI上からぽちぽち手動でやっている方が多いのはないのでしょうか?

Jenkinsを1台立てるだけなら手動でもいいですが、stagingとproduction用にJenkinsを複数台構築したり、他のプロジェクトでも似たような設定のJenkinsを再度構築する必要が出てくると、毎回手動で設定するのは大変です。

また、手動の場合はどのプラグインをインストールするだとか、グローバルセキュリティの設定をどうするだとかは忘れないように手順書に記載したといった作業も発生してしまいます。

誰でも同じ設定のJenkinsを簡単に構築できるようにするためには、設定も全て自動化させてしまいましょう。

Groovyスクリプトで設定を行う

Jenkinsの設定は基本的に全てgroovyのスクリプトを使うことで自動化できます。
$JENKINS_HOME/init.groovy.d にスクリプトを置いておけば、Jenkins起動時にそのスクリプトが実行されます。

また、Jenkins2.0からはセットアップウィザードがデフォルトで有効になっているので、以下のようにJVMオプションでこれを無効化しておきます。

jenkins.install.runSetupWizard=false

それでは設定を実施するスクリプトのサンプルをいくつか載せてみます。

プラグインのインストールを実施する

import jenkins.model.*

def instance  = Jenkins.getInstance()
def plugins   = ["git", "workflow-aggregator", "ldap"]

pm = instance.getPluginManager()
uc = instance.getUpdateCenter()

uc.updateAllSites()

def enablePlugin(pluginName) {
  if (! pm.getPlugin(pluginName)) {
    deployment = uc.getPlugin(pluginName).deploy(true)
    deployment.get()
  }

  def plugin = pm.getPlugin(pluginName)
  if (! plugin.isEnabled()) {
    plugin.enable()
  }

  plugin.getDependencies().each {
    enablePlugin(it.shortName)
  }
}

plugins.each {
  def plugin = pm.getPlugin(it)
  enablePlugin(it)
}


グローバル環境変数を設定する

import jenkins.model.*
import org.jenkinsci.plugins.*

def instance    = Jenkins.getInstance()
def globalProps = instance.getGlobalNodeProperties()
def props       = globalProps.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)

def environments = ["AWS_ACCESS_KEY":"xxxxxx", "AWS_SECRET_KEY":"xxxxxx"]

environments.each {
  if (props.isEmpty()) {
    globalProps.add(new hudson.slaves.EnvironmentVariablesNodeProperty(
      new hudson.slaves.EnvironmentVariablesNodeProperty.Entry(it.key, it.value )
    ))
  } else {
    props.get(0).envVars.put(it.key, it.value)
  }
}

instance.save()

SSHクレデンシャルを設定する


import jenkins.model.*
import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.common.*
import com.cloudbees.plugins.credentials.domains.*
import com.cloudbees.jenkins.plugins.sshcredentials.impl.*
import hudson.plugins.sshslaves.*;

String private_key = '''
-----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxx
-----END RSA PRIVATE KEY-----
'''

global_domain = Domain.global()
credentials_store = Jenkins.instance.getExtensionList(
    'com.cloudbees.plugins.credentials.SystemCredentialsProvider'
)[0].getStore()

credentials = new BasicSSHUserPrivateKey(
  CredentialsScope.GLOBAL,
  "root",
  "root",
  new BasicSSHUserPrivateKey.DirectEntryPrivateKeySource("${private_key}"),
  "",
  ""
)
credentials_store.addCredentials(global_domain, credentials)

ジョブのインポートを実施する

import jenkins.model.*

def instance  = Jenkins.getInstance()

def jobName = "job.name"
def configXml = new File("your_config.xml_path").text
def xmlStream = new ByteArrayInputStream( configXml.getBytes() )
instance.createProjectFromXML(jobName, xmlStream)

まとめ

上記のように簡単なgroovyのスクリプトを書くだけでJenkinsの構築、設定が全て自動でできます。
ansibleを使って構築している場合は、ansible2.3からjenkins_scriptモジュールがサポートされているので、こちらと組み合わせることでよりスマートに構成管理と自動構築ができるでしょう。

Jenkinsの構築を自動化しておくことで、チームのJenkinsおじさんに頼る必要なく誰でもできるようになりました。

61
53
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
61
53