2017-06-02 322 views

回答

3

你可以把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}" 
     } 
     } 
    } 
    } 
} 

可生产控制台日志:

[Pipeline] echo 
look at this: so_true 
[Pipeline] echo 
currentBuild.number: 84 

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

+0

我不认为这种方式的脚本是在主人身上执行的,而是受詹金斯的沙箱模式的约束?我对“Execute system groovy script”的系统部分超级感兴趣:-D – C4stor

+0

它在master上执行,它受jenkins的沙盒模式的约束,可以根据每个案例的基本情况重写方法在https://medium.com/dronzebot/jenkins-groovy-script-approval-eff569f597f6中描述。 – burnettk

+0

好吧,我的坏,伟大然后:) – C4stor

相关问题