当前位置: 首页 > news >正文

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

前置步骤:

  • DevOps GitLab CICD 实践1——GitLab 部署
  • DevOps GitLab CICD 实践2——Runner 部署

官方文档

编写结构类似Jenkins pineline流水线

GitLab CI/CD Pipeline Configuration Reference

GitLab CI/CD Pipeline Configuration Reference

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

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

  • 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:

  • GitLab CI/CD Variables, for configuring the environment the pipelines run in.
  • GitLab Runner advanced configuration, for configuring GitLab Runner.

We have complete examples of configuring pipelines:

  • For a quick introduction to GitLab CI, follow our quick start guide.
  • For a collection of examples, see GitLab CI/CD Examples.
  • To see a large .gitlab-ci.yml file used in an enterprise, see the .gitlab-ci.yml file for gitlab-ce.

目标

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

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

关键词描述
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被弃用。

参考

官方构建案例

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设置配置全局变量脚本,包含镜像仓库登陆名称、密码、打包名称等

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

若希望使用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

触发后会自动执行任务

Maven自动触发构建

自动镜像打包

自动上传(推送)镜像

执行结果

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

CICD全套流程目标实现!

总结

不管使用GitLab CICD或者是使用Jenkins+Ansible的方式,目标都在于践行DevOps,打通开发与运维流程,一旦配置好CICD流程,往后开发人员便不再操心打包、构建等操作,可以专注开发

转载于:https://juejin.im/post/5ca8082df265da30813813bd

相关文章:

  • 各国脑计划概览
  • JavaWeb xss攻击
  • EditText(插入表情图片)
  • 局域网的优点
  • Mybatis传递多个参数
  • 帆软2017百城巡展启动在即,力掀数据化管理之风
  • SQL-25 获取员工其当前的薪水比其manager当前薪水还高的相关信息
  • iOS Framework 单元测试(一)-- XCTests
  • 会话技术
  • java基础-Integer类常用方法介绍
  • linux下的小命令
  • 如何快速成为数据分析师(个人角度)
  • 长三角G60科创走廊智能驾驶产业联盟揭牌成立,近80家企业助力智能驾驶行业发展 ...
  • 用js来实现那些数据结构09(集合01-集合的实现)
  • Jfianl框架定时器使用配置
  • AHK 中 = 和 == 等比较运算符的用法
  • codis proxy处理流程
  • Create React App 使用
  • css选择器
  • HTML-表单
  • IP路由与转发
  • Java比较器对数组,集合排序
  • Java方法详解
  • LeetCode算法系列_0891_子序列宽度之和
  • MYSQL 的 IF 函数
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • Python 基础起步 (十) 什么叫函数?
  • Redis中的lru算法实现
  • Ruby 2.x 源代码分析:扩展 概述
  • SSH 免密登录
  • vue脚手架vue-cli
  • 分享几个不错的工具
  • 基于遗传算法的优化问题求解
  • 简析gRPC client 连接管理
  • 提升用户体验的利器——使用Vue-Occupy实现占位效果
  • 通信类
  • 温故知新之javascript面向对象
  • ​configparser --- 配置文件解析器​
  • ​linux启动进程的方式
  • #我与Java虚拟机的故事#连载08:书读百遍其义自见
  • #我与Java虚拟机的故事#连载09:面试大厂逃不过的JVM
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (二)【Jmeter】专栏实战项目靶场drupal部署
  • (淘宝无限适配)手机端rem布局详解(转载非原创)
  • (原创)攻击方式学习之(4) - 拒绝服务(DOS/DDOS/DRDOS)
  • (转)【Hibernate总结系列】使用举例
  • .NET/C# 项目如何优雅地设置条件编译符号?
  • .net6使用Sejil可视化日志
  • .NET教程 - 字符串 编码 正则表达式(String Encoding Regular Express)
  • @cacheable 是否缓存成功_让我们来学习学习SpringCache分布式缓存,为什么用?
  • [Angular] 笔记 6:ngStyle
  • [C# 网络编程系列]专题六:UDP编程
  • [C#]使用PaddleInference图片旋转四种角度检测
  • [Kubernetes]9. K8s ingress讲解借助ingress配置http,https访问k8s集群应用
  • [LeeCode]—Wildcard Matching 通配符匹配问题