问题描述
我正在尝试为Jenkins创建我的第一个Groovy脚本:
I'm trying to create my first Groovy script for Jenkins:
在这里查看 https://jenkins.io/doc/book/pipeline/ ,我创建了这个:
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"
}
}
}
但是,我得到了:
java.lang.NoSuchMethodError: No such DSL method "stages" found among steps
我想念什么?
此外,如何在不以纯文本形式编写密码的情况下将我的凭据传递到Git存储库?
Also, how can I pass my credentials to the Git Repository without writing the password in plain text?
推荐答案
您正在将Scripted Pipeline
与Declarative Pipeline
混淆并混合,以完全区别请参阅此处.但简短的故事:
You are confusing and mixing Scripted Pipeline
with Declarative Pipeline
, for complete difference see here. But the short story:
- 声明性管道是管道DSL的新扩展(基本上是只有一步的管道脚本,带有参数的管道步骤(称为指令),这些指令应遵循特定的语法.这种新格式的要点是,它更加严格,因此对于管道新手来说应该更容易,可以进行图形编辑等等.
- 脚本化管道是高级需求的后备.
- 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.
因此,如果我们查看您的脚本,则首先打开一个来自脚本管道的node
步骤.然后,使用stages
,它是declarative pipeline
中定义的pipeline
步骤的指令之一.因此,您可以例如编写:
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"
}
}
}
}
因此,如果您想使用declarative pipeline
,那就是这种方式.
So if you want to use declarative pipeline
that is the way to go.
如果要scripted pipeline
,请输入:
node {
stage('HelloWorld') {
echo 'Hello World'
}
stage('git clone') {
git clone "ssh://[email protected]/myrepo.git"
}
}
例如:跳过stages块.
E.g.: skip the stages block.
这篇关于没有这种DSL方法的“阶段"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!