如何使用Docker在GitLab上设置CI

by Ying Kit Yuen

英杰苑

如何使用Docker在GitLab上设置CI (How to setup CI on GitLab using Docker)

An example using Docker to test and build your pipeline

使用Docker测试和构建管道的示例

In the past you may have tried different tools to manage the deployment of your applications effectively. In this tutorial, I’ll be showing you a quick and easy way to set up continuous integration for your environment using GitLab and Docker. So lets get started.

过去,您可能尝试过各种工具来有效地管理应用程序的部署。 在本教程中,我将向您展示使用GitLab和Docker为您的环境设置持续集成的快速简便的方法。 因此,让我们开始吧。

Here is what we’ll be setting up:

这是我们将要设置的内容:

  • Version control

    版本控制
  • Issue Tracker

    问题追踪器
  • Documentation

    文献资料
  • Continuous Integration

    持续集成
  • Continuous Delivery

    持续交付
  • Repository (artifacts/docker images)

    存储库(工件/泊坞窗映像)

Tools like Jenkins are good for continuous integration and delivery. Mantis helps with issue tracking. But to improve the efficiency and quality of our project, we need to bring all these tools together. For example, we may want a git commit hook up with existing issues, or trigger an automated test after pushing commits to the master branch.

诸如Jenkins之类的工具非常适合进行持续集成和交付。 螳螂帮助跟踪问题。 但是,为了提高项目的效率和质量,我们需要将所有这些工具整合在一起。 例如,我们可能希望git commit与现有问题挂钩,或者在将提交推送到master分支后触发自动测试。

Usually, most tools already provide out of the box integration with other common services but it is still difficult to configure them at times. Moreover, the workflow would be broken if any one of the services in the chain go down. So it would be great if there were a single platform which could fulfil all these requires and that is why we’ve chosen GitLab.

通常,大多数工具已经提供了与其他常用服务的现成集成,但是有时仍然很难对其进行配置。 此外,如果链中的任何一项服务出现故障,工作流程都将中断。 因此,如果有一个可以满足所有这些需求的平台,那就太好了,这就是我们选择GitLab的原因

亚搏体育app CI (GitLab CI)

GitLab.com is a SAAS based service where you can host your Git repository, track issues and write the wiki in markdown. GitLab CI also allows you to setup continuous integration utilizing any Docker image available on Docker Hub. Let’s take a look at the following example.

GitLab.com是一项基于SAAS的服务,您可以在其中托管Git存储库,跟踪问题并以markdown编写Wiki。 GitLab CI还允许您使用任何Docker设置持续集成 该镜像可在Docker Hub上找到 。 让我们看下面的例子。

亚搏体育app CI YML (The GitLab CI YML)

GitLab CI uses a YAML file .gitlab-ci.yml to define the project configuration, which includes a definition of all the stages that need to be run after a CI/CD pipeline is triggered in response to a git push/merge. In this example, we have a simple Node.js project and we would like to make sure the code is good by linting and running a unit test. To play along, fork this repository and check it out.

GitLab CI使用YAML文件.gitlab-ci.yml来定义项目配置,其中包括对响应git push / merge触发CI / CD管道后需要运行的所有阶段的定义。 在此示例中,我们有一个简单的Node.js项目,我们希望通过添加和运行单元测试来确保代码良好。 要一起玩,请分叉该存储库并签出。

In the above YAML configuration file we defined 3 stages. Each stage is just a gulp task defined in gulpfile.js. Anyone could run the task locally as long as the have Node.js installed. But in GitLab CI, we only need to mention which Docker image is needed. In our case, that is node:6.11.2. Furthermore, this image attribute could be defined within the stage definition so that you could use different tool for each stage.

在上面的YAML配置文件中,我们定义了3个阶段。 每个阶段只是gulpfile.js定义的gulpfile.js任务。 只要安装了Node.js,任何人都可以在本地运行任务。 但是在GitLab CI中 ,我们只需要提及需要哪个Docker映像即可。 在我们的例子中,这是node:6.11.2。 此外,可以在阶段定义中定义此图像属性,以便您可以为每个阶段使用不同的工具。

阶段定义 (The stage definition)

Let’s take a deeper look in the stage definition.

让我们更深入地了解舞台定义。

The attributes of before_script and script can have multiple values (array in .yml). And if the script execution fails, the stage will be classified as failed.

before_scriptscript的属性可以具有多个值(.yml中的数组)。 如果脚本执行失败,则该阶段将被分类为失败。

触发管道 (Trigger the pipeline)

Just make some changes on the master branch and you can find the pipeline running on the CI / CD -> Pipeline page.

只需在master分支上进行一些更改,就可以在CI / CD -> Pipel ine页面上找到运行的管道。

详细查看舞台 (View the stage in detail)

Click on a specific pipeline and you can read the console output of each stage. This is useful when the stage/job fails.

单击特定管道,您可以阅读每个阶段的控制台输出。 当阶段/作业失败时,这很有用。

在Docker上使用GitLab CI的好处 (The benefits of using GitLab CI with Docker)

Different projects may require different dependencies such as Node.js, Ant, Maven. In the past when using a tool such as Jenkins, I’d have to make sure that all of these are installed on the server. By using Docker, the developer can reference dependencies available on Docker Hub without asking the server admin to setup such dependencies on the server each time. Actually Jenkins also has a pipeline plugin and it could work with Docker to serve exactly the same purpose. But extra effort on integrating Jenkins with version control is needed and as I mentioned before.

不同的项目可能需要不同的依赖项,例如Node.js,Ant,Maven。 过去使用诸如Jenkins之类的工具时,我必须确保所有这些工具都已安装在服务器上。 通过使用Docker ,开发人员可以引用Docker Hub上可用的依赖项,而无需要求服务器管理员每次在服务器上设置此类依赖项。 实际上,Jenkins也有一个管道插件,它可以与Docker一起使用以达到完全相同的目的。 但是,正如我之前提到的,将Jenkins与版本控制集成在一起需要付出额外的努力。

Although i prefer using GitLab CI, it doesn’t mean it could completely replace Jenkins. Jenkins offers a configurable user interface which is convenient for non-developers such as QA to execute certain tasks like deployments and integration tests.

尽管我更喜欢使用GitLab CI ,但这并不意味着它可以完全取代JenkinsJenkins提供了一个可配置的用户界面,对于质量保证等非开发人员来说,执行某些任务(如部署和集成测试)非常方便。

选择合适的工具-不一定是完美的 (Pick a suitable tool - It doesn’t have to be perfect)

The key is not about choosing the perfect tool. Instead it is more about the people who use it. So before searching for a new tool, try to identify the problem which you would like to solve first.

关键不是选择完美的工具。 相反,它更多地与使用它的人有关。 因此,在寻找新工具之前,请先确定您要解决的问题。

— Originally posted on Boatswain Blog.

—最初发布在Boatswain博客上

翻译自: https://www.freecodecamp.org/news/how-to-setup-ci-on-gitlab-using-docker-66e1e04dcdc2/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值