Jenkins 是一款著名的开源持续集成和持续交付自动化工具。它拥有出色的支持社区,以及数百个插件,开发人员已经使用它多年。
本文将提供一个简短的指南,介绍如何开始使用 Pipeline 和多分支 Pipeline。
为什么选择 Pipeline?
- 开发人员可以使用一个工具多次自动化代码的集成、测试和部署,从源代码到产品消费者。
- Pipeline “即代码”,也称为 Jenkinsfile,可以保存在任何源代码控制系统中。在之前的 Jenkins 版本中,作业只能使用 UI 进行配置。借助 Jenkinfile,Pipeline 更易于维护和移植。
- 多分支 Pipeline 与 Git 集成,因此不同的分支、功能和发布可以拥有独立的 Pipeline,从而使每位开发人员都可以自定义其开发/部署流程。
- 团队中的非技术成员可以使用参数触发和自定义构建,分析测试报告,接收电子邮件警报,并通过 Pipeline 阶段视图更好地了解构建和部署过程(在最新版本中使用 Blue Ocean UI 进行了改进)。
- Jenkins 也可以使用 Docker 安装,并且 Pipeline 可以与 Docker 代理进行交互。
如果您之前没有使用过 Jenkins Pipeline,我建议在开始之前阅读文档,因为它包含对该技术的完整描述和介绍,以及使用它的好处。
要求
- Jenkins 2.89.2 (WAR) 和 Java 8 是本操作指南中使用的版本
- 使用的插件(安装方法:
Manage Jenkins → Manage Plugins →Available
)- Pipeline: 声明式
- Blue Ocean
- Cucumber 报告
Jenkins Pipeline 入门
这是我使用的 Jenkinsfile(您也可以访问 GitHub 上的代码)
pipeline {
agent any
stages {
stage('testing pipeline'){
steps{
echo 'test1'
sh 'mkdir from-jenkins'
sh 'touch from-jenkins/test.txt'
}
}
}
}
1. 点击 New Item(新建项目)。

opensource.com
2. 命名项目,选择 Pipeline(Pipeline),然后点击 OK(确定)。

opensource.com
3. 创建项目后,将显示配置页面。在 Definition(定义)部分,您必须决定是从源代码控制管理 (SCM) 获取 Jenkinsfile,还是在 Jenkins 中创建 Pipeline 脚本。建议将 Jenkinsfile 托管在 SCM 中,以便它具有可移植性和可维护性。

opensource.com
我选择的 SCM 是 Git,使用简单的用户和密码凭据(也可以使用 SSH)。默认情况下,Jenkins 将在该存储库中查找 Jenkinsfile,除非在 Script Path(脚本路径)目录中另有指定。
4. 保存 Jenkinsfile 后,返回作业页面并选择 Build Now(立即构建)。Jenkins 将触发作业。它的第一阶段是从 SCM 中拉取 Jenkinsfile。它会报告与上次运行相比的任何更改并执行它。

opensource.com
点击 Stage View(阶段视图)提供控制台信息

opensource.com
使用 Blue Ocean
Jenkins 的 Blue Ocean 为 Pipeline 提供了更好的 UI。它可以从作业的主页面访问(见上图)。

opensource.com
这个简单的 Pipeline 有一个阶段(除了默认阶段):Checkout SCM(检出 SCM),它分三个步骤拉取 Jenkinsfile。第一步回显一条消息,第二步在 Jenkins 工作区中创建一个名为 from-jenkins
的目录,第三步在该目录内放置一个名为 test.txt
的文件。Jenkins 工作区的路径是 $user/.jenkins/workspace
,位于执行作业的机器中。在本例中,作业在任何可用的节点中执行。如果没有其他节点连接,则它在安装 Jenkins 的机器中执行——查看 Manage Jenkins > Manage nodes(管理 Jenkins > 管理节点)以获取有关节点的信息。
创建 Pipeline 的另一种方法是使用 Blue Ocean 的插件。(以下屏幕截图显示的是同一个存储库。)
1. 点击 Open Blue Ocean(打开 Blue Ocean)。

Miguel Suarez 截屏
2. 点击 New Pipeline(新建 Pipeline)。

opensource.com
3. 选择 SCM(SCM) 并输入存储库 URL;将提供一个 SSH 密钥。必须将其添加到您的 Git SSH 密钥中(在 Settings →SSH and GPG keys
(设置 →SSH 和 GPG 密钥)中)。

opensource.com
4. Jenkins 将自动检测分支和 Jenkinsfile(如果存在)。它还将触发作业。

opensource.com
Pipeline 开发
以下 Jenkinsfile 从 GitHub 存储库触发 Cucumber 测试,创建并归档 JAR,发送电子邮件,并展示作业可以使用变量、并行阶段等执行的不同方式。本演示中使用的 Java 项目是从 cucumber/cucumber-jvm Fork 到 mluyo3414/cucumber-jvm 的。您还可以访问 GitHub 上的Jenkinsfile。由于 Jenkinsfile 不在存储库的顶层目录中,因此配置必须更改为另一个路径

opensource.com
pipeline {
// 1. runs in any agent, otherwise specify a slave node
agent any
parameters {
// 2.variables for the parametrized execution of the test: Text and options
choice(choices: 'yes\nno', description: 'Are you sure you want to execute this test?', name: 'run_test_only')
choice(choices: 'yes\nno', description: 'Archived war?', name: 'archive_war')
string(defaultValue: "your.email@gmail.com", description: 'email for notifications', name: 'notification_email')
}
//3. Environment variables
environment {
firstEnvVar= 'FIRST_VAR'
secondEnvVar= 'SECOND_VAR'
thirdEnvVar= 'THIRD_VAR'
}
//4. Stages
stages {
stage('Test'){
//conditional for parameter
when {
environment name: 'run_test_only', value: 'yes'
}
steps{
sh 'cd examples/java-calculator && mvn clean integration-test'
}
}
//5. demo parallel stage with script
stage ('Run demo parallel stages') {
steps {
parallel(
"Parallel stage #1":
{
//running a script instead of DSL. In this case to run an if/else
script{
if (env.run_test_only =='yes')
{
echo env.firstEnvVar
}
else
{
echo env.secondEnvVar
}
}
},
"Parallel stage #2":{
echo "${thirdEnvVar}"
}
)
}
}
}
//6. post actions for success or failure of job. Commented out in the following code: Example on how to add a node where a stage is specifically executed. Also, PublishHTML is also a good plugin to expose Cucumber reports but we are using a plugin using Json.
post {
success {
//node('node1'){
echo "Test succeeded"
script {
// configured from using gmail smtp Manage Jenkins-> Configure System -> Email Notification
// SMTP server: smtp.gmail.com
// Advanced: Gmail user and pass, use SSL and SMTP Port 465
// Capitalized variables are Jenkins variables – see https://wiki.jenkins.io/display/JENKINS/Building+a+software+project
mail(bcc: '',
body: "Run ${JOB_NAME}-#${BUILD_NUMBER} succeeded. To get more details, visit the build results page: ${BUILD_URL}.",
cc: '',
from: 'jenkins-admin@gmail.com',
replyTo: '',
subject: "${JOB_NAME} ${BUILD_NUMBER} succeeded",
to: env.notification_email)
if (env.archive_war =='yes')
{
// ArchiveArtifact plugin
archiveArtifacts '**/java-calculator-*-SNAPSHOT.jar'
}
// Cucumber report plugin
cucumber fileIncludePattern: '**/java-calculator/target/cucumber-report.json', sortingMethod: 'ALPHABETICAL'
//publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: true, reportDir: '/home/reports', reportFiles: 'reports.html', reportName: 'Performance Test Report', reportTitles: ''])
}
//}
}
failure {
echo "Test failed"
mail(bcc: '',
body: "Run ${JOB_NAME}-#${BUILD_NUMBER} succeeded. To get more details, visit the build results page: ${BUILD_URL}.",
cc: '',
from: 'jenkins-admin@gmail.com',
replyTo: '',
subject: "${JOB_NAME} ${BUILD_NUMBER} failed",
to: env.notification_email)
cucumber fileIncludePattern: '**/java-calculator/target/cucumber-report.json', sortingMethod: 'ALPHABETICAL'
//publishHTML([allowMissing: true, alwaysLinkToLastBuild: false, keepAll: true, reportDir: '/home/tester/reports', reportFiles: 'reports.html', reportName: 'Performance Test Report', reportTitles: ''])
}
}
}
始终检查 Pipeline Syntax(Pipeline 语法)以了解如何在 Jenkinsfile 中使用不同的插件。

opensource.com
电子邮件通知指示构建成功

opensource.com
成功构建中归档的 JAR

opensource.com
您可以在同一页面上访问 Cucumber reports(Cucumber 报告)。

opensource.com

opensource.com
如何创建多分支 Pipeline
如果您的项目已经有 Jenkinsfile,请按照 Jenkins 文档中的 Multibranch Pipeline project(多分支 Pipeline 项目) 说明进行操作。它使用 Git,并假定凭据已配置。这是传统视图中的配置外观

opensource.com
如果这是您第一次创建 Pipeline,请按照以下步骤操作
1. 选择 Open Blue Ocean(打开 Blue Ocean)。

Miguel Suarez 截屏
2. 选择 New Pipeline(新建 Pipeline)。

opensource.com
3. 选择 Git(Git) 并插入 Git 存储库地址。此存储库当前没有 Jenkinsfile。将生成一个 SSH 密钥;它将在下一步中使用。

opensource.com
4. 转到 GitHub。点击右上角的个人资料头像,然后选择 Settings(设置)。然后从左侧菜单中选择 SSH and GPG Keys(SSH 和 GPG 密钥),并插入 Jenkins 提供的 SSH 密钥。

opensource.com
5. 返回 Jenkins 并点击 Create Pipeline(创建 Pipeline)。如果项目不包含 Jenkinsfile,Jenkins 将提示您创建一个新的。

opensource.com
6. 点击 Create Pipeline(创建 Pipeline)后,交互式 Pipeline 图表将提示您通过点击 + 添加阶段。您可以为每个阶段添加并行或顺序阶段以及多个步骤。列表提供了步骤的不同选项。

opensource.com
7. 以下图表显示了三个阶段(阶段 1、阶段 2a 和阶段 2b),其中简单的打印消息指示步骤。您还可以添加环境变量,并指定 Jenkinsfile 将在哪个代理中执行。

opensource.com
点击 Save(保存),然后点击 Save & Run(保存并运行)提交新的 Jenkinsfile。

opensource.com
您还可以添加新分支。

opensource.com
8. 作业将执行。

opensource.com

opensource.com
如果添加了新分支,您可以在 GitHub 中看到它。

opensource.com
9. 如果创建了另一个带有 Jenkinsfile 的分支,您可以通过点击 Scan Multibranch Pipeline Now(立即扫描多分支 Pipeline)来发现它。在本例中,从 Master 在 GitHub 中创建了一个名为 new-feature-2
的新分支(只有带有 Jenkinsfile 的分支才会显示在 Jenkins 中)。

opensource.com
扫描后,新分支将出现在 Jenkins 中。

opensource.com
此新功能是直接使用 GitHub 创建的;Jenkins 在执行扫描时将检测新分支。如果您不希望在发现新 Pipeline 时自动执行它们,请通过点击作业的 Multibranch Pipeline 主页面上的 Configure(配置)并添加属性 Suppress automatic SCM triggering(禁止自动 SCM 触发)来更改设置。这样,Jenkins 将发现新的 Pipeline,但必须手动触发它们。

opensource.com
本文最初发表在 Medium 上的 ITNext 频道上,并经许可转载。
评论已关闭。