【问题标题】:Jenkins: "Execute system groovy script" in a pipeline step (SCM committed)Jenkins:管道步骤中的“执行系统常规脚本”(SCM 已提交)
【发布时间】:2017-06-02 15:23:40
【问题描述】:

有没有办法从 SCM 提交的管道文件中使用 Jenkins“执行系统常规脚本”步骤?

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

如果不是,我是否可以通过其他方式复制该功能,例如使用共享库插件?

谢谢!

【问题讨论】:

    标签: jenkins groovy


    【解决方案1】:

    您可以将 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
    

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

    【讨论】:

    • 我不认为以这种方式放置的脚本是在 master 上执行的,而是受到 jenkins 沙盒模式的约束?我对“执行系统常规脚本”的系统部分非常感兴趣:-D
    • 它在 master 上执行,并且受 jenkins 沙盒模式的约束,可以根据 medium.com/dronzebot/… 中描述的方法逐个案例覆盖。
    • 好吧,我的错,那就太好了:)
    • @burnettk,如果我的 groovy 代码中有导入,它会起作用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 2018-02-06
    • 2019-05-11
    相关资源
    最近更新 更多