【问题标题】:Install Snyk in Jenkins "Global Tool Configuration" using groovy使用 groovy 在 Jenkins“全局工具配置”中安装 Snyk
【发布时间】:2022-06-15 16:10:00
【问题描述】:

我正在尝试使用 groovy 将 Snyk 安装添加到 Jenkins。插件安装完毕,可以在全局工具配置中看到安装选项:

问题是描述符在我手动添加安装程序并单击保存之前不可用。如果我不手动执行此任务(我想阻止),它会导致我的代码失败并显示以下错误消息“无法在 null 对象上调用方法 setInstallations()”

我的代码:

import hudson.model.*
import jenkins.model.*
import hudson.tools.*
import hudson.tasks.*
import io.snyk.jenkins.tools.SnykInstaller
import io.snyk.jenkins.tools.SnykInstallation

def snyk_name = "Snyk"
def snyk_home = ""
def snyk_installer = new SnykInstaller("", "latest", 24)
def snyk_properties = new InstallSourceProperty([snyk_installer])
def instance = Jenkins.getInstance()

println("[init.groovy.d] START Configuring Snyk Installation...")
// Get the GlobalConfiguration descriptor of Snyk plugin.
def snyk_conf = instance.getDescriptor("io.snyk.jenkins.SnykStepBuilder.SnykStepBuilderDescriptor")

def snyk_inst = new SnykInstallation(
    snyk_name,
    snyk_home,
    [snyk_properties]
)

// Only add the new Snyk setting if it does not exist - do not overwrite existing config
def snyk_installations = snyk_conf.getInstallations()

def snyk_inst_exists = false
snyk_installations.each {
    installation = (SnykInstallation) it
    if (snyk_inst.getName() == installation.getName()) {
        snyk_inst_exists = true
        println("Found existing installation: " + installation.getName())
    }
}
if (!snyk_inst_exists) {
    snyk_installations += snyk_inst
    snyk_conf.setInstallations((SnykInstallation[]) snyk_installations)
    snyk_conf.save()
}


// Save the state
instance.save()

println("[init.groovy.d] END")

有什么方法可以以编程方式做我想做的事吗?

【问题讨论】:

    标签: jenkins groovy


    【解决方案1】:

    在我的本地 Jenkins (v 2.263.1) 上测试了你的 groovy 之后,我想出了以下对我有用的方法:

    import hudson.model.*
    import jenkins.model.*
    import hudson.tools.*
    import hudson.tasks.*
    import io.snyk.jenkins.tools.*
    
    def instance = Jenkins.getInstance()
    def snyk_name = "SnykLatest"
    def snyk_home = ""
    def snyk_installer = new SnykInstaller("", "latest", 24L, null)
    def snyk_properties = new InstallSourceProperty([snyk_installer])
    
    println("[init.groovy.d] START Configuring Snyk Installation...")
    // Get the GlobalConfiguration descriptor of Snyk plugin.
    def snyk_conf = instance.getDescriptor("io.snyk.jenkins.tools.SnykInstallation")
    
    def snyk_inst = new SnykInstallation(
        snyk_name,
        snyk_home,
        [snyk_properties]
    )
    
    // Only add the new Snyk setting if it does not exist - do not overwrite existing config
    def snyk_installations = snyk_conf.getInstallations()
    
    def snyk_inst_exists = false
    snyk_installations.each {
        installation = (SnykInstallation) it
        if (snyk_inst.getName() == installation.getName()) {
            snyk_inst_exists = true
            println("Found existing installation: " + installation.getName())
        }
    }
    if (!snyk_inst_exists) {
        snyk_installations += snyk_inst
        snyk_conf.setInstallations((SnykInstallation[]) snyk_installations)
        snyk_conf.save()
    }
    
    
    // Save the state
    instance.save()
    
    println("[init.groovy.d] END")
    

    在基本术语中,SnykInstaller 期望 4 个值而不是 3。当 Groovy 期望一个长值时,Groovy 也将第 3 个值作为整数。

    参考资料:

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2018-03-28
    • 2017-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    相关资源
    最近更新 更多