云计算_CI/CD_devOps_集成_Gitlab-runner

CICD自动化的工具和流程

持续集成
    (Continuous Integration ,CI)		
     持续集成是一个将集成提前至开发周期的早期阶段的实践方式,让构建、测试和集成代码更经常反复地发生。
	 传统软件开发过程中,集成通常发生在每个人都完成了各自的工作之后。在项目尾声阶段,通常集成还要花费数周或者数月的时间来完成。
持续交付 -- 在于可交付的产物
    持续交付添加了测试Test->模拟Staging->生产Production的流程
      测试环境
     类生产环境--灰度发布
持续部署 是在持续交付的基础上,把部署到生产环境的过程自动化。
      生产环境

案例

01.从mvn构建代码,对代码进行静态分析,做单元测试,测试通过之后就可以build镜像,镜像构建成功后就把镜像push推送到Harbor镜像仓库中,
02.调用kubernetes集群的restAPI更新服务,而后kubernetes接收到了更新的指令,
 从Harbor镜像仓库pull拉取镜像,从而完成服务的更新与重启,
03.最后我们从客户端来访问kubernetes集群的服务

Gitlab 项目管理

 01.Gitlab
   GitLab中的项目的根目录下创建的.gitlab-ci.yml 脚本的工具

 02. GitLab Runner是一个开源的项目,是用来执行GitLab中的项目的根目录下创建的.gitlab-ci.yml 脚本的工具
      GitLab Runner is open-source and written in Go.
   It can be run as a single binary; no language-specific requirements are needed.
  极狐GitLab持续集成 (CI)持续交付(CD)
      构建、测试和部署。从可扩展的Pipeline到集成安全性
  	合并请求(MR)中共享新代码,并触发Pipeline。构建、测试和验证
  	
  自动集成部署
      组件 
       Gitlab	       代码版本管理
       GitLab-CI     就是一套配合GitLab使用的持续集成系统,GitLab8.0以后的版本是默认集成了GitLab-CI并且默认启用的。	 
       GitLab-Runner 就是一个用来执行软件集成脚本的东西  
	Gitlab-runner 概念
	   pipeline  
	   Stage  CI 环节包含三个 Stage:build、test 和deploy。
	   Job
  	   
	权限
        Shared   runners   are for use by all projects
        Group    runners   are for all projects and subgroups in a group
        Specific runners   are for individual projects
     tag  
       runner has the tag value, acorrespond  project’s .gitlab-ci.yml file	
    配置文件  config.toml file.		
 03.软件
     Git:git 
     Gitlab   Gitlab Runner	
     Docker	
     Harbor		 

gitlab-runner安装步骤

 步骤
 linux 准备工作	 
  useradd -d /home/deploy -m deploy -s /bin/bash
  passwd deploy
 
 1. 安装docker
     安装docker
	 下拉 docker pull gitlab/gitlab-runner
 2. 运行gitlab-runner容器
  docker run -d 
    --name gitlab-runner --restart always  \
    -v /var/run/docker.sock:/var/run/docker.sock   \
    -v /srv/gitlab-runner/config:/etc/gitlab-runner  \
     gitlab/gitlab-runner:latest
 
 3. 注册为gitlab-ci的runner
   启动 Runner 然后和 GitLab CI 绑定
      非docker的形式 gitlab-ci-multi-runner register :执行注册命令
	     输入 ci 地址  输入 ci token   runner 名称  设置 tag
		 - 挂载docker.sock是为了docker:latest镜像操控runner服务器的docker服务;
		 说明: An executor determines the environment each job runs in.
		       the  shell executor.
			   the Docker executor.
  docker exec gitlab-runner gitlab-runner register -n \
   --url http://192.168.1.211:8180/ \
   --registration-token RNbs-Sh57ap4u \
   --tag-list runmyDocker   \
   --executor docker \
   --docker-image docker \
   --docker-volumes /var/run/docker.sock:/var/run/docker.sock \
   --description "runmyDocker   
    --template-config /tmp/test-config.template.toml \  
  
  # register 默认交互模式下使用,非交互模式添加 --non-interactive
  gitlab-runner 注销
     注销全部 docker exec gitlab-runner gitlab-runner unregister --all-runners

自动构建组件

基本组件
  代码库-版本库GitLab  GitLab CI 
  执行构建任务 GitLab-runner 
  镜像库 harbor
  资源库: 文件系统或者分布式文件系统 或者对象存储
具体文件
    项目工程下编写 .gitlab-ci.yml 配置文件
  
  fatal: unable to access 'https://mygitlab.com/mydata.git/': Could not resolve host: mygitlab.com.com
解决方式  
 在配置文件.toml中增加host和IP的映射关系
 [runners.docker]
  tls_verify = false
 extra_hosts = ["gitlab.com:1.1.1.1", "harbor.com:2.2.2.2"]
 shm_size = 0	 

配置文件

01.Gitlab 配置文件

.gitlab-ci.yml 共28个关键词 pipeline
  管道配置从作业(job)开始, 作业是 .gitlab-ci.yml 文件的最基本元素
   job 了可用的参数
      image  services  environment  cache  dependencies
	  stages  stage only  
	  tags  retry  trigger  include
	  script  before_script  after_script  allow_failure  when  artifacts
	  coverage  except extends interruptible pages  parallel
	  release  resource_group rules  timeout  variables  
   新建作业(job)、定义阶段(stage)、允许失败(allow_failure)、
       什么时候开始工作。
      也可用:when:manual和when:delayed
      人工触发(when: manual)、
      作业运行前执行的脚本(before_script)
       作业运行后执行的脚本(after_script )

02.Gitlab-runner配置文件

config.toml
  [[runners]]
    name   url  token
	environment  executor  shell tags
    executor  
      [runners.kubernetes]
      [runners.docker]  
      [runners.ssh]
      [runners.virtualbox]
    shell bash sh powershell  pwsh
    [runners.docker]  
     image
     dns  dns_search
     extra_hosts
	[[runners.docker.services]]

03.Dockerfile配置文件

  FROM habor.test.com/testil:latest as ReleaseData
  ENV OBSUTIL /root/bin/obsuti
  ARG H_VERSION
  COPY ./entrypoint.sh /
  RUN mkdir -p /opt/test \
  && echo '1.1.1.1 cn1.test.com' | sudo tee -a /etc/hosts \
  && $OBSUTIL cp -r -f os://test/ap /opt/test/${H_VERSION}  
  ENTRYPOINT '/entrypoint.sh'

配置文件格式

语言的表达能力,书写便捷性 
  INI(.ini)文件
  XML
  JSON(.json)
  YAML(.yaml 或 .yml)
  TOML(.toml)  显式键名链的方式  使用#来表示注释开始,至当前行尾结束。 数组使用方括号包裹  表格数组

变量

Gitlab-runner变量

#变量的共性问题
  key  value  Type 
  Environment scope
  Protect variable
  Mask variable
不同变量:优先级  变量的执行环境
  
变量的情况
  01.Trigger variables.
   CI/CD variables in triggered pipelines display on each job’s page,
    	 but only users with the Owner and Maintainer role can view the values.	
  02.Scheduled pipeline variables.
  03.Manual pipeline run variables.
  04.Variables added when creating a pipeline with the API.	
  05.Project CI/CD variables.  项目(projects)
         权限:Only project members with the Maintainer role can add or update project CI/CD variables.
           设置位置: project’s Settings > CI/CD and expand the Variables section   
  06.Group CI/CD variables.    项目组(groups)
          设置位置:  project’s Settings > CI/CD  Add Variable
  07.Instance CI/CD variables.
       设置方式 define instance variables via the UI or API
  	 
  08.Variables defined in jobs in the .gitlab-ci.yml file.
  09.Variables defined outside of jobs (globally) in the .gitlab-ci.yml file.
  10.Deployment variables.
  11.Predefined variables.
变量类型
   Variable type: All predefined CI/CD variables and variables defined in the .gitlab-ci.yml file
   File typ : need a file as input.
变量
   mask
   protect:  only passed to pipelines running on protected branches or protected tags.
   CI/CD variable security
列出所有环境变量  export  

报错以及解决

现象:若已经配置好了gitlab-runner了,执行commit,pipeline状态一直是pending,并且提示:  
  This build is stuck, because the project doesn't have any runners online assigned to it. Go to Runners page 

 这个是因为未找到对应的runner导致的,
    原因一是有可能gitlab-runner注册失败,
    原因二有可能是.gitlab-ci.yml配置文件里面tags没有匹配到已注册可用的runner。 
	
关于原因二
 runner和job匹配的规则是,runner的tag和项目的tag要相同。因为你的项目里可能没有标注tag,那么就要选择这个按钮		
	解决方式
	   01.settings/ci_cd--》Runners
	        Run untagged jobs   Indicates whether this runner can pick jobs without tags
			查看tag 
	  02.在 .gitlab-ci.yml
	          tags:
                - "workflow"
	参考
       搭建内部的gitlab服务器遇到的坑总结	  https://blog.csdn.net/Hello_Ray/article/details/85273600	
       Docker搭建自己的Gitlab CI Runner  https://cloud.tencent.com/developer/article/1010595	
 关于原因一
       01. Kubernetes部署gitlab-runner   https://tyyzqmf.github.io/2018/11/08/Kubernetes%E9%83%A8%E7%BD%B2gitlab-runner/	 
         第一: 注册token(registration token)是什么呢?就是gitlab runners管理界面那个token		
		 第二: 这个config.toml的token是runner注册成功后生成的token(或者说是一个随机ID),并不是注册token(registration token)
		  注册好的runner的token用来注册的时候使用
		02. Deploy Gitlab Runner With Docker https://blog.programster.org/deploy-gitlab-runner-with-docker
		  Debugging
		     docker logs gitlab-runner
			   All Runner request return Forbidden
		03.配置文件修改后--docker restart gitlab-runner
		  		
		04.配置
		  docker exec gitlab-runner gitlab-runner register -n 
		  --url https://pub-gitlab.test.com/ 
		  --registration-token zQp-FEp7 
		  --tag-list workflow 
		  --executor docker 
		  --docker-image ubuntu:18.04 
		  --docker-volumes /var/run/docker.sock:/var/run/docker.sock
		  --description "rungitlabD" 
		  --docker-extra-hosts "gitlab.test.com:12.22.22.22" 
		  --docker-extra-hosts "harbor.test.com:12.22.22.22" 

参考

  https://docs.gitlab.com/runner/
  https://docs.gitlab.com/runner/configuration/advanced-configuration.html
  .gitlab-ci.yml语法完整解析(三)  https://blog.csdn.net/github_35631540/article/details/111029174
  使用主机安装查看runner配置  https://www.cnblogs.com/wu-wu/p/13269950.html
posted @ 2022-03-01 19:00  辰令  阅读(96)  评论(0编辑  收藏  举报