pipeline 是在 Jenkins服务端执行的,所以并不是客户端
pipeline有2种语法,声明式语法、脚本式脚本,这里学习的是声明式语法
Groovy 背景
Jenkins 流水线(Pipeline 是 Jenkins2.X 最核心的特性,帮助 Jenkins 实现从 CI 到 CD 与DevOps 的转变,可支持复杂流程的编排与可视化) 是一套插件
通过 Jenkinsfile 执行,支持脚本式(Script)和声明式(Declarative)两种,其中脚本式可通过 Groovy 语法编写执行过程,声明式需要用 script {} 包裹使用 Groovy 语句。
声明式
- pipeline :声明其内容为一个声明式的 pipeline 脚本
 - agent:执行节点(支持 any,none,label,docker,dockerfile)
 - stages:阶段集合,包裹所有的阶段(例如:打包,部署等各个阶段)
 - stage:阶段,被 stages 包裹,一个 stages 可以有多个 stage
 - steps:步骤,为每个阶段的最小执行单元,被 stage 包裹
 
pipeline {agent anystages {stage('Example') {steps {echo 'Hello World'}}}}
脚本式
def TEST_PROJECT_NAME=""def deployments = ["testing", "staging", "production"]def run(stepName) {retry(3){TEST_PROJECT_NAME = sh(script: "openssl rand -hex 4",returnStdout: true).trim()sh (script: "echo ${TEST_PROJECT_NAME}",label: "开始 ${stepName} 环境API测试")}}node {deployments.each{ param ->stage("$param") {run("$param")}}}
简单案例
第一个pipeline
创建项目,类型 pipeline

编辑流水线

pipeline {agent anystages {stage('Stage 1') {steps {echo 'Hello world!'sh "echo 'Hello world!' "sh "hostname"sh "uptime"}}}}
- agent any:所有jenkins服务端操作,假如你有集群的话可以指定某台,any表示所有,这里是单机
 - echo 在控制台输出中写入简单字符串,这个并不是linux的echo命令,而是jenkins语法的echo
agent语法 
agent any # 所有jenkins服务端操作,any表示所有agent { label 'master' } #假如你有集群的话可以指定某台,在主机名为master的机器执行
构建
之前有构建过一次,所以有阶段视图

查看构建结果
点击阶段视图的Logs

可以看到构建结果

点击 #1 可以看到 整个构建流程日志

构建流程日志

回放功能,它允许用户实时修改脚本运行,但又不更新Jenkinsfile
第二个pipeline
pipeline是流水线工作
pipeline {agent anystages {stage('Build') {steps {echo 'Building..'}}stage('Test') {steps {echo 'Testing..'}}stage('Deploy') {steps {echo 'Deploying....'}}}}
点击构建 此时3个阶段会依次进行

在 pipline 块内,为全局变量,对所有阶段和步骤生效
在 stage 阶段内,为局部变量,只对阶段和步骤生效
parameters多版本发布
pipeline {agent anyparameters {choice(name:'branchs',choices:"LNMP-1\nLNMP-2\nLNMP-3\n",description:'all branch')choice(name:'deployProgam',choices:"mysql\nredis\n",description:"deploy")choice(name:'UpdateOrRollback',choices:"update\nrollback\n",description:'updateroback')string(name: 'account', defaultValue: '', description: 'chiose account')}stages {stage("input"){steps{sh "echo -e deployProgram=${params.deployProgam} >/home/jenkins/scrpit/updatemodule.cfg"sh "echo -e release=${params.branchs} >>/home/jenkins/scrpit/updatemodule.cfg"sh "echo -e choice=${params.UpdateOrRollback} >>/home/jenkins/scrpit/updatemodule.cfg"writeFile file: "updatemodule.cfg", text: "deployProgram=${params.deployProgam}\nrelease=${params.branchs}\nchoice=${params.UpdateOrRollback}\n"}}stage("publish"){steps{sh "account=${params.account}"sh label: '', script: 'rsync -aze ssh --progress /home/jenkins/scrpit/updatemodule.cfg root@server:/root/daily_sync/project_update/new_vpn_release/script'}}}}
此时构建页面会发生改变,没有改变则刷新

邮件发送
pipeline{agent anystages{stage('mvn test'){steps{echo "mvn test"}}}post{always{ //always表示不管怎么样都做下面的操作pmd canComputeNew: false, defaultEncoding: '', healthy: '', pattern: '', unHealthy: ''}failure{step([$class: 'Mailer',notifyEveryUnstableBuild: true,recipients: "738402018@qq.com",sendToIndividuals: true])echo "failed!Please check pipeline code!"}success{echo "hello!success!"}}}
输入型选项
pipeline{agent anytriggers{upstream(upstreamProjects: 'job1,job2', threshold: hudson.model.Result.SUCCESS)}stages{stage('pre deploy'){steps{script{BRANCHES = "dev"dataMap = input message: '准备发布到哪个环境', ok: '确定',parameters:[choice(choices: ['dev', 'sit', 'prod'], description: '部署环境', name: 'ENV'),choice(choices: "${BRANCHES}", description: '分支', name: 'TAG')],submitterParameter: 'APPROVER'sh "echo ${dataMap}"}}}stage("演示一下"){steps{echo "${dataMap['APPROVER']}"echo "${dataMap['ENV']}"echo "${dataMap['TAG']}"}}}}
写法2
pipeline {agent anystages {stage('Example') {input {message "准备发布到哪个环境?"ok "确定"submitter "alice,bob"parameters {choice(name:'branchs',choices:"226-beta\n226-release\nAPP_RELEASE\n",description:'all branch')choice(name:'deployProgam',choices:"stock_publisher_pvo\nredis_publisher\nnew_trademodule\nnew_trademodule_credit\nstrategy_credit\nstrategy_trade_pvo\nstrategy_grab_pvo\nbarbuild\nbarbuild_pvo\nstock_mon\ntest\n",description:"deploy")choice(name:'UpdateOrRollback',choices:"update\nrollback\n",description:'updateroback')}}steps {echo "Hello ${branchs} nice to meet you."}}}}
pipeline脚本
pipeline {agent {kubernetes {label 'jenkins-slave-java'}}parameters {gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'BRANCH', type: 'PT_BRANCH', description:'please switch branch'choice(name: 'BaseImage', choices: ['openjdk11.0.9'], description: 'base image tag')choice(name: 'RancherProject', choices: ['dev'],description: 'please switch environment')string(name: 'BuildParameter', defaultValue: 'none', description: 'default same as RancherProject')choice(name: 'SyncToRancher', choices: ['Y','N'],description: 'sync to rancher')string(name: 'DBPassFile', defaultValue:'none', description: 'when value is none,DBPassFile path is ...,you can input special value to cover it')}environment {PRO_NAME = "gateway"BuildParameter="${params.BuildParameter}"SyncToRancher = "${params.SyncToRancher}"NS = "${params.RancherProject}"RNAME = "476548611639.dkr.ecr.eu-south-1.amazonaws.com/gateway"S3NAME = "hope-jar-test"BRANCH = "${params.BRANCH}"DBPasswd='test-db'BaseImage="${params.BaseImage}"}stages {stage('Clean workspace') {steps {deleteDir()}}stage('Process parameters') {steps {script {if("${params.BuildParameter}"=="none") {BuildParameter="${RancherProject}"}else {BuildParameter="${params.BuildParameter}"}if("${params.DBPassFile}"=="none") {DBPassFile="${params.DBPassFile}"}else {DBPassFile="${params.DBPassFile}"}}}}stage('Pull SourceCode') {steps {echo "${BRANCH}"git branch: "${BRANCH}", credentialsId: 'd560c72c-c2f9-43e0-9cdf-784818071c40', url: 'https://git-codecommit.eu-south-1.amazonaws.com/v1/repos/MyPay_Payment_Gateway'}}stage('Build') {steps{sh '''echo "==============Start Build=========="cd mypay/pay_commonmvn -DskipTests=true clean installcd ../mypay-ordermvn -DskipTests=true clean installcd ../mypayadmin-loggingmvn -DskipTests=true clean installcd ../mypay-accountmvn -DskipTests=true clean installcd ../mypayadmin-generatormvn -DskipTests=true clean installcd ../pay-gatewaymvn -DskipTests=true clean installecho "==============End Build=========="'''}}stage('Collect artifact') {steps {script {if(!fileExists("${PRO_NAME}")) {sh "mkdir -p ${PRO_NAME}/{classes,lib,webapps}"}sh "cp -f /root/mavenlib/com/deepvision/pay/pay-gateway/1.0.0-SNAPSHOT/pay-gateway-1.0.0-SNAPSHOT.jar ${PRO_NAME}/classes/${PRO_NAME}.jar"}}}stage('Process Dockerfile') {steps {script {sh '''cp -f /root/bin/DockerfileTemp Dockerfilesed -i "s/BASEIMAGETAG/${BaseImage}/g" Dockerfilesed -i "s#deploymentdir#${PRO_NAME}#g" Dockerfile'''}}}stage('Build docker image and sync to rancher') {steps{sh '''Tag="${NS}_${BRANCH}_$(date '+%Y%m%d%H%M')"aws ecr get-login-password | docker login --username AWS --password-stdin 476548611639.dkr.ecr.eu-south-1.amazonaws.comdocker build -t ${RNAME}:${Tag} .docker push ${RNAME}:${Tag}docker rmi ${RNAME}:${Tag}echo "==============start sync ${RNAME}:${Tag} to rancher=========="if [ "${SyncToRancher}" == "Y" ]thenpython /root/bin/syncRancher.py test ${NS} ${PRO_NAME} ${Tag}fi'''}}}}
pipeline {agent {kubernetes {label 'jenkins-slave-java'}}parameters {gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'BRANCH', type: 'PT_BRANCH', description: '选择需要构建的分支'choice(name: 'HOSTNAME', choices: ['dev-static-gateway.deepxin.com'], description: '主机名')string(name: 'BuildCmd', defaultValue: 'npm install && npm run build', description:'编译命令')}environment {HOSTNAME="${params.HOSTNAME}"S3NAME = "deepxin-fe-all/dev"BuildCmd="${params.BuildCmd}"BRANCH="${params.BRANCH}"}stages {stage('Clean workspace') {steps {deleteDir()}}stage('Pull SourceCode') {steps {dir("${WORKSPACE}") {git branch: "${params.BRANCH}", credentialsId: 'd560c72c-c2f9-43e0-9cdf-784818071c40', url: 'https://git-codecommit.eu-south-1.amazonaws.com/v1/repos/mypay-backoffice'}}}stage('Build') {steps{dir("${WORKSPACE}") {sh "${BuildCmd}"}}}stage('Deploy') {steps {dir("${WORKSPACE}") {sh "aws s3 sync dist/ s3://${S3NAME}/${HOSTNAME}/"}}}}}
