Most of us are acquainted with developing an app or a website. But very few know what happens in the software industry after code is written before getting deployed to the server. Guess you are already curious 😉?
我们大多数人都熟悉开发应用程序或网站。 但是很少有人知道在编写代码并将其部署到服务器之前在软件行业中会发生什么。 猜猜你已经好奇了吗?
Once a code is written it undergoes continuous changes due to bug fixes or new feature addition. All such changes need to be thoroughly tested so that
编写代码后,由于错误修复或添加了新功能,代码会不断变化 。 所有这些更改都需要进行彻底的测试,以便
- New changes work perfectly fine. 新的更改工作正常。
- Existing behavior of code is not altered. 代码的现有行为不会改变。
Here is where a continuous integration and continuous deployment pipeline, popularly called a CI/CD pipeline comes into picture.
这是一个持续集成和持续部署管道(通常称为CI / CD管道)出现的地方。
Continuous integration takes care that new code changes are integrated into existing code without affecting current code behavior.
持续集成需要注意将新代码更改集成到现有代码中,而不影响当前代码行为。
Continuous deployment ensures that these new changes are deployed once they have been tested thoroughly.
持续部署可确保这些新更改在经过全面测试后即可部署。
- A CI/CD pipeline automates all the tasks that follow after a code change, thus saving manual time and effort and ensuring timely delivery of software changes. CI / CD管道可自动执行代码更改后执行的所有任务,从而节省了手动时间和精力,并确保及时交付软件更改。
The major stages involved when a code change is made in the software are -
在软件中进行代码更改时涉及的主要阶段是:
- Source Code/ Version Management (GitHub) 源代码/版本管理(GitHub)
- Building the project (Jenkins) and Containerizing(Docker 🐳) 构建项目(Jenkins)和容器化(Docker🐳)
- Running Unit Test cases (Jenkins ) 运行单元测试用例(詹金斯)
- Deploying (Jenkins / Rundeck) 部署(Jenkins / Rundeck)
- Application Monitoring (ELK stack) 应用程序监视(ELK堆栈)
Source Code/Version Management (Git)
源代码/版本管理(Git)
Version control allows you to keep track of your work and helps you to easily explore the changes you have made. We will use GitHub as a tool for our VCS. (it’s popular and easy)
版本控制使您可以跟踪自己的工作,并帮助您轻松浏览所做的更改。 我们将使用GitHub作为我们的VCS工具。 (它很流行而且很容易)
Reference Materials: Beginner’s Tutorial For Git . Also, check this to know how git works internally.(very important to know and useful too)
参考资料: Git入门教程 。 另外,请检查此内容以了解git在内部如何工作 (非常重要并且也非常有用)
Build executable and Containerize it(Jenkins)
构建可执行文件并将其容器化(詹金斯)
- Jenkins is an open-source tool that enables developers to create a pipeline and automate tasks such as creating application executable, running tests, deployment of code to remote servers. Hence it reduces effort in tasks that seem redundant but are extremely important in software development. Jenkins是一个开源工具,使开发人员能够创建管道并自动执行任务,例如创建应用程序可执行文件,运行测试,将代码部署到远程服务器。 因此,它减少了看似多余但在软件开发中极为重要的任务的工作量。
- Also Jenkins has a large collection of plugins that help it to integrate with tools required during different pipeline phases. Some of those are Docker, Maven, Amazon EC2, etc. Hence it is widely used in the software industry. 此外,Jenkins拥有大量插件,可帮助其与不同管道阶段所需的工具集成。 其中一些是Docker,Maven,Amazon EC2等。因此,它被广泛用于软件行业。
Advantages (for Dev and QA)
优势(对于开发和质量检查而言)
- Every commit made in the source code is built and unit tested. So, instead of checking the entire source code developers only need to focus on a particular commit. This leads to frequent new software releases. 源代码中的每个提交均已构建并进行了单元测试。 因此,开发人员无需检查整个源代码,而只需要专注于特定的提交即可。 这导致频繁发布新软件。
- You only need to commit changes to the source code and Jenkins will automate the rest of the process for you. 您只需要提交对源代码的更改,Jenkins将为您自动完成其余过程。
- Even the QA team only worries about writing test cases and not about setting up the build environment as it is managed by Jenkins itself. 甚至QA团队也只担心编写测试用例,而不担心设置构建环境,因为构建环境由Jenkins本身管理。
Creating the BUILD stage of pipeline in Jenkins:
在Jenkins中创建管道的BUILD阶段 :
We will see how to create a task in Jenkins that will pull code from Git on every commit, create an executable from it and then create a docker image of it.
我们将看到如何在Jenkins中创建一个任务,该任务将在每次提交时从Git中提取代码,从中创建可执行文件,然后为其创建docker映像。
- Create new item in Jenkins 在Jenkins中创建新项目
2. Name it as “BUILD”
2.将其命名为“ BUILD”
3. Select the freestyle project & click ok
3.选择自由式项目,然后单击确定。
4. Now go to “Configure”
4.现在转到“配置”
5. Add Github Repository link and enter credentials.
5.添加Github存储库链接并输入凭据。
6. In Build Triggers check GitHub hook Trigger for GITSCM Polling.
6.在“构建触发器”中,选中GitHub挂钩的GITSCM轮询触发器。
7. In build choose “Execute Shell” where you will build an executable of the code pulled from Github. Create a docker image for this executable and then create and run the docker container for the docker image.
7.在构建中,选择“ Execute Shell”,您将在其中构建从Github提取的代码的可执行文件 。 为此可执行文件创建一个docker映像,然后为该docker映像创建并运行docker容器。
Why Docker? 😕
为什么选择Docker? 😕
Docker containers provide us with a dedicated and isolated environment where our application can run. It is done so that the dependencies and build environment required by any program or executable do not conflict with the overall system environment. Learn more about docker:
Docker容器为我们提供了一个可以在其上运行应用程序的专用且隔离的环境。 这样做是为了确保任何程序或可执行文件所需的依赖关系和构建环境都不会与整个系统环境冲突。 了解有关Docker的更多信息:
Docker official documentation page.
Docker官方文档页面。
Dockers in Jenkins pipeline.
Jenkins 管道中的Docker。
Beginning with dockers
从码头工人开始
Running Test Cases:
运行测试用例 :
The application build created in the build phase is tested against the test cases you have written for this application. Test cases can be of different types such as unit tests(ran during build), integration tests, load testing for the executable etc. The build must run successfully against all test cases in order to proceed further.
在构建阶段创建的应用程序构建将针对您为此应用程序编写的测试用例进行测试。 测试用例可以具有不同的类型,例如单元测试 (在构建过程中运行), 集成测试 ,可执行文件的负载测试等。构建必须针对所有测试用例成功运行才能继续进行。
Steps to set up a test phase in pipeline:
在管道中设置测试阶段的步骤:
- Repeat steps 1–3 to create the build stage and name it as TEST.. 重复步骤1-3,以创建构建阶段并将其命名为TEST。
- Go to configuration and set the trigger to be successful on the BUILD stage. 进行配置,并将触发器设置为在BUILD阶段成功。
3. In the build option, select ‘Execute Shell’ and type in the commands that will run the test cases using the docker image created in the BUILD phase.
3.在构建选项中,选择“ Execute Shell”,然后键入将使用在BUILD阶段中创建的Docker映像运行测试用例的命令。
Additional reference
附加参考
Deploying the final build:
部署最终版本:
Once our code has successfully passed all the stages of testing, it is ready to be deployed on the application server(like AWS, GCP) for the end-user to use it. Deployment has to be done on a single system or multiple systems, depending on the requirement. Currently Jenkins supports both types of deployments. Steps to setup deployment phase in the pipeline:
一旦我们的代码成功通过了所有测试阶段,就可以将其部署到应用程序服务器(如AWS,GCP )上,供最终用户使用。 部署必须根据需要在单个系统或多个系统上完成。 目前,Jenkins支持两种类型的部署。 在管道中设置部署阶段的步骤:
- Repeat steps 1–3 used to create the BUILD stage and name it as DEPLOY. 重复用于创建BUILD阶段的步骤1-3,并将其命名为DEPLOY。
- Go to configuration and set the trigger to be successful on the TEST stage. 进行配置,并将触发器设置为在TEST阶段成功。
You need to deploy your changes to a remote server. Configure Jenkins for this purpose using this.
您需要将更改部署到远程服务器。 为此,请使用this配置Jenkins。
Application Monitoring:
应用监控:
Once the application is deployed we want to monitor certain aspects such as incoming application requests, application logs, etc that helps to analyze and optimize the application. To do this we can use the ELK stack (Elastic Search, Logstash, Kibana). Learn more about ELK.
部署应用程序后,我们希望监视某些方面,例如传入的应用程序请求,应用程序日志等,这些方面有助于分析和优化应用程序。 为此,我们可以使用ELK堆栈(Elastic Search,Logstash,Kibana)。 了解有关ELK的更多信息。
翻译自: https://medium.com/wind-of-change/creating-a-ci-cd-pipeline-6ff9aeb0848c