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. jenkins发送jemter邮件附件格式配置

    原文:https://www.cnblogs.com/chenchen-tester/p/6930200.html build.xml <?xml version="1.0" ...

  2. Navicat for MySQL 使用

    库创建标准 表查看sql样式

  3. python 多线程实现循环打印 abc

    python 多线程实现循环打印 abc 好久没写过python了, 想自己实践一下把 非阻塞版 import threading import time def print_a(): global ...

  4. 实现同时将一批.bmp文件转换成.mat格式

    %% 功能:实现同时对一批.bmp文件的转换成.mat格式PicFormat = {'*.bmp','Bitmap image (*.bmp)';... '*.jpg','JPEG image (*. ...

  5. 关于brew没有搜索到php的解决方案

    在终端添加php的资源包 brew tap homebrew/homebrew-php 链接 https://github.com/Homebrew/homebrew-php

  6. 第六天-css基础(css定位)

    css定位   方位名称:  left  right  top  bottom   position:absolute 绝对定位(脱离标准流 div{ width:100px; height:100p ...

  7. redis 提供 6种数据淘汰策略

    淘汰策略的原因 在 redis 中,允许用户设置最大使用内存大小 server.maxmemory,在内存限定的情况下是很有用的.譬如,在一台 8G 机子上部署了 4 个 redis 服务点,每一个服 ...

  8. kindeditor 引用js架包问题

    最近在搞kindeditor(富文本编辑器),遇到了很多插件修改无效的问题,仔细研究了一下才发现,别有洞天. 下面来介绍一下引用的js架包.具体有什么用. <!-- kindeditor.js ...

  9. sklearn3_svc分类器预测

    python机器学习-乳腺癌细胞挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...

  10. GitHub-Microsoft:sql-server-samples

    ylbtech-GitHub-Microsoft:sql-server-samples 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1. https://gi ...