DevOps GitLab CICD 实践3——CI文件编写

前置步骤:html

官方文档

编写结构相似Jenkins pineline流水线java

GitLab CI/CD Pipeline Configuration Referencelinux

GitLab CI/CD Pipeline Configuration Reference

GitLab CI/CD pipelines are configured using a YAML file called .gitlab-ci.yml within each project.git

The .gitlab-ci.yml file defines the structure and order of the pipelines and determines:github

  • What to execute using GitLab Runner.
  • What decisions to make when specific conditions are encountered. For example, when a process succeeds or fails.

This topic covers CI/CD pipeline configuration. For other CI/CD configuration information, see:spring

We have complete examples of configuring pipelines:docker

目标

  • maven项目自动打包
  • 自动测试
  • 自动镜像构建并上传

官方参数表列出了做业的可用参数:api

关键词 描述
script 由Runner执行的Shell脚本。
image 使用泊坞窗图像。也可用:image:nameimage:entrypoint
services 使用docker services图像。也可用:services:nameservices:aliasservices:entrypoint,和services:command
before_script 覆盖在做业以前执行的一组命令。
after_script 覆盖做业后执行的一组命令。
stages 定义管道中的阶段。
stage 定义做业阶段(默认值:) test
only 建立做业时限制。也可用:only:refsonly:kubernetesonly:variables,和only:changes
except 在未建立做业时限制。也可用:except:refsexcept:kubernetesexcept:variables,和except:changes
tags 用于选择Runner的标签列表。
allow_failure 让工做失败。失败的做业无助于提交状态。
when 何时开始工做。也可用:when:manualwhen:delayed
environment 做业部署到的环境的名称。也可用:environment:nameenvironment:urlenvironment:on_stop,和environment:action
cache 后续运行之间应缓存的文件列表。也可用:cache:pathscache:keycache:untracked,和cache:policy
artifacts 成功附加到做业的文件和目录列表。也可用:artifacts:pathsartifacts:nameartifacts:untrackedartifacts:whenartifacts:expire_inartifacts:reports,和artifacts:reports:junit。 在GitLab 企业版,这些都是可供选择:artifacts:reports:codequalityartifacts:reports:sastartifacts:reports:dependency_scanningartifacts:reports:container_scanningartifacts:reports:dastartifacts:reports:license_management,和artifacts:reports:performance
dependencies 做业所依赖的其余做业,以便您能够在它们之间传递工件。
coverage 给定做业的代码覆盖率设置。
retry 在发生故障的状况下,能够自动重试做业的次数和次数。
parallel 应该并行运行多少个做业实例。
trigger 定义下游管道触发器。
include 容许此做业包含外部YAML文件。也可用:include:localinclude:fileinclude:template,和include:remote
extends 此做业将继承的配置条目。
pages 上传做业结果以用于GitLab Pages。
variables 在做业级别定义做业变量。

注: 参数typestype弃用缓存

参考

官方构建案例app

Deploy a Spring Boot application to Cloud Foundry with GitLab CI/CD

参考其脚本

Configure GitLab CI/CD to deploy your application

Now we need to add the GitLab CI/CD configuration file (.gitlab-ci.yml) to our project’s root. This is how GitLab figures out what commands need to be run whenever code is pushed to our repository. We will add the following .gitlab-ci.yml file to the root directory of the repository, GitLab will detect it automatically and run the steps defined once we push our code:

image: java:8

stages:
 - build
 - deploy

build:
 stage: build
 script: ./mvnw package
 artifacts:
 paths:
 - target/demo-0.0.1-SNAPSHOT.jar

production:
 stage: deploy
 script:
 - curl --location "https://cli.run.pivotal.io/stable?release=linux64-binary&source=github" | tar zx - ./cf login -u $CF_USERNAME -p $CF_PASSWORD -a api.run.pivotal.io - ./cf push  only:
 - master
复制代码

配置

全局变量

进入工程CI设置配置全局变量脚本,包含镜像仓库登录名称、密码、打包名称等

配置全局变量目的在于配置脚本中不该该包含密码等敏感信息

1554278638713.png

若但愿使用GitLab内置环境变量,可参考官方表格

GitLab CI/CD environment variables

CI脚本

结合可用参数和样例配置,根据已经存在的SpringBoot项目编写响应的CI脚本.gitlab-ci.yml

image: docker:stable
services:
 - docker:dind

variables:
 DOCKER_DRIVER: overlay
 SPRING_PROFILES_ACTIVE: gitlab-ci

stages:
 - build
 - package

maven-build:
 image: maven:3-jdk-8
 stage: build
 script: "mvn package -B"
 artifacts:
 paths:
 - target/*.jar

docker-build:
 stage: package
 script:
 - docker build -t $CONTAINER_IMAGE:latest .
 - docker login -u $DOCKER_HUB_USER -p $DOCKER_HUB_PASS
 - docker push $CONTAINER_IMAGE:latest
复制代码

该脚本对于知足脚本目标的工程均可复用,若须要测试,则增长相关的测试步骤便可

此处因为工程包含外部依赖关系,不在构建时测试

自动构建

默认的触发构建事件为commit

触发后会自动执行任务

1554278973797.png

1554279027476.png

Maven自动触发构建

1554279066238.png

自动镜像打包

1554279219445.png

1554279272711.png

自动上传(推送)镜像

1554279311054.png

执行结果

1554279508887.png

若执行失败则会发送邮件给开发人员

1554279616575.png

CICD全套流程目标实现!

总结

无论使用GitLab CICD或者是使用Jenkins+Ansible的方式,目标都在于践行DevOps,打通开发与运维流程,一旦配置好CICD流程,日后开发人员便再也不操心打包、构建等操做,能够专一开发

相关文章
相关标签/搜索