Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
609 views
in Technique[技术] by (71.8m points)

jenkins - no such DSL method `stages`

I'm trying to create my first Groovy script for Jenkins:

After looking here https://jenkins.io/doc/book/pipeline/, I created this:

node {
  stages {

    stage('HelloWorld') {
      echo 'Hello World'
    }

    stage('git clone') {
      git clone "ssh://[email protected]/myrepo.git"
    }

  }
}

However, I'm getting:

java.lang.NoSuchMethodError: No such DSL method "stages" found among steps

What am I missing?

Also, how can I pass my credentials to the Git Repository without writing the password in plain text?

question from:https://stackoverflow.com/questions/42113655/no-such-dsl-method-stages

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are confusing and mixing Scripted Pipeline with Declarative Pipeline, for complete difference see here. But the short story:

  • declarative pipelines is a new extension of the pipeline DSL (it is basically a pipeline script with only one step, a pipeline step with arguments (called directives), these directives should follow a specific syntax. The point of this new format is that it is more strict and therefor should be easier for those new to pipelines, allow for graphical editing and much more.
  • scripted pipelines is the fallback for advanced requirements.

So, if we look at your script, you first open a node step, which is from scripted pipelines. Then you use stages which is one of the directives of the pipeline step defined in declarative pipeline. So you can for example write:

pipeline {
  ...
  stages {
    stage('HelloWorld') {
      steps {
        echo 'Hello World'
      }
    }
    stage('git clone') {
      steps {
        git clone "ssh://[email protected]/myrepo.git"
      }
    }
  }
}

So if you want to use declarative pipeline that is the way to go.

If you want to scripted pipeline, then you write:

node {
  stage('HelloWorld') {
    echo 'Hello World'
  }

  stage('git clone') {
    git clone "ssh://[email protected]/myrepo.git"
  }
}

E.g.: skip the stages block.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...