Jenkins:管道步骤中的"执行系统groovy脚本"(SCM已提交)

C4s*_*tor 2 groovy jenkins

有没有办法从SCM提交的管道文件中使用Jenkins"Execute system groovy script"步骤?

如果是,我将如何访问其中的预定义变量(如构建)?

如果不是,我是否能够使用例如共享库插件复制功能?

谢谢 !

bur*_*ttk 5

您可以将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}"
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

生成控制台日志:

[Pipeline] echo
look at this: so_true
[Pipeline] echo
currentBuild.number: 84
Run Code Online (Sandbox Code Playgroud)

单击任何管道作业左侧导航中的"管道语法"链接,以获取可在"全局变量参考"中访问的一些示例.