Jenkins 管道中作业 DSL 中的工作区路径
Workspace path in job DSL within Jenkins pipeline
我正在创建一个 jenkins 管道作业以使用 jenkins 作业 DSL 插件播种作业。如何获取 DSL 文件中的工作区路径?詹金斯管道代码是这样的:
#!groovy
node{
stage("build jobs"){
ws{
git poll: true, credentialsId: 'xxx', url: 'ssh://git@aaaaa.cc.xxx.com:/xxx/xxx.git'
checkout scm
jobDsl(removedJobAction: 'DISABLE', removedViewAction: 'DELETE', targets: 'jobs/*.groovy', unstableOnDeprecation: true)
}
}
}
失败的 DSL 代码是:
hudson.FilePath workspace = hudson.model.Executor.currentExecutor().getCurrentWorkspace()
出现错误:
Processing DSL script pipeline.groovy
java.lang.NullPointerException: Cannot invoke method getCurrentWorkspace() on null object
at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:91)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:35)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at pipeline.run(pipeline.groovy:1)
at pipeline$run.call(Unknown Source)
在作业 DSL 步骤中无法访问管道区域中创建的变量
您可以在作业 DSL 脚本中使用 __FILE__
变量来获取当前脚本的路径。也许您可以使用它来派生工作区目录。有关详细信息,请参阅 Script Location。
def scriptDir = new File(__FILE__).parent.absolutePath
我偶然发现了这个,因为似乎没有什么好办法。这是我的做法:
node {
stage('test') {
sh 'pwd > workspace.txt'
jobDsl scriptText: '''
String workspace = readFileFromWorkspace('workspace.txt').trim()
def file = new File(workspace, 'test.txt')
file.append('It worked!')'''
}
}
所以先抓取pipeline脚本中的workspace,然后传给job dsl。如果您的脚本中需要的不仅仅是工作区变量,我建议您通过属性文件进行传输:
node {
stage('test') {
sh 'echo "workspace="$(pwd) > build.properties'
jobDsl scriptText: '''
Properties props = new Properties();
props.load(streamFileFromWorkspace('build.properties'))
def file = new File(props.getProperty('workspace'), 'test.txt')
file.append('It worked!')'''
}
}
这可以通过使用SEED_JOB
变量来实现:
String workspacePath = SEED_JOB.lastBuild.checkouts[0].workspace
在project's wiki中有描述:
Access to the seed job is available through the SEED_JOB variable. The variable contains a reference to the internal Jenkins object that
represents the seed job. The actual type of the object depends on the
type of job that runs the DSL. For a freestyle project, the object is
an instance of hudson.model.FreeStyleProject. See the Jenkins API
Documentation for details.
The SEED_JOB variable is only available in scripts, not in any classes
used by a script. And it is only available when running in Jenkins,
e.g. in the "Process Job DSLs" build step.
The following example show how to apply the same quiet period for a
generated job as for the seed job.
job('example') {
quietPeriod(SEED_JOB.quietPeriod) }
您可以将工作区参数传递给作业 dsl。例如:
流水线代码如下:
node {
step([
$class: 'ExecuteDslScripts',
scriptText: 'job("example-2")'
])
step([
$class: 'ExecuteDslScripts',
targets: ['jobs/projectA/*.groovy', 'jobs/common.groovy'].join('\n'),
removedJobAction: 'DELETE',
removedViewAction: 'DELETE',
lookupStrategy: 'SEED_JOB',
additionalClasspath: ['libA.jar', 'libB.jar'].join('\n'),
additionalParameters: [
message: 'Hello from pipeline',
credentials: 'SECRET'
WORKSPACE: env.WORKSPACE
]
])
}
https://github.com/jenkinsci/job-dsl-plugin/wiki/User-Power-Moves#use-job-dsl-in-pipeline-scripts
我正在创建一个 jenkins 管道作业以使用 jenkins 作业 DSL 插件播种作业。如何获取 DSL 文件中的工作区路径?詹金斯管道代码是这样的:
#!groovy
node{
stage("build jobs"){
ws{
git poll: true, credentialsId: 'xxx', url: 'ssh://git@aaaaa.cc.xxx.com:/xxx/xxx.git'
checkout scm
jobDsl(removedJobAction: 'DISABLE', removedViewAction: 'DELETE', targets: 'jobs/*.groovy', unstableOnDeprecation: true)
}
}
}
失败的 DSL 代码是:
hudson.FilePath workspace = hudson.model.Executor.currentExecutor().getCurrentWorkspace()
出现错误:
Processing DSL script pipeline.groovy
java.lang.NullPointerException: Cannot invoke method getCurrentWorkspace() on null object
at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:91)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:35)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at pipeline.run(pipeline.groovy:1)
at pipeline$run.call(Unknown Source)
在作业 DSL 步骤中无法访问管道区域中创建的变量
您可以在作业 DSL 脚本中使用 __FILE__
变量来获取当前脚本的路径。也许您可以使用它来派生工作区目录。有关详细信息,请参阅 Script Location。
def scriptDir = new File(__FILE__).parent.absolutePath
我偶然发现了这个,因为似乎没有什么好办法。这是我的做法:
node {
stage('test') {
sh 'pwd > workspace.txt'
jobDsl scriptText: '''
String workspace = readFileFromWorkspace('workspace.txt').trim()
def file = new File(workspace, 'test.txt')
file.append('It worked!')'''
}
}
所以先抓取pipeline脚本中的workspace,然后传给job dsl。如果您的脚本中需要的不仅仅是工作区变量,我建议您通过属性文件进行传输:
node {
stage('test') {
sh 'echo "workspace="$(pwd) > build.properties'
jobDsl scriptText: '''
Properties props = new Properties();
props.load(streamFileFromWorkspace('build.properties'))
def file = new File(props.getProperty('workspace'), 'test.txt')
file.append('It worked!')'''
}
}
这可以通过使用SEED_JOB
变量来实现:
String workspacePath = SEED_JOB.lastBuild.checkouts[0].workspace
在project's wiki中有描述:
Access to the seed job is available through the SEED_JOB variable. The variable contains a reference to the internal Jenkins object that represents the seed job. The actual type of the object depends on the type of job that runs the DSL. For a freestyle project, the object is an instance of hudson.model.FreeStyleProject. See the Jenkins API Documentation for details.
The SEED_JOB variable is only available in scripts, not in any classes used by a script. And it is only available when running in Jenkins, e.g. in the "Process Job DSLs" build step.
The following example show how to apply the same quiet period for a generated job as for the seed job.
job('example') { quietPeriod(SEED_JOB.quietPeriod) }
您可以将工作区参数传递给作业 dsl。例如:
流水线代码如下:
node {
step([
$class: 'ExecuteDslScripts',
scriptText: 'job("example-2")'
])
step([
$class: 'ExecuteDslScripts',
targets: ['jobs/projectA/*.groovy', 'jobs/common.groovy'].join('\n'),
removedJobAction: 'DELETE',
removedViewAction: 'DELETE',
lookupStrategy: 'SEED_JOB',
additionalClasspath: ['libA.jar', 'libB.jar'].join('\n'),
additionalParameters: [
message: 'Hello from pipeline',
credentials: 'SECRET'
WORKSPACE: env.WORKSPACE
]
])
}
https://github.com/jenkinsci/job-dsl-plugin/wiki/User-Power-Moves#use-job-dsl-in-pipeline-scripts