【注意】最后更新于 December 29, 2021,文中内容可能已过时,请谨慎使用。
Jenkins Pipeline 的语法分為两类,Declarative Pipeline(声明式) 和 Scripted Pipeline, Scripted类型基于groovy比较灵活,但是上手难度比较高,官方现在主推Declarative
声明式基础语法
声明式的语法基本遵从 pipeline->stages->stage->steps
结构,下面是一个简单的例子
1
2
3
4
5
6
7
8
9
10
11
|
pipeline {
agent any
stages {
stage('Build') {
steps {
sh './gradlew clean build'
archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true
}
}
}
}
|
从git获取代码
jenkins内置了 git函数可以在pipeline里直接调用
1
2
3
4
5
6
|
steps{
echo 'start'
git branch: '<所在分支名称>',
credentialsId: '<系统配置的密钥名称>',
url: '项目地址'
}
|
逻辑控制
使用场景: 不同的分支做不同的操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
pipeline {
agent none
stages {
stage('Example Build') {
steps {
echo 'Hello World'
}
}
stage('Example Deploy') {
agent {
label "some-label"
}
when {
beforeAgent true //设置先对条件进行判断,符合预期才进入steps
branch 'production'
}
steps {
echo 'Deploying'
}
}
}
}
|
在jenkins页面上的配置
参考资料
官方文档