declarative pipeline syntax seems limited, am I missing something?

51 views
Skip to first unread message

Gabe Nydick

unread,
Aug 30, 2018, 5:04:15 PM8/30/18
to jenkins...@googlegroups.com
How do I replicate the following functionality? I can't seem to find a way


def myRepo = checkout scm
def gitCommit = myRepo.GIT_COMMIT
def gitBranch = myRepo.GIT_BRANCH
def shortGitCommit = "${gitCommit[0..10]}"
def previousGitCommit = sh(script: "git rev-parse ${gitCommit}~", returnStdout: true)
def dockerImage = "$image:${gitCommit}"
def dockerLatest = "$image:latest"
def dockerfile = 'base.Dockerfile'

Damien Coraboeuf

unread,
Aug 31, 2018, 2:29:16 AM8/31/18
to jenkins...@googlegroups.com
Hi,

You can put this in a `script` section, and put what you need in environment variables for the rest of the stages / steps:

steps {
   script {
      ...
      def shortGitCommit = "${gitCommit[0..10]}" as String
      env.SHORT_GIT_COMMIT = shortGitCommit
      ...
   }
   echo "Short Git Commit = $SHORT_GIT_COMMIT"
}

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CACTWNKBUW6koy37j1ELe1PbBZifd8MMEpyVL%2BwbeeUGV06RKCw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Gabe Nydick

unread,
Aug 31, 2018, 4:36:04 PM8/31/18
to jenkins...@googlegroups.com
Thanks. From what I've read, it seems like the declarative way of doing things is authoritatively described as being more flexible, but things that used to be common place, now all have to be shelled out. What's the point if you're now "declarative" but everything you do is in scripts?

Baptiste Mathus

unread,
Sep 6, 2018, 8:56:37 AM9/6/18
to jenkins...@googlegroups.com
>  authoritatively described as being more flexible

Well, if you did read this somewhere, please provide the link, we should update and fix this.
I suppose it depends on what people put behind the term "flexible", but in my mind, no that is not the promise of Declarative.

Declarative is about making Pipelines generally more accessible to beginners, and much more standard between teams and companies. That goal is mostly about actually lowering flexibility, but at the same time removing also a lot of surprises that people discover along the way using Scripted.

For your issue, you can still use Shared libs, and yes you can (and many of argue, should) extract many things in shell scripts or cli tools that can be used outside of Jenkins.
Pipeline is an awesome orchestration tool/language, but you really want to get away from using it like a full blown programming language (which many people do using Scripted, because they can :). And they can't with Declarative, which is good overall).

My 2 cents

Reply all
Reply to author
Forward
0 new messages