Jenkins pipeLine:
Jenkins Pipeline(或简称为带有大写“P”的“Pipeline”)是一套插件,支持将持续交付管道实现和集成到 Jenkins 中。
一个持续交付(CD)管道是一直到你的用户和客户的过程正从版本控制软件的自动化表达。对您的软件(在源代码控制中提交)的每一次更改在发布之前都经历了一个复杂的过程。此过程涉及以可靠且可重复的方式构建软件,以及通过多个测试和部署阶段推进构建的软件(称为“构建”)。
Pipeline 提供了一组可扩展的工具,用于通过Pipeline 域特定语言 (DSL) 语法对从简单到复杂的交付管道“作为代码”进行建模 。
Jenkins 管道的定义被写入一个文本文件(称为 a Jenkinsfile),该文件 又可以提交到项目的源代码控制存储库。 这是“管道即代码”的基础;将 CD 管道视为要进行版本控制和审查的应用程序的一部分,就像任何其他代码一样。
创建一个Jenkinsfile并将其提交到源代码控制提供了许多直接的好处:
自动为所有分支和拉取请求创建流水线构建过程。
流水线上的代码审查/迭代(以及剩余的源代码)。
管道的审计跟踪。
管道的单一事实来源 ,可以由项目的多个成员查看和编辑。
虽然在 Web UI 中或使用 a 定义管道的语法 Jenkinsfile是相同的,但通常认为最佳实践是在 a 中定义管道Jenkinsfile并将其签入源代码管理。
声明式与脚本式流水线语法:
A Jenkinsfile可以使用两种类型的语法编写 - 声明式和脚本式。
声明式管道和脚本式管道的构造根本不同。声明式流水线是 Jenkins 流水线的一个更新的特性,它:提供比 Scripted Pipeline 语法更丰富的语法特性,以及旨在使编写和阅读流水线代码更容易。
从根本上说,Jenkins 是一个支持多种自动化模式的自动化引擎。Pipeline 在 Jenkins 上添加了一组强大的自动化工具,支持从简单的持续集成到全面的 CD 管道的用例。通过对一系列相关任务进行建模,用户可以利用 Pipeline 的许多特性:
代码:管道在代码中实现,通常检查到源代码控制中,使团队能够编辑、审查和迭代他们的交付管道。
耐用:管道可以在 Jenkins 控制器的计划内和计划外重启中存活下来。
可暂停:流水线可以选择停止并等待人工输入或批准,然后再继续流水线运行。
通用性:管道支持复杂的现实世界 CD 要求,包括分叉/加入、循环和并行执行工作的能力。
可扩展:Pipeline 插件支持对其 DSL [ 1 ] 的自定义扩展 以及与其他插件集成的多个选项。
下面的流程图是在 Jenkins Pipeline 中轻松建模的一个 CD 场景的示例:
管道是用户定义的 CD 管道模型。管道的代码定义了整个构建过程,通常包括构建应用程序、测试应用程序和交付应用程序的阶段。
A Pipeline is a user-defined model of a CD pipeline. A Pipeline’s code defines your entire build process, which typically includes stages for building an application, testing it and then delivering it.
Also, a pipeline block is a key part of Declarative Pipeline syntax(Declarative Pipeline Syntax).
Node
A node is a machine which is part of the Jenkins environment and is capable of executing a Pipeline.
Also, a node block is a key part of Scripted Pipeline syntax.
Stage
A stage block defines a conceptually distinct subset of tasks performed through the entire Pipeline (e.g. "Build", "Test" and "Deploy" stages), which is used by many plugins to visualize or present Jenkins Pipeline status/progress.
Step
A single task. Fundamentally, a step tells Jenkins what to do at a particular point in time (or "step" in the process). For example, to execute the shell command make use the sh step: sh 'make'. When a plugin extends the Pipeline DSL, that typically means the plugin has implemented a new step.
管道语法概述
以下流水线代码框架说明了声明式流水线语法和 脚本式流水线语法之间的根本区别。
Stage 和 Step(以上)都是声明式和脚本式流水线语法的常见元素。
在声明式流水线语法中,pipeline块定义了在整个流水线中完成的所有工作:
Jenkinsfile(声明式管道)
pipeline {
agent any
stages {
stage('Build') { 定义“构建”阶段。执行与“构建”阶段相关的一些步骤。
steps {
//
}
}
stage('Test') { 定义“测试”阶段。执行与“测试”阶段相关的一些步骤。
steps {
//
}
}
stage('Deploy') { 定义“部署”阶段。执行与“部署”阶段相关的一些步骤。
steps {
//
}
}
}
}
以下是Jenkinsfile使用声明式流水线语法的示例:
Jenkinsfile(声明式管道)
pipeline {
agent any
options {
skipStagesAfterUnstable()
}
stages {
stage('Build') {
steps {
sh 'make'
}
}
stage('Test'){
steps {
sh 'make check'
junit 'reports/**/*.xml'
}
}
stage('Deploy') {
steps {
sh 'make publish'
}
}
}
}
Jenkinsfile(脚本化流水线)
node {
stage('Build') {
sh 'make'
}
stage('Test') {
sh 'make check'
junit 'reports/**/*.xml'
}
if (currentBuild.currentResult == 'SUCCESS') {
stage('Deploy') {
sh 'make publish'
}
}
}
- pipeline 是声明式流水线特定的语法,它定义了一个“块”,其中包含用于执行整个流水线的所有内容和指令。
- agent 是声明式流水线特定的语法,它指示 Jenkins 为整个流水线分配一个执行器(在一个节点上)和工作区。
- stage是描述此 Pipeline 阶段的语法块。stage在流水线语法页面上阅读有关声明式流水线语法中的块的更多信息。如所提到的上述,stage块在脚本管道语法可选的。
- steps是声明性管道特定的语法,描述要在此stage. sh是执行给定 shell 命令的流水线步骤(由流水线:节点和进程插件提供)。
- junit是另一个用于聚合测试报告的Pipeline步骤(由 JUnit 插件提供)。
- node是特定于脚本化流水线的语法,它指示 Jenkins 在任何可用的代理/节点上执行此流水线(以及其中包含的任何阶段)。这实际上等效agent于声明式管道特定的语法。
创建 Jenkinsfile: