This article was sponsored by CloudBees. Thank you for supporting the sponsors who make SitePoint possible!
本文由CloudBees赞助。 感谢您支持使SitePoint成为可能的赞助商!
Recently, I wrote a series of articles about Jenkins, an easy-to-use continuous integration (CI) technology for enterprise IT. In these articles, we took a quick look at how we can assure the quality of our PHP projects using the tool. In this article, we’ll dive into the Jenkins plugin “Workflow”, created and maintained by both CloudBees and members of the Jenkins community. This open source plugin builds upon the industry standard CI technology and expands it to a full continuous delivery (CD) platform with sophisticated pipeline management.
最近,我写了一系列有关Jenkins 的文章 ,Jenkins是一种用于企业IT的易于使用的持续集成(CI)技术。 在这些文章中,我们快速浏览了如何使用该工具确保PHP项目的质量。 在本文中,我们将深入介绍由CloudBees和Jenkins社区成员创建和维护的Jenkins插件“ Workflow”。 这个开源插件建立在行业标准CI技术的基础之上,并将其扩展到具有复杂管道管理功能的完整连续交付(CD)平台。
As enterprises scale up their use of Jenkins and bring more and more workflows into their systems, automating them can become a challenge. The Workflow plugin helps to build and automate complex workflows. By scripting, you can mix multiple actions like parameterized triggers, copied artifacts, promoted builds and conditional build steps. Through the Workflow plugin, it becomes relatively easy to write such a script.
随着企业扩大对Jenkins的使用并将越来越多的工作流程引入其系统中,使其自动化可能成为一个挑战。 工作流程插件可帮助构建和自动化复杂的工作流程。 通过脚本编写,您可以混合多个操作,例如参数化触发器,复制的工件,升级的构建和条件构建步骤。 通过Workflow插件,编写这样的脚本变得相对容易。
安装和创建工作流程 (Installing and creating a workflow)
Before you start, you might want to make sure you have basic knowledge of Jenkins by reading my previous articles. If you haven’t done so, I suggest keeping those articles open as a reference.
在开始之前,您可能需要阅读我以前的文章 ,以确保您具有Jenkins的基本知识。 如果您还没有这样做,建议您将这些文章保留为参考。
Let’s get started by installing the Workflow plugin to get a good view on this particular plugin. Within the plugin page, search for Workflow: Aggregator and install it. All dependencies will be installed automatically for you. When done, make sure you restart Jenkins.
让我们从安装Workflow插件开始,以对该特定插件有一个很好的了解。 在插件页面中,搜索Workflow:Aggregator并安装它。 所有依赖项将自动为您安装。 完成后,请确保重新启动Jenkins。
Now it’s time to create a new job by clicking new item in the left menu. Fill in the form and make sure you pick workflow as your type. When done, submit the form and head straight to the configuration page for this job.
现在是时候通过单击左侧菜单中的新项目来创建新作业了。 填写表格,并确保选择工作流作为您的类型。 完成后,提交表单并直接转到此作业的配置页面。
![workflow_1](https://i-blog.csdnimg.cn/blog_migrate/07e8abc89e4cb84533efb2c07b18c72d.webp?x-image-process=image/format,png)
The configuration looks very basic from the outside, but it’s very powerful. The most important part is the script field, in which you’ll script your workflow. Scripting is done in the groovy language. Let’s start with the old fashioned helloworld example, so we get the very basics of this mighty plugin. Add the following content to your script field and click save.
从外部看,该配置非常基本,但是功能非常强大。 最重要的部分是脚本字段,您可以在其中编写工作流程脚本。 脚本是用普通语言完成的。 让我们从老式的helloworld示例开始,因此我们获得了这个强大插件的基本知识。 将以下内容添加到脚本字段,然后单击“保存”。
echo 'Hello SitePoint!'
echo 'Hello SitePoint!'
If you start a build and you take a look at the console output, you’ll see the following content.
如果开始构建并查看控制台输出,则会看到以下内容。
Started by user Peter Nijssen
Running: Print Message
Hello SitePoint!
Running: End of Workflow
Finished: SUCCESS
Of course, it’s nice to see we can echo a message to the console, but we want to script our whole build process. Let’s say we want to build our project Jumph, from the previous series of articles.
当然,很高兴看到我们可以向控制台回显一条消息,但是我们希望编写整个构建过程的脚本。 假设我们要根据之前的系列文章来构建项目Jumph。
If you’re new to the groovy language like I am, you might wonder how to start. There’s a snippet generator for the plugin, which will help you start quite fast. Below the script field, you’ll see a snippet generator. Mark the checkbox in front of snippet generator and select build from job. Fill in Jumph and click generate groovy. You will get the following output, which you can insert in your script text area.
如果您像我一样不熟悉常规语言,您可能会想知道如何开始。 该插件有一个摘要生成器,可以帮助您快速启动。 在脚本字段下方,您会看到一个代码片段生成器。 选中代码段生成器前面的复选框,然后从作业中选择构建。 填写Jumph,然后单击Generate groovy。 您将获得以下输出,您可以将其插入脚本文本区域。
build 'Jumph'
![workflow_2](https://i-blog.csdnimg.cn/blog_migrate/bb6f3d93644f4acc8053015a154780b4.webp?x-image-process=image/format,png)
If you now run a build, you’ll notice Jumph is actually being kicked off. Nice! However, we could have started the build process of the project ourselves. This is a simple example of what is called a step in Workflow terminology. A step can also be just archiving the artifacts of your project, or sending an email when it’s done. Let’s take a deep dive into Workflow, to get a better understanding of it’s power.
如果现在运行构建,您会注意到Jumph实际上已经开始了。 真好! 但是,我们本来可以启动项目的构建过程。 这是工作流术语中一个简单步骤的示例。 步骤也可以只是归档项目的工件,或在完成后发送电子邮件。 让我们深入研究Workflow,以更好地了解它的功能。
Let’s say we want to build the project for deployment. We want to create a tar.gzfile which later can be used for deployment. With Workflow, we can script this process.
假设我们要构建要部署的项目。 我们要创建一个tar.gzfile,以后可以将其用于部署。 使用Workflow,我们可以编写脚本。
def src = 'https://github.com/peternijssen/Jumph.git'
stage 'Build'
node {
git url: src
sh 'export SYMFONY_ENV=prod && composer install --no-dev --optimize-autoloader'
sh 'bower install'
}
In the above script, we first define our URL for the Git repository. Within the current node, we pull in this Git repository and run composer install while we set the Symfony environment to production. Next we run Bower to install our front-end dependencies. If we were to run this script, our project would be prepared to be deployed.
在上面的脚本中,我们首先定义Git存储库的URL。 在当前节点内,我们将这个Git存储库放入并运行composer install,同时将Symfony环境设置为生产环境。 接下来,我们运行Bower来安装前端依赖项。 如果我们要运行此脚本,则将准备好部署我们的项目。
Perhaps you have a tool like Gulp or Grunt running along to prepare your front-end. You can easily expand this script to run multiple scripts to prepare your application for deployment.
也许您正在运行诸如Gulp或Grunt之类的工具来准备您的前端。 您可以轻松地将此脚本扩展为运行多个脚本,以准备部署应用程序。
The next step we want to do is pack the application in a tar.gz file and move it to our public root, so it can be downloaded.
我们要做的下一步是将应用程序打包在tar.gz文件中,并将其移至我们的公共根目录,以便可以下载它。
def timestamp = new Date().format(“yyyyMMddHHmmss”)
def fileName = ‘jumph-’ + timestamp + ‘.tar.gz’
stage 'Package', concurrency: 1
node {
sh 'tar -zcvf ' + fileName + ' app/ bin/ src/ vendor/ web/'
sh 'cp ' + fileName + ' /srv/builds/'
mail body: 'A new build is awaiting: ' + fileName, subject: 'New build available', to: 'example@sitepoint.com'
}
We’re also sending an email and making sure our file name includes a timestamp, so we’ll always be aware of when the file was actually created.
我们还将发送电子邮件,并确保我们的文件名包含时间戳,因此我们将始终知道实际创建文件的时间。
If we think we’re going to use this part more than once, we can wrap it in a ‘function’.
如果我们认为我们将多次使用此部分,则可以将其包装为“函数”。
node {
packageApp(fileName)
mail body: ‘A new build is awaiting: ’ + fileName, subject: ‘New build available’, to: ‘example@sitepoint.com’
}
/**
* Package the app in tar.gz file
*
* @param fileName name of the file
*/
def packageApp(fileName) {
sh ‘tar -zcvf ’ + fileName + ’ app/ bin/ src/ vendor/ web/’
sh ‘cp ’ + fileName + ’ /srv/builds/’
}
In the end, your complete script will look like this:
最后,您的完整脚本将如下所示:
def src = ‘<a href="https://github.com/peternijssen/Jumph.git">https://github.com/peternijssen/Jumph.git</a>’
def timestamp = new Date().format(“yyyyMMddHHmmss”)
def fileName = ‘jumph-’ + timestamp + ‘.tar.gz’
<p>stage ‘Build’
node {
git url: src
sh ‘export SYMFONY_ENV=prod && composer install –no-dev –optimize-autoloader’
sh ‘bower install’
}
stage ‘Package’, concurrency: 1
node {
packageApp(fileName)
mail body: ‘A new build is awaiting: ’ + fileName, subject: ‘New build available’, to: ‘example@sitepoint.com’
}
/* FUNCTIONS */
/**
* Package the app in tar.gz file
*
* @param fileName name of the file
*/
def packageApp(fileName) {
sh ‘tar -zcvf ’ + fileName + ’ app/ bin/ src/ vendor/ web/’
sh ‘cp ’ + fileName + ’ /srv/builds/’
}
We still have a pretty simple script compared with what we can actually do with Workflow. For example, we could also start by running some simple tests to make sure our build is in the correct shape before we actually package the app. Instead of packaging, we could also deploy the app straight to our staging environment. After checking our staging environment, we could also let Workflow ask you for permission to deploy it to the production environment.
与我们可以实际使用Workflow相比,我们还有一个非常简单的脚本。 例如,我们也可以从运行一些简单的测试开始,以确保在实际打包应用程序之前,我们的构建是正确的。 除了打包外,我们还可以将应用程序直接部署到暂存环境中。 在检查我们的暂存环境之后,我们还可以让Workflow询问您是否允许将其部署到生产环境。
If you’re interested in these kinds of flows, you can check out the Java examples which you can find here and here.
如果您对这些流程感兴趣,可以查看Java示例,您可以在此处和此处找到这些示例。
Perhaps you also noticed we divided the script into two stages. A stage named build and a stage named package. By setting the concurrency to one on the package stage, we make sure that only the most recent build, that passed all the previous stages, will be performed. Next to that, a stage will also divide the script into sections.
也许您还注意到我们将脚本分为两个阶段。 一个名为build的阶段和一个名为package的阶段。 通过在打包阶段将并发设置为一个,我们确保仅执行通过所有先前阶段的最新构建。 紧接着,一个阶段还将脚本分为几部分。
As your whole build process grows, you can imagine it can take a lot of time for it to be completed. If your server then experiences problems or gets shut down, your whole process could get disrupted. However, Workflow is suspendable. This means Jenkins will remember at which part of the script it stopped, and will continue executing as soon as it’s back up. Through Workflow, you can make sure your process keeps on running where it left just before the shutdown.
随着整个构建过程的发展,您可以想象完成它可能要花费很多时间。 如果服务器随后出现问题或被关闭,则整个过程可能会中断。 但是,工作流是可挂起的。 这意味着Jenkins将记住它在脚本的哪一部分停止,并在备份后立即继续执行。 通过工作流,您可以确保进程在关闭前仍在继续运行。
结论 (Conclusion)
As seen in the article, Workflow is a major step in the evolution of Jenkins which takes it from CI to full dev-to-production continuous delivery, which is the key enabling technology in DevOps transformations. The plugin allows you to write a simple script to create a continuous delivery pipeline from a simple job.
如本文所见,工作流是Jenkins演进的重要一步,它将其从CI转变为从开发到生产的连续交付,这是DevOps转换中的关键支持技术。 该插件允许您编写一个简单的脚本,以从一个简单的作业创建一个连续的交付管道。
If you’re interested in what Workflow can do, check out this tutorial to get a better idea. If you’re an author of a Jenkins plugin, you might be interested in getting it to work in combination with Workflow through DSL.
如果您对Workflow可以做什么感兴趣,请查看本教程以获得更好的主意。 如果您是Jenkins插件的作者,则可能有兴趣让它与通过DSL的Workflow结合使用。
If you need an even more advanced package then the Workflow plugin, you might want to take a look at the CloudBees Jenkins Enterprise solution on the CloudBees website.
如果您需要一个比Workflow插件还要高级的软件包,则可以在CloudBees网站上查看CloudBees Jenkins Enterprise解决方案。
How would Workflow improve your build process? Have you given it a go yet?
工作流程将如何改善您的构建过程? 你放手了吗?
翻译自: https://www.sitepoint.com/supercharge-continuous-delivery-jenkins-workflow-plugin/