Jenkins 使用Restful 接口方式,执行Groovy Script进行系统配置

Jenkins 使用Restful 接口方式,执行Groovy Script进行系统配置(manage–>configure)

目的:绕过Jenkins提供的UI,进行Jenkins插件服务配置。

场景:由于部分插件比如sonar、gitlab等插件,需要在系统配置中进行全局配置,对于Jenkins而言,插件配置模块的页面是由插件提供的*.Jelly文件生成,Jenkins只是进行了统一提交,具体是由插件进行扩展和实现的,所以没有单独的API可以灵活操作。

解决:使用Jenkins Restful接口发送Groovy脚本,调用插件方法来实现自定义操作。

下面以简单的sonar配置为例子,调用插件方法,进行简单的CRUD。

Restful请求方式见参考网址,示例脚本如下:

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

你可能感兴趣的:(工具使用)