jenkins pipeline机制:
每一个check-in 都会触发pipeline的执行,每一个阶段的状态对组内成员都可见 每一个阶段都会给一个及时反馈,每一次最后的stable版本都可以完成一次发布
pipleline项目创建,首先安装pipeline相关插件
在创建的第一个pipeline中使用默认给定script:
node {
def mvnHome
stage('Preparation') { // for display purposes
// Get some code from a GitHub repository
git 'https://github.com/jglick/simple-maven-project-with-tests.git'
// Get the Maven tool.
// ** NOTE: This 'M3' Maven tool must be configured
// ** in the global configuration.
mvnHome = tool 'M3'
}
stage('Build') {
// Run the maven build
withEnv(["MVN_HOME=$mvnHome"]) {
if (isUnix()) {
sh '"$MVN_HOME/bin/mvn" -Dmaven.test.failure.ignore clean package'
} else {
bat(/"%MVN_HOME%\bin\mvn" -Dmaven.test.failure.ignore clean package/)
}
}
}
stage('Results') {
junit '**/target/surefire-reports/TEST-*.xml'
archiveArtifacts 'target/*.jar'
}
}
pipeline语法:
agent:定义pipeline执行节点
参数:
必须出现的指令
any 可以使任意agent上执行pipeline
none pipeline将不分配全局agent 每个stage分配自己的agent
label 指定运行节点的label
node 自定义运行节点配置
指定label
指定customWorkspace
docker 控制目标节点上的docker运行相关内容
pipeline{
agent{
nodee{
label 'master'
customWorkspace "pipelineWorspace"
}
}
}
stages:包含一个或多个stage的序列,pipeline的大部分工作在此执行
必须出现指令
无参数
每个pipeline只有一个stages
stage 包含在stages中,pipeline完成的所有实际工作都需要包含在stage中
triggers 定义了pipeline自动化触发方式
参数:
cron 接收一个crontab风格的字符串来定义pipeline触发的常规时间
pollScm 接受一个crontab风格的字符串来定义jenkins检查scm源更改的常规时间
,如果存在新的更改,则pipeline将重新触发
例子:
pipeline{
agent{
node{
label 'master'
customWorkspace 'myworkspace'
}
}
triggers{
cron('H/2 * * * *')
}
parameters{
string(name:'PERSON' ,defaultValue: 'jenkins',description:'输入字符串')
}
}
关于crontab时间格式参考:五段 分 时 日 月 周
H/30 * * * * * 每30分钟构建一次
H H/2 * * *每两个小时构建一次
0 8 * * * 每天八点构建一次
0 8,12,22 * * * 每天8,12,22点构建,一天构建3次
H/3 0-23 * * * 每三分钟构建一次,每天0-23:59执行任务
environment 定义pipeline或stage运行时的环境变量
参数
不是必须出现的命令
无参数
environment{
hlw='hello world'
}
stages{
stage('print environment_1'){
steps{
echo hlw
}
}
stage('print environment_2'){
steps{
sh 'echo ${hlw}'
}
}
}
pipeline{
agent{
node{
label 'master'
customWorkspace 'myworkspace'
}
}
environment{
hlw='hello world'
}
stages{
stage('print environment_1'){
steps{
echo hlw
sleep 5
}
}
stage('print environment_2'){
steps{
sh 'echo ${hlw}'
sleep 5
}
}
}
post{
success{
echo 'sync goodbay'
sleep 5
}
failure{
}
unstable{
}
}
}
options:定义pipeline的专有属性
参数:
不是必须的指令
buildDiscarder 保持构建的最大个数
disableConcurrentBuilds 不允许并行执行pipeline任务
timeout:pipeline超时时间
retry 失效后,重试整个pipeline生成的所有控制台输出时间
skipStageAfterUnstable:一旦构建状态进入了 unstable 状态就跳过此stage
pipeline{
agent{
node{
label 'master'
customWorkspace 'pipelineWorkspace'
}
}
option{
buildDiscarder(logRotator(numToKeepStr:'2'))
}
}
parameters定义pipeline的专有参数列表
参数:
不是必须出现的命令
支持数据类型 booleanParam choice credentials file text password
pipeline{
agent{
node{
label 'master'
customWorkspace 'pipelineWorkspace'
}
}
option{
buildDiscarder(logRotator(numToKeepStr:'2'))
}
parameters{
string(name:'PERSON',defaultValue:'',description:'')
}
stages{
stage('Test parameters'){
steps{
echo "Hello ${params.PERSON}"
}
}
}
}