Pipeline相关

Pipeline五大特性

代码:Pipeline以代码的形式实现,通常被检入源代码控制,使团队能够编辑、审查和迭代其CD流程。
可持续性:Jenklins重启或者中断后都不会影响Pipeline Job。
停顿:Pipeline可以选择停止并等待任工输入或批准,然后再继续Pipeline运行。
多功能:Pipeline支持现实世界的复杂CD要求,包括fork/join子进程,循环和并行执行工作的能力
可扩展:Pipeline插件支持其DSL的自定义扩展以及与其他插件集成的多个选项。

 

agent:该部分指定整个Pipeline或特定阶段将在Jenkins环境中执行的位置,具体取决于该agent 部分的放置位置。该部分必须在pipeline块内的顶层定义 ,也可以使用在stage级。
stage:表示这个Pipeline的某一个执行阶段(一个Pipeline可以划分成若干个Stage,每个Stage代表一组操作,例如:“Build”,“Test”,“Deploy”。Stage是一个逻辑分组的概念,可以跨多个Node)
steps: 包含一个或者多个在stage块中执行的step序列(Step是最基本的操作单元,小到创建一个目录,大到构建一个Docker镜像,由各类Jenklins Plugin提供)
environment:指定键值对,可用于step中,主要是为常量或者变量赋值,根据所在的位置来决定其作用范围(类似于java中全局和局部的概念)
options:允许执行pipeline内置的专用选项,也可以使用由插件提供的
parameters:提供触发pipeline时的参数列表
trigger:定义了触发pipeline的方式(jenkins1.x中的pollscm定时构建)
tools:自动安装工具,注意这里使用的一定是在jenkins全局配置中已经定义好了的
when:可以用来执行一些代码逻辑
post:可以根据pipeline的状态来执行一些操作

 

 

 

拉取docker镜像

agent{
docker{
image 'mydocker' //指定docker的镜像名称
label 'slave1' //指定在哪个机器上执行docker镜像
args '-v /tmp:/tmp' //运行时传入docker run的参数
}
}


拉取代码并做pmd检测(可以根据需要换成sonar),如果此次构建成功的话输出“hello!success!”;如果失败的话输出“failed!Please check pipeline code!”并发送邮件到指定的地址上。

pipeline{
agent any
tools{
maven 'maven3' //maven3必须是已经在jenkins上配置的工具
}
stages{
stage('checkout code'){
steps{
git credentialsId: '2c7a38c6-f536-4e93-bf3c-2ff4563fae8e', url: 'https://github.com/XXX/pipeline_script_test.git'
}
}
stage('mvn test'){
steps{
sh "mvn -B -f ${env.workspace}/pom.xml pmd:pmd"
}
}
}
post{
always{ //always表示不管怎么样都做下面的操作
pmd canComputeNew: false, defaultEncoding: '', healthy: '', pattern: '', unHealthy: ''
}
failure{
step([
$class: 'Mailer',
notifyEveryUnstableBuild: true,
recipients: "@doordu.com",
sendToIndividuals: true
])
echo "failed!Please check pipeline code!"
}
success{
echo "hello!success!"}
}
}

判断和异常处理
流程控制if/else条件
node {
stage('Example'){
if(env.BRANCH_NAME == 'master'){
echo 'I only execute on the master branch'
}else {
echo 'Iexecute elsewhere'
}
}
}

循环
for循环仅存在域脚本式pipeline中,但是可以通过在声明式pipeline中调用script step来执行
pipeline {
agent any
stages {
stage('Example'){
steps{
echo 'Hello world!'
script {
def browsers = ['chrome','firefox']
for (int i = 0;i < browers.size();++i){
echo "Testing the ${browsers[i]} browser"
}
}
}
}
}
}

自定义变量(局部)
def username = 'Jenkins'
echo "Hello Mr.${username}"
#注意一定要用双引号,单引号识别为字符串

环境变量(局部)
withEnv(['MYTOOL_HOME=/usr/local/mytool']){
sh '$MYTOOL_HOME/bin/start'
}

环境变量(全局)
environment {CC='clang'}
echo "Compiler is ${env.CC}"

参数化构建(全局)
parameters {string(name:'Jenkins',defaultValue:'Hello',description:'How should I greet the world')}
ehco "${params.Greeting} World!"


判断
when仅用于stage内部
when的内置条件为:
#当有环境变量 name 为 DEPLOY_TO 值是 production 条件成立
- when {branch 'master'}
- when {environment name:'DEPLOY_TO',value:'production'}

#表达式返回值为真时
- when {expression {return params.DEBUG_BUILD}}

#allOf 所有条件都满足时
- when {not {branch 'master'}}
- when {allOf {branch 'master'; environment name:'DEBUG_TO',value:'production'}}

#anyOf有一个条件满足时即可
- when {anyOf {branch 'master' ; branch 'staging'}}

posted @ 2021-01-15 12:00  Buster_Hsueh  阅读(108)  评论(0编辑  收藏  举报