JenkinsはRestfulインタフェース方式を用いてGroovy Scriptを実行してシステム構成を行う

5911 ワード

JenkinsはRestfulインタフェース方式を用いてGroovy Scriptを実行してシステム構成を行う(manage->configure)
目的:Jenkinsが提供するUIを迂回してJenkinsプラグインサービス構成を行う.
シーン:一部のプラグイン、例えばsonar、gitlabなどのプラグインは、システム構成においてグローバル構成を行う必要があるため、Jenkinsにとって、プラグイン構成モジュールのページはプラグインから提供される*.Jellyファイルから生成され、Jenkinsは統一的に提出されただけで、具体的にはプラグインから拡張され実現されたので、単独のAPIは柔軟に操作できない.
解決:Jenkins Restfulインタフェースを使用してGroovyスクリプトを送信し、プラグインメソッドを呼び出してカスタム操作を実現します.
以下では簡単なsonar構成を例に,プラグインメソッドを呼び出し,簡単なCRUDを行う.
Restful要求方式は参考URLを参照し、サンプルスクリプトは以下の通りである.
import jenkins.model.*
import hudson.plugins.sonar.*
import hudson.plugins.sonar.model.TriggersConfig

def mode = "xxx"

if (mode == "update") {
    println("Update method: ")
    doUpdate()
} else if (mode == "add") {
    println("add method: ")
    doAdd()
} else if (mode == "delete") {
    println("delete method")
    doDelete()
} else {
    println("get sonar config")
    doGet()
}

//update target sonar config
def doUpdate() {

    def jenkins_instance = Jenkins.getInstance()
    def sonar_global_conf = jenkins_instance.getDescriptor(SonarGlobalConfiguration.class)
    def sonar_installations = sonar_global_conf.getInstallations()

    def target_sonar_name = "test-sonar"
    def new_sonar_name = "updated name"

    sonar_installations.each {
        current_inst = (SonarInstallation) it
        if (current_inst.getName() == target_sonar_name) {
            println("Update existing sonar installation: " + current_inst.getName())

            updated_inst = new SonarInstallation(
                    new_sonar_name,
                    current_inst.getServerUrl(),
                    current_inst.getServerAuthenticationToken().toString(),
                    current_inst.getMojoVersion(),
                    current_inst.getAdditionalProperties(),
                    current_inst.getTriggers(),
                    current_inst.getAdditionalAnalysisProperties()
            )

            //remove current target and add updated.
            sonar_installations -= current_inst
            sonar_installations += updated_inst
        } else {
            println("existing sonar installation: do not update" + current_inst.getName())
        }
    }

    //update config
    sonar_global_conf.setInstallations((SonarInstallation[]) sonar_installations)
    sonar_global_conf.save()
    jenkins_instance.save()
}

//add target sonar config
def doAdd() {
    def jenkins_instance = Jenkins.getInstance()
    def sonar_global_conf = jenkins_instance.getDescriptor(SonarGlobalConfiguration.class)
    def sonar_installations = sonar_global_conf.getInstallations()

    def sonar_name = "New setting"
    def sonar_server_url = "http://sonarqube:9000"
    def sonar_auth_token = ""
    def sonar_mojo_version = ''
    def sonar_additional_properties = ''
    def sonar_triggers = new TriggersConfig()
    def sonar_additional_analysis_properties = ''

    sonar_installations.each {
        current_inst = (SonarInstallation) it
        if (current_inst.getName() == sonar_name) {
            println("existing sonar installation:" + sonar_name)
        } else {
            new_inst = new SonarInstallation(
                    sonar_name,
                    sonar_server_url,
                    sonar_auth_token,
                    sonar_mojo_version,
                    sonar_additional_properties,
                    sonar_triggers,
                    sonar_additional_analysis_properties
            )
            sonar_installations += new_inst
            println("add new  sonar installation success :" + sonar_name)
        }
    }

    //update config
    sonar_global_conf.setInstallations((SonarInstallation[]) sonar_installations)
    sonar_global_conf.save()
    jenkins_instance.save()
}

//delete target sonar config
def doDelete() {
    def jenkins_instance = Jenkins.getInstance()
    def sonar_global_conf = jenkins_instance.getDescriptor(SonarGlobalConfiguration.class)
    def sonar_installations = sonar_global_conf.getInstallations()

    def sonar_name = "New setting"
    def isDeleted = false

    sonar_installations.each {
        current_inst = (SonarInstallation) it
        if (current_inst.getName() == sonar_name) {
            sonar_installations -= current_inst
            isDeleted = true
            println("delete target sonar installation success:" + sonar_name)
        }
    }

    if (!isDeleted) {
        println("the target sonar is not find :" + sonar_name)
    }

    //update config
    sonar_global_conf.setInstallations((SonarInstallation[]) sonar_installations)
    sonar_global_conf.save()
    jenkins_instance.save()
}

//get sonar config
def doGet() {
    def jenkins_instance = Jenkins.getInstance()
    def sonar_global_conf = jenkins_instance.getDescriptor(SonarGlobalConfiguration.class)
    def sonar_installations = sonar_global_conf.getInstallations()

    sonar_installations.each {
        current_inst = (SonarInstallation) it
        println("GET existing sonar installation: " + current_inst.getName())
        println("GET existing sonar installation: " + current_inst.getServerUrl())
        println("GET existing sonar installation: " + current_inst.getServerAuthenticationToken().toString())
        println("GET existing sonar installation: " + current_inst.getMojoVersion())
        println("GET existing sonar installation: " + current_inst.getAdditionalProperties())
        println("GET existing sonar installation: " + current_inst.getTriggers())
        println("GET existing sonar installation: " + current_inst.getAdditionalAnalysisProperties())
    }

}





参考サイト:https://wiki.jenkins.io/display/JENKINS/Jenkins+Script+Console