使用groovy在Jenkins“全局工具配置”中安装Snyk

w9apscun  于 2022-11-01  发布在  Jenkins
关注(0)|答案(1)|浏览(359)

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

问题是,在我手动添加安装程序并单击“保存”之前,描述符不可用。如果我不手动执行此任务(我希望避免),则会导致代码失败,并显示以下错误消息“Cannot invoke method setInstallations()on null object”
我的代码:

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")

有没有什么方法可以通过编程来完成我想做的事情?

bvjxkvbb

bvjxkvbb1#

在我的本地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还将第3个值作为整数,而它期望的是长值。
参考文献:

相关问题