简体   繁体   中英

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 ?

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:

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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM