Jenkins学习笔记第八篇pipeline机制

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}"
            }
        }
    }
}
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员路同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值