我正在使用来自插件网站https://wiki.jenkins.io/display/JENKINS/Docker+Plugin的常规脚本代码来设置Jenkins Docker插件 一切都按需工作,但是脚本设计为使用单个docker模板创建单个docker cloud。当然,我可以手动将新的docker模板添加到现有的docker cloud中,但是我想使用Groovy脚本使其工作。或者,或者,如果可以使用相同的Groovy脚本使用多个Docker模板创建单个Docker云。
我尝试通过将def DockerTemplateParameters和def dockerTemplateBaseParameters模板添加到具有不同值参数的现有dockerCloud模板中来添加一个模板,但是脚本无法以这种方式工作...
/*
Automatically configure docker cloud stack in Jenkins.
Docker plugin v1.1.5
*/
//configure cloud stack
import com.nirima.jenkins.plugins.docker.DockerCloud
import com.nirima.jenkins.plugins.docker.DockerTemplate
import com.nirima.jenkins.plugins.docker.DockerTemplateBase
import
com.nirima.jenkins.plugins.docker.launcher.AttachedDockerComputerLauncher
import io.jenkins.docker.connector.DockerComputerAttachConnector
import jenkins.model.Jenkins
// parameters
def dockerCloudParameters = [
name: 'Adas-Project-Server-18',
serverUrl: 'tcp://10.10.1.2:4243',
credentialsId: '',
version: '1.26',
connectTimeout: 5,
readTimeout: 15,
dockerHostname: '',
containerCapStr: '5'
]
def DockerTemplateParameters = [
labelString: 'docker-adas-linux',
instanceCapStr: '1',
remoteFs: '/var/lib/jenkins'
]
def dockerTemplateBaseParameters = [
image: 'adas-build:linux',
bindAllPorts: false,
bindPorts: '',
cpuShares: null,
dnsString: '',
dockerCommand: '',
environmentsString: '',
extraHostsString: '',
hostname: '',
macAddress: '',
memoryLimit: null,
memorySwap: null,
network: '',
privileged: true,
pullCredentialsId: '',
sharedMemorySize: null,
tty: false,
volumesFromString: '',
volumesString: 'media:/media
/var/lib/jenkins/workspace/CCACHE/Linux/.ccache/:/usr/src/.ccache'
]
// https://github.com/jenkinsci/docker-plugin/blob/docker-plugin-
1.1.2/src/main/java/com/nirima/jenkins/plugins/docker/DockerTemplateBase.java
DockerTemplateBase dockerTemplateBase = new DockerTemplateBase(
dockerTemplateBaseParameters.image,
dockerTemplateBaseParameters.pullCredentialsId,
dockerTemplateBaseParameters.dnsString,
dockerTemplateBaseParameters.network,
dockerTemplateBaseParameters.dockerCommand,
dockerTemplateBaseParameters.volumesString,
dockerTemplateBaseParameters.volumesFromString,
dockerTemplateBaseParameters.environmentsString,
dockerTemplateBaseParameters.hostname,
dockerTemplateBaseParameters.memoryLimit,
dockerTemplateBaseParameters.memorySwap,
dockerTemplateBaseParameters.cpuShares,
dockerTemplateBaseParameters.sharedMemorySize,
dockerTemplateBaseParameters.bindPorts,
dockerTemplateBaseParameters.bindAllPorts,
dockerTemplateBaseParameters.privileged,
dockerTemplateBaseParameters.tty,
dockerTemplateBaseParameters.macAddress,
dockerTemplateBaseParameters.extraHostsString
)
// https://github.com/jenkinsci/docker-plugin/blob/docker-plugin-1.1.2/src/main/java/com/nirima/jenkins/plugins/docker/DockerTemplate.java
DockerTemplate dockerTemplate = new DockerTemplate(
dockerTemplateBase,
new DockerComputerAttachConnector(),
DockerTemplateParameters.labelString,
DockerTemplateParameters.remoteFs,
DockerTemplateParameters.instanceCapStr
)
// https://github.com/jenkinsci/docker-plugin/blob/docker-plugin-1.1.2/src/main/java/com/nirima/jenkins/plugins/docker/DockerCloud.java
DockerCloud dockerCloud = new DockerCloud(
dockerCloudParameters.name,
[dockerTemplate],
dockerCloudParameters.serverUrl,
dockerCloudParameters.containerCapStr,
dockerCloudParameters.connectTimeout,
dockerCloudParameters.readTimeout,
dockerCloudParameters.credentialsId,
dockerCloudParameters.version,
dockerCloudParameters.dockerHostname
)
// get Jenkins instance
Jenkins jenkins = Jenkins.getInstance()
// add cloud configuration to Jenkins
jenkins.clouds.add(dockerCloud)
// save current Jenkins state to disk
jenkins.save()
远远低于我的尝试(仍然无法正常工作):
我已经使用上述凹槽脚本创建了云。现在,我尝试使用@OliverGondža建议中的代码向其添加另一个docker模板:
/*
Adding new docker template into existing docker cloud stack in Jenkins.
Docker plugin v1.1.5
*/
//configure cloud stack
import com.nirima.jenkins.plugins.docker.DockerTemplate
import com.nirima.jenkins.plugins.docker.DockerTemplateBase
import io.jenkins.docker.connector.DockerComputerAttachConnector
import jenkins.model.Jenkins
// parameters
def AndroidTemplateParameters = [
labelString: 'docker-adas-android',
instanceCapStr: '1',
remoteFs: '/var/lib/jenkins'
]
def AndroidTemplateBaseParameters = [
image: 'yi/adas-build:android',
bindAllPorts: false,
bindPorts: '',
cpuShares: null,
dnsString: '',
dockerCommand: '',
environmentsString: '',
extraHostsString: '',
hostname: '',
macAddress: '',
memoryLimit: null,
memorySwap: null,
network: '',
privileged: true,
pullCredentialsId: '',
sharedMemorySize: null,
tty: false,
volumesFromString: '',
volumesString: 'media:/media /var/lib/jenkins/workspace/CCACHE/Android/.ccache/:/usr/src/.ccache'
]
// https://github.com/jenkinsci/docker-plugin/blob/docker-plugin-1.1.2/src/main/java/com/nirima/jenkins/plugins/docker/DockerTemplateBase.java
DockerTemplateBase AndroidTemplateBase = new DockerTemplateBase(
AndroidTemplateBaseParameters.image,
AndroidTemplateBaseParameters.pullCredentialsId,
AndroidTemplateBaseParameters.dnsString,
AndroidTemplateBaseParameters.network,
AndroidTemplateBaseParameters.dockerCommand,
AndroidTemplateBaseParameters.volumesString,
AndroidTemplateBaseParameters.volumesFromString,
AndroidTemplateBaseParameters.environmentsString,
AndroidTemplateBaseParameters.hostname,
AndroidTemplateBaseParameters.memoryLimit,
AndroidTemplateBaseParameters.memorySwap,
AndroidTemplateBaseParameters.cpuShares,
AndroidTemplateBaseParameters.sharedMemorySize,
AndroidTemplateBaseParameters.bindPorts,
AndroidTemplateBaseParameters.bindAllPorts,
AndroidTemplateBaseParameters.privileged,
AndroidTemplateBaseParameters.tty,
AndroidTemplateBaseParameters.macAddress,
AndroidTemplateBaseParameters.extraHostsString
)
// get Jenkins instance
Jenkins jenkins = Jenkins.getInstance()
def cloud = jenkins.clouds.getByName("Adas-Project-Server-18")
cloud.addTemplate(new DockerTemplate(
AndroidTemplateBase,
new DockerComputerAttachConnector(),
AndroidTemplateParameters.labelString,
AndroidTemplateParameters.remoteFs,
AndroidTemplateParameters.instanceCapStr
))
// save current Jenkins state to disk
jenkins.save()
当我尝试在Jenkins脚本控制台中运行以上代码时,出现以下错误:
java.lang.NullPointerException:无法调用方法addTemplate() 空对象
需要帮助修复此代码并使其起作用。 先感谢您, 伊戈尔。
答案 0 :(得分:0)
您可以通过created () {
this.data = JSON.parse(JSON.stringify(this.rt));
},
访问已配置的云,以找到要更新的云。您可以通过调用jenkins.clouds.getByName(String)
向其添加更多模板。完成后,请记住保存Jenkins。大概是这样的:
DockerCloud.addTemplate(DockerTemplate)