[英]Jenkins: “Execute system groovy script” in a pipeline step (SCM committed)
Is there a way to use the Jenkins "Execute system groovy script" step from a pipeline file which is SCM commited ? 有没有办法从SCM提交的管道文件中使用Jenkins“Execute system groovy script”步骤?
If yes, how would I access the predefined variables (like build) in it ? 如果是,我将如何访问其中的预定义变量(如构建)?
If no, would I be able to replicate the functionality otherwise, using for example the Shared Library Plugin ? 如果不是,我是否能够使用例如共享库插件复制功能?
Thanks ! 谢谢 !
You can put groovy code in a pipeline in a (always-source-controlled) Jenkinsfile, like this: 您可以将groovy代码放在(始终源控制的)Jenkinsfile的管道中,如下所示:
pipeline {
agent { label 'docker' }
stages {
stage ('build') {
steps {
script {
// i used a script block because you can jam arbitrary groovy in here
// without being constrained by the declarative Jenkinsfile DSL
def awesomeVar = 'so_true'
print "look at this: ${awesomeVar}"
// accessing a predefined variable:
echo "currentBuild.number: ${currentBuild.number}"
}
}
}
}
}
Produces console log: 生成控制台日志:
[Pipeline] echo
look at this: so_true
[Pipeline] echo
currentBuild.number: 84
Click on the "Pipeline Syntax" link in the left navigation of any of pipeline job to get a bunch of examples of things you can access in the "Global Variables Reference." 单击任何管道作业左侧导航中的“管道语法”链接,以获取可在“全局变量参考”中访问的一些示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.