1 什么是pipeline
管道,等同于流水线。
什么是流水线呢?想象一下,把大象放到冰箱的步骤是啥,第一打开冰箱,第二把大象放进冰箱,第三关闭冰箱,完美!pipeline就是这些步骤的脚本。
Jenkins是持续集成工具,简单来说是用来部署的,部署的流程有哪些呢?一般一个简单的流程:拉代码 > 前端编译 > 后端编译 > 发送到目标机器 > 启动服务。将Jenkins和pipeline结合起来,可以更好的实现项目的部署。
2 声明式pipeline语法
下面来简单介绍下pipeline语法,掌握这12个语法,那么对于流水线这块知识点你就hold住啦~
1、agent
//agent部分指定整个Pipeline或特定阶段将在Jenkins环境中执行的位置,具体取决于该agent 部分的放置位置。该部分必须在pipeline块内的顶层定义 ,但阶段级使用是可选的。agent 参数:any/none/label/node/docker/dockerfile//指定在master节点运行agent {node { label "master"}}
2、post
//构建后操作post {unstable {emailext body: '$DEFAULT_CONTENT', postsendScript: '$DEFAULT_POSTSEND_SCRIPT', presendScript: '$DEFAULT_PRESEND_SCRIPT', recipientProviders: [requestor()], replyTo: '$DEFAULT_REPLYTO', subject: '$DEFAULT_SUBJECT', to: '$DEFAULT_RECIPIENTS'}failure {emailext body: '$DEFAULT_CONTENT', postsendScript: '$DEFAULT_POSTSEND_SCRIPT', presendScript: '$DEFAULT_PRESEND_SCRIPT', recipientProviders: [requestor()], replyTo: '$DEFAULT_REPLYTO', subject: '$DEFAULT_SUBJECT', to: '$DEFAULT_RECIPIENTS' }}
3、steps
pipeline {agent anystages {stage('stages') {steps {echo '这是一个步骤,是stage里面的一个步骤'}}}}
4、environment
pipeline {agent anyenvironment {SONAR_SERVER = 'http://xx.xx.xx.xx:9000'JAVA_HOME = '/data/jdk'}stages {stage('Build') {steps {sh 'printenv'echo 'environment:设置环境变量'}}}}
5、options
options {timestamps() //输入时间ansiColor('xterm') //输入颜色timeout(time: 1, unit: 'HOURS') //构建超过1小时后自动终止}
6、parameters
parameters {string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')text(name: 'BIOGRAPHY', defaultValue: '', description: 'Enter some information about the person')booleanParam(name: 'TOGGLE', defaultValue: true, description: 'Toggle this value')choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'Enter a password')file(name: "FILE", description: "Choose a file to upload")}
7、triggers
pipeline {agent anytriggers {cron('H 4/* 0 0 1-5')}stages {stage('triggers') {steps {echo 'triggers:设置触发时间'}}}}
8、stage
stages {stage('Build') {steps {sh 'printenv'echo 'environment:设置环境变量'}}}
9、tools
//工具名称必须在Jenkins 管理Jenkins → 全局工具配置中预置tools{maven 'maven3' //maven3必须是已经在jenkins上配置的工具}
10、input
stages{steps {input message:"这是一个输入"}}
11、when
//当指定的Groovy表达式求值为true时执行when {expression {return params.DEBUG_BUILD}}
12、parallel
stage('parallel') {parallel {stage('Run1') {steps {echo '并行1'}}stage('Run2') {steps {echo '并行2'}}}}
3 一个pipeline的dmeo
这是一个拉取代码进行sonar扫描的demo,里面涵盖的pipeline的大部分语法。
//注:定义变量先注释构建一次拿到变量,再把注释取消重新构建,重新构建之后才能拿到git_urldef git_urlswitch(comp){case 'demo':switch(app){case 'demo1':case 'demo2':git_url = 'git@git.xxx.com:${git_group}/xx-${app}.git'break;default:git_url = 'git@git.xxx.com:${git_group}/${app}.git'}break;default:git_url = 'git@git.xxx.com:${git_group}/${app}.git'}pipeline {agent {node{label 'jenkins-node1'}}tools {//工具名称必须在Jenkins 管理Jenkins → 全局工具配置中预配置。jdk 'JDK_1.7.0_79'}environment {BUILD_USER_ID = "定时任务"}options {timestamps()ansiColor('xterm')buildDiscarder logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '30', numToKeepStr: '10') //保持构建的天数:30 保持构建的最大个数:10disableConcurrentBuilds() //不允许并发构建}triggers {cron('0 8 * * 1-5')}parameters {string(name: 'git_group', defaultValue: 'xxx', description: '')string(name: 'comp', defaultValue: 'xxx', description: '')string(name: 'app', defaultValue: 'xxx', description: '')gitParameter branch: '', branchFilter: '.*', defaultValue: 'origin/master', description: '', name: 'branch',quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH'}stages {stage("1、构建任务信息"){steps {echo "\033[44;37m 1、构建任务信息 \033[0m"script {wrap([$class: 'BuildUser']) {//BUILD_USER_ID:需在environment定义该变量,否则自动触发时会报错buildName "#${BUILD_NUMBER}-${BUILD_USER_ID}-${BRANCH}-${BUILD_TIMESTAMP}"/* 注:如果复制job配置,下面这段代码会报错HTTPD_LOCATION = sh(returnStdout: true, script: 'git show -s |grep -vE "commit|Date" |grep -v "^$"')buildDescription "${HTTPD_LOCATION}"*/}}}}stage('2、拉取代码') {steps {echo "\033[44;37m 2、拉取代码 \033[0m"checkout([$class: 'GitSCM', branches: [[name: '$branch']], doGenerateSubmoduleConfigurations: false, extensions: [],submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'xxxx-xxxx-xxxx-xxxx',url: "${git_url}"]]])}}stage('3、sonar扫描') {steps{echo "\033[44;37m 3、sonar扫描 \033[0m"withSonarQubeEnv('sonar') {script{switch (app) {case 'demo':sh 'printenv'echo "\033[44;37m 跳过sonar扫描\033[0m"break;default:echo "\033[44;37m sh 'mvn clean compile -U $SONAR_MAVEN_GOAL' \033[0m"sh 'mvn clean compile -U $SONAR_MAVEN_GOAL'}}}}}}post {unsuccessful {emailext body: '$DEFAULT_CONTENT', postsendScript: '$DEFAULT_POSTSEND_SCRIPT', presendScript: '$DEFAULT_PRESEND_SCRIPT', recipientProviders: [requestor()], replyTo: '$DEFAULT_REPLYTO', subject: '$DEFAULT_SUBJECT', to: '$DEFAULT_RECIPIENTS'}}}




