Jenkins Pipeline(或简称为 "Pipeline")是一套插件,将持续交付的实现和实施集成到 Jenkins 中。

Jenkins Pipeline 提供了一套可扩展的工具,用于将“简单到复杂”的交付流程实现为“持续交付即代码”。Jenkins Pipeline 的定义通常被写入到一个文本文件(称为Jenkinsfile)中,该文件可以被放入项目的源代码控制库中。
 
Jenkinsfile 能使用两种语法进行编写 - 声明式和脚本化。
  • 相比脚本化的流水线语法,他提供更丰富的语法特性。
  • 是为了使编写和读取流水线代码更容易而设计的。

Jenkinsfile (Declarative Pipeline)

pipeline {
agent any
stages {
stage('Build') {
steps {
//
}
}
stage('Test') {
steps {
//
}
}
stage('Deploy') {
steps {
//
}
}
}
}

Jenkinsfile (Scripted Pipeline)

node {
stage('Build') {
//
}
stage('Test') {
//
}
stage('Deploy') {
//
}
}

下面将以声明式脚本为例,介绍jenkinsfile:

设置运行的agent
pipeline {
agent {label 'jenkins-slave'} // 配置构建项目在标签为jenkins-slave的机器上运行
.....
}
使用多个agent
pipeline {
agent none
stages {
stage('Build') {
agent any
steps {
echo "build..."
}
}
stage('Test on Linux') {
agent {
label 'linux'
}
steps {
echo "test..."
}
}
.....
}

配置可选参数

  agent any
options{
disableConcurrentBuilds() //不允许同时执行流水线
skipDefaultCheckout() //默认跳过来自源代码控制的代码
timeout(time: 10, unit: 'MINUTES') //设置流水线运行的超时时间
timestamps() //预定义由Pipeline生成的所有控制台输出时间
}
.....

配置全局变量

  environment {
service="java"
}
.....

配置局部变量

stage('Deploy'){
steps {
withEnv(['service=java']){
echo '$service'
}}}
.....

配置可选参数

  parameters{
string(name: 'branch', defaultValue: 'dev', description: 'which branch do you want to build?')
choice(name: 'service',choices:"java\nnodejs",description: "服务名")
}
.....

配置机密文本、用户名和密码

    stage('Deploy'){
steps {
withCredentials([usernamePassword(credentialsId: 'aliyun_oss_upload', passwordVariable: 'aliyun_sceret', usernameVariable: 'aliyun_key')]) {
sh '~/ossutil config -e ${endpoint} -i ${aliyun_key} -k ${aliyun_sceret};~/ossutil cp -r -f dist "oss://${name}"'
}}}

注:需先在jenkins添加用户凭据

拉取代码

    stage('Checkout') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '3', url: 'ssh://git@giturl/javacode.git']]])
}
}

#在job中点击Pipline Syntax ,选择checkout out from version control ,选择git输入仓库地址,生成拉取代码配置

定义构建完成后执行动作

  post {
success {
echo '构建成功'
}
failure {
echo '构建失败'
}
unstable {
echo '该任务被标记为不稳定任务'
}
aborted {
echo '该任务被终止'
}
}

条件判断

    stage('Build'){
steps {
script {
if ("${gitrepo}" == "java") {
echo "java"
}
else if ("${gitrepo}" == "python"){
echo "python"
} else {
echo "nodejs"
}
}
}
}

#if 需定义在script{}内

获取命令返回值

    stage('Push'){
steps {
script{
def pid = sh returnStatus: true, script: " ps -ef|grep tomcat|awk '{print \$2}'"
echo '$pid'
}
}
}

jenkins pipline和jenkinsfile的更多相关文章

  1. jenkins pipline 和 jenkinsfile

    Jenkins Pipeline(或简称为 "Pipeline")是一套插件,将持续交付的实现和实施集成到 Jenkins 中.Jenkins Pipeline 提供了一套可扩展的 ...

  2. Jenkins高级用法 - Jenkinsfile 介绍及实战经验

    系列目录 1.Jenkins 安装 2.Jenkins 集群 3.Jenkins 持续集成 - ASP.NET Core 持续集成(Docker&自由风格&Jenkinsfile) 4 ...

  3. jenkins pipline 几个注意细节

    新建jenkins pipline 1)pipeline的脚本语法要正确,sonarqube的projectKey需要做相应的修改 2)先执行一次构建,会报错 3)进到jenkins workspac ...

  4. 持续集成工具之Jenkins pipline简单示例

    前文我们主要聊了下jenkins的插件安装.用户及权限管理.邮件发送.配置凭证到gitlab上拉取项目和创建普通job:回顾请参考https://www.cnblogs.com/qiuhom-1874 ...

  5. ubuntu 16.04 jenkins pipline的实现 最终docker启动服务

    准备工作:两台虚拟机A:192.168.1.60 B:192.168.1.61 C:一个存放代码的代码库(github)A:jenkins git docker openssh-server(ssh) ...

  6. 使用Jenkins+Pipline 持构建自动化部署之安卓源码打包、测试、邮件通知

    一.引言 Jenkins 2.x的精髓是Pipeline as Code,那为什么要用Pipeline呢?jenkins1.0也能实现自动化构建,但Pipeline能够将以前project中的配置信息 ...

  7. jenkins:实现Jenkinsfile与Json的转换

    实现Jenkinsfile与Json的转换 目录 实现Jenkinsfile与Json的转换 方法1:使用现有的jenkins插件 参考 方法2:解析原生的jenkinsfile文件 参考 最近在做个 ...

  8. jenkins pipline 发送邮件

    推荐一个好网站https://www.w3cschool.cn/jenkins/jenkins-e7bo28ol.html 获取git 用户信息// Get checkout output value ...

  9. Jenkins Pipline语法

    引用自:http://baijiahao.baidu.com/s?id=1582812185263227836&wfr=spider&for=pc 引用自:https://www.cn ...

随机推荐

  1. 【转】linux中fork()函数详解

    原文链接:http://blog.csdn.net/jason314/article/details/5640969#comments 总结:面宝P268 fork()的意思是进程从这里开始分叉,分成 ...

  2. mybatis解析和基本运行原理

    Mybatis的运行过程分为两大步: 第1步,读取配置文件缓存到Configuration对象,用于创建SqlSessionFactory: 第2步,SqlSession的执行过程.相对而言,SqlS ...

  3. C# 从Object对象中读取属性的值

    https://www.cnblogs.com/xbblogs/p/7739483.html

  4. zrender-部分小知识点集合

    1.存组件元素和取组件元素,会在数据更新时,将存起来的拿出来 在construct(){ this.saveData=[];//先声明一个空的数组 } //存的方法 setSave(ele,i,nam ...

  5. 在CentOS/Windows下配置Nginx(以及踩坑)

    在CentOS/Windows下配置Nginx(以及踩坑) 1. 序言 因为这类文章网上比较多,实际操作起来也大同小异,所以我并不会着重于详细配置方面,而是将我配置时踩的坑写出来. 2. CentOS ...

  6. 千万级别数据量mysql优化策略

    表结构优化 1.  使用独立表空间 独立表空间指的是innodb表的一种数据结构 独占表空间:  每一个表都将会生成以独立的文件方式来进行存储,每一个表都有一个.frm表描述文件,还有一个.ibd文件 ...

  7. zabbix服务端连接客户端报错Received empty response from Zabbix Agent at [192.168.10.105]. Assuming that agent dropped connection because of access permissions

    这是zabbix WEB报的问题:Received empty response from Zabbix Agent at [192.168.10.105]. Assuming that agent ...

  8. mysql 查询一个月的数据

    //今天 select * from 表名 where to_days(时间字段名) = to_days(now()); //昨天 SELECT * FROM 表名 WHERE TO_DAYS( NO ...

  9. 使用多块GPU进行训练 1.slim.arg_scope(对于同等类型使用相同操作) 2.tf.name_scope(定义名字的范围) 3.tf.get_variable_scope().reuse_variable(参数的复用) 4.tf.py_func(构造函数)

    1. slim.arg_scope(函数, 传参) # 对于同类的函数操作,都传入相同的参数 from tensorflow.contrib import slim as slim import te ...

  10. 线性回归和正则化(Regularization)

    python风控建模实战lendingClub(博主录制,包含大量回归建模脚本和和正则化解释,2K超清分辨率) https://study.163.com/course/courseMain.htm? ...