oct*_*ian 25 groovy jenkins jenkins-pipeline
我正在尝试为Jenkins创建我的第一个Groovy脚本:
看了这里https://jenkins.io/doc/book/pipeline/后,我创建了这个:
node {
stages {
stage('HelloWorld') {
echo 'Hello World'
}
stage('git clone') {
git clone "ssh://git@mywebsite.com/myrepo.git"
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我得到了:
java.lang.NoSuchMethodError: No such DSL method "stages" found among steps
我错过了什么?
另外,如何在不用纯文本编写密码的情况下将我的凭据传递给Git存储库?
Jon*_*n S 62
你感到困惑和混合Scripted Pipeline
使用Declarative Pipeline
,完全的差异在这里看到.但短篇小说:
因此,如果我们查看您的脚本,您首先打开一个node
步骤,该步骤来自脚本管道.然后你使用stages
哪个是在中pipeline
定义的步骤的指令之一declarative pipeline
.所以你可以写一下:
pipeline {
...
stages {
stage('HelloWorld') {
steps {
echo 'Hello World'
}
}
stage('git clone') {
steps {
git clone "ssh://git@mywebsite.com/myrepo.git"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以如果你想使用declarative pipeline
那就是要走的路.
如果你愿意scripted pipeline
,那么你写:
node {
stage('HelloWorld') {
echo 'Hello World'
}
stage('git clone') {
git clone "ssh://git@mywebsite.com/myrepo.git"
}
}
Run Code Online (Sandbox Code Playgroud)
例如:跳过阶段块.
M-R*_*avi 12
Jenkinsfile 可以使用两种类型的语法编写 -声明式和脚本式。
声明式管道和脚本式管道的构造根本不同。声明式流水线是 Jenkins 流水线的一个更新的特性,它:
提供比 Scripted Pipeline 语法更丰富的语法特性,以及
旨在使编写和阅读流水线代码更容易。
然而,许多写入 Jenkinsfile 的单个语法组件(或“步骤”)对于声明式和脚本式流水线都是通用的。例子:
在声明式流水线语法中,pipeline
块定义了在整个流水线中完成的所有工作。
Jenkinsfile(声明式管道):
pipeline {
agent any 1
stages {
stage('Build') { 2
steps {
// 3
}
}
stage('Test') { 4
steps {
// 5
}
}
stage('Deploy') { 6
steps {
// 7
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在脚本化流水线语法中,一个或多个node
块在整个流水线中完成核心工作。虽然这不是 Scripted Pipeline 语法的强制性要求,但将流水线的工作限制在node
块内有两件事:
通过将项目添加到 Jenkins 队列来安排块中包含的步骤运行。一旦执行程序在节点上空闲,这些步骤就会运行。
创建一个工作区(特定于该特定管道的目录),可以在其中对从源代码管理检出的文件进行工作。
注意:根据您的 Jenkins 配置,某些工作区在一段时间不活动后可能不会自动清理。有关更多信息,请参阅从JENKINS-2111链接的票证和讨论。
Jenkinsfile(脚本管道):
node { 1
stage('Build') { 2
// 3
}
stage('Test') { 4
// 5
}
stage('Deploy') { 6
// 7
}
}
Run Code Online (Sandbox Code Playgroud)
stage
块在脚本化流水线语法中是可选的。但是,stage
在脚本式流水线中实现块可以更清晰地可视化 Jenkins UI 中每个“阶段”的任务/步骤子集。这是Jenkinsfile
使用声明性的示例,它是等效的脚本式流水线语法:
Jenkinsfile(声明式管道):
pipeline {
agent any
options {
skipStagesAfterUnstable()
}
stages {
stage('Build') {
steps {
sh 'make'
}
}
stage('Test'){
steps {
sh 'make check'
junit 'reports/**/*.xml'
}
}
stage('Deploy') {
steps {
sh 'make publish'
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Jenkinsfile(脚本管道):
node {
stage('Build') {
sh 'make'
}
stage('Test') {
sh 'make check'
junit 'reports/**/*.xml'
}
if (currentBuild.currentResult == 'SUCCESS') {
stage('Deploy') {
sh 'make publish'
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
42246 次 |
最近记录: |