实际项目开发-GitLab CI持续集成

1.概述

Gitlab是常用的开源git代码管理工具之一,随着发展也推出了ci/cd解决方案.顾名思义具体来说ci/cd主要完成以下两个工作:
注:从8.0版本开始,GitLab 持续集成(CI)完全集成到GitLab中,且默认所有的项目开启。


ci(持续构建):代码提交后触发自动化的单元测试,代码预编译,构建镜像,上传镜像等.
cd(持续发布):持续发布则指将构建好的程序发布到各种环境,如预发布环境,正式环境.


2.结构

gitlab ci/cd是由独立的runner程序完成,runner采用go语言编写,因此可以很好的进行跨平台,通常可以将runner部署到任何gitlab server之外的服务器,从而避免对gitlab server的影响.
这里写图片描述


3.gitlab ci/cd流程

gitlab通过在项目的根目录放置.gitlab-ci.yml文件来触发pipline,文件书写遵循yml语法,因此,概括来说gitlab ci/cd只需要两步:

  1. 写好.gitlab-ci.yml文件,并放置到项目根目录
  2. 配置好gitlab runner.完成后,提交代码时会自动根据gitlab-ci.yml的触发条件进行执行相应的stage.

从此刻开始,在每一次push到Git仓库的过程中,Runner会自动开启pipline,pipline将显示在项目的Pipline页面中。

.gitlab-ci.yml文件会告诉GitLab Runner 做什么。默认情况下,它运行一个pipeline,分为三个阶段:build,test,deploy。你并不需要用到所有的阶段,没有job的阶段会被忽略。

如果一切运行正常(没有非零的返回值),您将得到与commit关联的漂亮的绿色标记。这使得在查看代码之前,很容易就能看出是否有一个提交导致了测试失败。

大多数项目使用GitLab CI服务来运行测试套件,这样如果开发人员发现问题就会及时得到反馈。


4.具体在代码中怎么实现

1)创建一个简单的.gitlab-ci.yml

.gitlab-ci.yml是用来配置CI在我们的项目中做些什么工作。它位于项目的根目录。在任何的push操作,GitLab都会寻找.gitlab-ci.yml文件,并对此次commit开始jobs,jobs的内容来源于.gitlab-ci.yml文件。因为.gitlab-ci.yml是存在于我们的项目仓库中,并且受版本控制的,所以旧版本也可以执行成功,且使用CI可以让forks更容易,分支可也以拥有不同的pipelines和jobs,而且对于CI来说只会拥有单一的来源。你也可以在我们的博客中找到我们为什么使用.gitlab-ci.yml的原因。

这里写图片描述

stages:
  - sonar

sonar:
  stage: sonar
  script:
    - mvn -q -Dmaven.multiModuleProjectDirectory=$MAVEN_HOME clean org.jacoco:jacoco-maven-plugin:prepare-agent test checkstyle:checkstyle sonar:sonar -Dsonar.branch.name=$CI_COMMIT_REF_NAME -Dsonar.branch.target=master
  tags:
    - java
  except:
    - /^(deploy|release|tag){1}.*$/

2) 推送.gitlab-ci.yml到GitLab

这里写图片描述
这里写图片描述

3)到Pipelines页面查看

这里写图片描述

这里写图片描述

这里写图片描述

Running with gitlab-runner 10.7.2 (b5e03c94)
  on OpenShift Runner b7f20de2
Using Kubernetes namespace: gitlab
Using Kubernetes executor with image maven:3-jdk-8 ...
Waiting for pod gitlab/runner-b7f20de2-project-2833-concurrent-0bld9f to be running, status is Pending
Waiting for pod gitlab/runner-b7f20de2-project-2833-concurrent-0bld9f to be running, status is Pending
Waiting for pod gitlab/runner-b7f20de2-project-2833-concurrent-0bld9f to be running, status is Pending
Waiting for pod gitlab/runner-b7f20de2-project-2833-concurrent-0bld9f to be running, status is Pending
Waiting for pod gitlab/runner-b7f20de2-project-2833-concurrent-0bld9f to be running, status is Pending
Running on runner-b7f20de2-project-2833-concurrent-0bld9f via gitlab-runner-36-kcv55...
Cloning repository...
Cloning into '/lanxinghua/demo'...
Checking out 3d74f136 as master...
Skipping Git submodules setup
$ mvn -q -Dmaven.multiModuleProjectDirectory=$MAVEN_HOME clean org.jacoco:jacoco-maven-plugin:prepare-agent test checkstyle:checkstyle sonar:sonar -Dsonar.branch.name=$CI_COMMIT_REF_NAME -Dsonar.branch.target=master

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running DataTest
Jul 12, 2018 7:17:14 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@68034211: startup date [Thu Jul 12 07:17:14 UTC 2018]; root of context hierarchy
Jul 12, 2018 7:17:14 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
1531379835430
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.493 sec
Running NewDataTest
Jul 12, 2018 7:17:15 AM org.springframework.test.context.support.DefaultTestContextBootstrapper getDefaultTestExecutionListenerClassNames
INFO: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
Jul 12, 2018 7:17:15 AM org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners
INFO: Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext]
Jul 12, 2018 7:17:15 AM org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners
INFO: Could not instantiate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/TransactionDefinition]
Jul 12, 2018 7:17:15 AM org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners
INFO: Could not instantiate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
Jul 12, 2018 7:17:15 AM org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
INFO: Using TestExecutionListeners: [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@16da1abc, org.springframework.test.context.support.DirtiesContextTestExecutionListener@671ea6ff]
Jul 12, 2018 7:17:15 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Jul 12, 2018 7:17:15 AM org.springframework.context.support.GenericApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericApplicationContext@75cf0de5: startup date [Thu Jul 12 07:17:15 UTC 2018]; root of context hierarchy
1531379836208
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.852 sec
Running OrderTest
b Test
a Test
c Test
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 sec

Results :

Tests run: 5, Failures: 0, Errors: 0, Skipped: 0

Job succeeded

这里写图片描述

ok,到这里就差不多了!!!

猜你喜欢

转载自blog.csdn.net/m0_37499059/article/details/81015858