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. yii框架学习(MVC)

    路由:两种方式,第一种是默认方式访问,假设配置了虚拟主机,那么localhost/web/index.php?r=admin/index    访问的是controllers目录下的admin控制器里 ...

  2. CTS添加新测试用例步骤

    一.CTS添加新测试用例: 前言: google源代码中的cts测试用例集目录为:source_android4.2/cts/tests/tests/ (source_android4.2表示andr ...

  3. 0.JQuery安装

    jQuery 安装 网页中添加 jQuery 可以通过多种方法在网页中添加 jQuery. 您可以使用以下方法: 从 jquery.com 下载 jQuery 库 从 CDN 中载入 jQuery, ...

  4. beta week 2/2 Scrum立会报告+燃尽图 05

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/9957 一.小组情况 组长:贺敬文组员:彭思雨 王志文 位军营 徐丽君队名: ...

  5. POJ 3128 Leonardo's Notebook (置换)

    Leonardo's Notebook Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2324   Accepted: 98 ...

  6. 使用 Itext 生成PDF

    一.生成PDF,所需jar包(itext-2.0.8.jar,iTextAsian.jar) 在springboot中只需要引入依赖即可,依赖代码如下: <dependency> < ...

  7. 使用微软易升安装纯净版win10

    1.打开官方网址 https://www.microsoft.com/zh-cn/software-download/windows10 2.下载工具 3.根据你的需求,我这里是给另外以外机器安装,一 ...

  8. VSCODE开发VUE.JS前端插件

    VUE前端插件.转载自:https://www.cnblogs.com/karthuslorin/p/8577224.html vscode是微软开发的的一款代码编辑器,就如官网上说的一样,vscod ...

  9. linux添加用户所在群组

    etc目录下面有两个文件一个passwd一个grouppasswd里gid是主组,其他组是扩展组,扩展组在/etc/group里描述.useradd username如果不指定,默认创建一个与uid相 ...

  10. 小D课堂 - 零基础入门SpringBoot2.X到实战_汇总

    第1节零基础快速入门SpringBoot2.0 小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_1.SpringBoot2.x课程介绍和高手系 ...