Jenkins Pipeline(或简称为 "Pipeline")是一套插件,将持续交付的实现和实施集成到 Jenkins 中。
Jenkins Pipeline 提供了一套可扩展的工具,用于将“简单到复杂”的交付流程实现为“持续交付即代码”。Jenkins Pipeline 的定义通常被写入到一个文本文件(称为Jenkinsfile)中,该文件可以被放入项目的源代码控制库中。
Jenkinsfile 是 Jenkins 2.x 核心特性 Pipeline 的脚本,由Groovy语言实现。
jenkinsfile 能使用两种语法进行编写 - 声明式和脚本化。
声明式和脚本化的流水线从根本上是不同的。 声明式流水线的是 Jenkins 流水线更近的特性:

a.相比脚本化的流水线语法,它提供更丰富的语法特性,
    b.是为了使编写和读取流水线代码更容易而设计的。

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生成的所有控制台输出时间
}

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

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. hdu 5810 Balls and Boxes 二项分布

    Balls and Boxes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  2. AtCoder AGC019E Shuffle and Swap (DP、FFT、多项式求逆、多项式快速幂)

    题目链接 https://atcoder.jp/contests/agc019/tasks/agc019_e 题解 tourist的神仙E题啊做不来做不来--这题我好像想歪了啊= =-- 首先我们可以 ...

  3. 十七、mysql数据库备份

    使用java代码在window环境下实现定时执行Mysql备份与还原 //备份 public void doBackup() { Date currentTime = new Date(); Syst ...

  4. [题解] [AtCoder2134] Zigzag MST

    题面 题解 考虑kruscal的过程 对于三个点\(x, y, x + 1\), 我们可以将\((x, y, z), (y, x + 1, z + 1)\)看做\((x, y, z), (x, x + ...

  5. zookeeper系列(六)zookeeper的系统模型(数据树)

    作者:leesf    掌控之中,才会成功:掌控之外,注定失败. 出处:http://www.cnblogs.com/leesf456/p/6072597.html尊重作者原创,奇文共欣赏,大家共同学 ...

  6. shell编程-定时任务(备份数据库)

    计划任务定时备份,删除等操作: #crontab -e #注意 会区分用户 默认在root用户登录用的是root权限用户的计划任务, 如果想在postgres备份 应使用postgres用户权限, 设 ...

  7. Java内存缓存-通过Map定制简单缓存

    缓存 在程序中,缓存是一个高速数据存储层,其中存储了数据子集,且通常是短暂性存储,这样日后再次请求此数据时,速度要比访问数据的主存储位置快.通过缓存,可以高效地重用之前检索或计算的数据. 为什么要用缓 ...

  8. 一些有意思的git

    fs: https://github.com/psankar/simplefs https://github.com/gzc/isystem/blob/master/basic/Crash_Consi ...

  9. 链表反转 C++

    ListNode* reverse1(ListNode* pHead) { if(pHead == NULL) return NULL; ListNode * p1 = NULL; ListNode ...

  10. LC 966. Vowel Spellchecker

    Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word ...