pipeline学习
目录
- 一、常用语法
- 二、基础使用
- 三、使用 Groovy 沙盒
- 四、参数化构建过程
- 五、pipeline script from SCM
- 六、参考
一、常用语法
1、拉取git仓库代码
checkout([
$class: 'GitSCM',
branches: [[name: 代码分支名称]],
doGenerateSubmoduleConfigurations: false,
userRemoteConfigs: [[credentialsId: 权限, url: 代码地址]]
])
2、cleanWs()或者deleteDir(), 删除工作目录
// 删除${WORKSPACE}目录
cleanWs()// 判断目录是否存在
dir("${env.WORKSPACE}@tmp") {
//删除${WORKSPACE}@tmp目录
deleteDir()
}
3、withCredentials ,获取用户名和密码
steps{
withCredentials([usernamePassword(credentialsId: 'user_for_openshift', passwordVariable: 'password', usernameVariable: 'username')]) {
sh 'docker login -u $username -p $password registory.ctiwifi.cn:5000
}
}
4、readJSON读取json文件
def readJsonFile(filePath) {
def propMap = readJSON file: filePath
return propMap
}
二、基础使用
![pipeline学习](https://img.it610.com/image/info8/a42c55b3468a48babb30389e3d24c202.jpg)
文章图片
![pipeline学习](https://img.it610.com/image/info8/b051f506f1374b25a120ba3268860eab.jpg)
文章图片
![pipeline学习](https://img.it610.com/image/info8/31780e3056854caca1f19cb89f70b21b.jpg)
文章图片
![pipeline学习](https://img.it610.com/image/info8/9295c170e75e4e0c8c07f34e4b1d69c0.jpg)
文章图片
- pipeline代码
pipeline {
agent any
stages {
stage('初始化') {
steps {
echo '初始化。。。'
}
}
stage('打包') {
steps {
echo '打包。。。'
}
}
stage('测试') {
steps {
echo '测试。。。'
}
}
stage('发布') {
steps {
echo '发布。。。'
}
}
}
}
三、使用 Groovy 沙盒
![pipeline学习](https://img.it610.com/image/info8/9a2afa1079ae4ca5a91cd2f243a2e221.png)
文章图片
![pipeline学习](https://img.it610.com/image/info8/2afdaf1b7700485e94b7caaa29e5b783.jpg)
文章图片
![pipeline学习](https://img.it610.com/image/info8/c45df0eb3a3c4ee7868f3b88efa6ff29.jpg)
文章图片
![pipeline学习](https://img.it610.com/image/info8/e2b9c27dfd91404d8cbc75f56ed56138.jpg)
文章图片
![pipeline学习](https://img.it610.com/image/info8/8118ec8be9c149be8d1095617d6dd89c.jpg)
文章图片
![pipeline学习](https://img.it610.com/image/info8/8c46015926a241a7ab54304d5c73f75b.png)
文章图片
- pipeline代码
pipeline {
agent any
stages {
stage('初始化') {
steps {
echo '初始化。。。'
}
}
stage('打包') {
steps {
echo 'git代码拉取。。。'
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'a8db6793-cc2b-4d82-bd3d-c5beb1c5149e', url: 'http://192.168.3.11/root/rapid-screen.git']]])echo 'mvn打包。。。'
sh "/usr/local/apache-maven-3.8.2/bin/mvn -U clean install"
}
}
stage('测试') {
steps {
echo '测试。。。'
}
}
stage('发布') {
steps {
echo '发布。。。'
}
}
}
}
四、参数化构建过程
![pipeline学习](https://img.it610.com/image/info8/90a697314b4f49e1bacc55b2f8399033.jpg)
文章图片
![pipeline学习](https://img.it610.com/image/info8/f89294d5aa1e448cb0f299868b0f493d.jpg)
文章图片
- pipeline代码
pipeline {
agent any
parameters {
booleanParam(name: 'ENABLE_BACKEND_BUILD', defaultValue: true, description: '开启后端构建')
booleanParam(name: 'ENABLE_DEPLOY', defaultValue: true, description: '开启部署')
}
stages {
stage('初始化') {
steps {
echo '初始化。。。'
}
}
stage('打包') {
when{
expression{params.ENABLE_BACKEND_BUILD}
}
steps {
echo '打包。。。'
ws("backend_build"){
dir("my_docker"){
echo 'git代码拉取。。。'
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'a8db6793-cc2b-4d82-bd3d-c5beb1c5149e', url: 'http://192.168.3.11/root/rapid-screen.git']]])echo 'mvn打包。。。'
sh "/usr/local/apache-maven-3.8.2/bin/mvn -U clean install"
}
echo '打包完成。。。'
// 删除backend_build工作目录
cleanWs()
}
}
}
stage('测试') {
steps {
echo '测试。。。'
}
}
stage('发布') {
when{
expression{params.ENABLE_DEPLOY}
}
steps {
echo '发布。。。'
}
}
}
}
五、pipeline script from SCM
1、新建一个docker-build项目并配置Jenkinsfile,如下
![pipeline学习](https://img.it610.com/image/info8/fd5a150252584f6492d745d4e9670f76.jpg)
文章图片
【pipeline学习】2、新建pipeline项目,选择pipeline script from SCM,git设置为上面新建的docker-build项目
![pipeline学习](https://img.it610.com/image/info8/abeffd1dc07641c59baa458eeae99935.jpg)
文章图片
![pipeline学习](https://img.it610.com/image/info8/27c61ed630504f2397d0c1b408ffe3fa.jpg)
文章图片
六、参考
- https://blog.csdn.net/diantun00/article/details/81075007
- pipeline常用插件: https://blog.csdn.net/liurizhou/article/details/86669572
推荐阅读
- 由浅入深理解AOP
- 继续努力,自主学习家庭Day135(20181015)
- python学习之|python学习之 实现QQ自动发送消息
- 一起来学习C语言的字符串转换函数
- opencv|opencv C++模板匹配的简单实现
- 定制一套英文学习方案
- 漫画初学者如何学习漫画背景的透视画法(这篇教程请收藏好了!)
- 《深度倾听》第5天──「RIA学习力」便签输出第16期
- 如何更好的去学习
- 【韩语学习】(韩语随堂笔记整理)