3

Is it possible to create a pipeline inside a groovy script via a function call?

If I have a script such as:

def call() {
  pipeline {
    agent any
    stages { ... }
  }
}

then I can call this script in Jenkins with:

@Library('foo') _

foo()

And I see the pipeline running with all of its stages.

But if I change the script so that the pipeline step is outside of the call() method and in its own method:

def build() {
  pipeline {
    agent any
    stages {
      ...
    }
  }
}

And call it via Jenkins:

@Library('foo') _

foo.build()

then I get the following error:

java.lang.NoSuchMethodError: No such DSL method 'agent' found among steps [...]

I'd really like to have the one script with build(), deploy(), release(), etc methods, each defining their own pipeline, but it seems that a pipeline can only be created via the call() method, and I'll need to have separate scripts for each pipeline (build.groovy, deploy.groovy, etc).

  • You could build a job from a running job, look at [this](https://stackoverflow.com/questions/36306883/how-can-i-trigger-another-job-from-a-jenkins-pipeline-jenkinsfile-with-github). – ReThibau Nov 19 '19 at 23:54

0 Answers0