gitlab的CI/CD实现

环境准备:

  1. gitlab账号公网账号:代码仓库和编译器
  2. 目标机:装有dockergitlab-runner环境的服务器(Linux或类unix机器,我使用的时centos
  3. 项目代码:testgolang为例(gitlab官网仓库)
  4. Dockerfile:对程序编译后打镜像
  5.  .gitlab-ci.yml :CI/CD的gitlab机器运行逻辑的操作文档

 一  环境配置

1.1 配置gitlab

点击Expand进入

 

1.2 服务器配置

安装docker参考:https://www.cnblogs.com/zyxnhr/p/11825331.html

安装gitlab-runner:参考https://docs.gitlab.com/runner/install/linux-manually.html

安装完成后,像gitlab进行注册

 注册之后,查看相关注册的信息

[root@iZj6c56sisrhp6wdto3autZ ~]# cat /etc/gitlab-runner/config.toml

复制代码
concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "mbp13-local-runner"
  url = "https://gitlab.com/"
  token = "KM2x1z2gmF_Np78Eos7r"
  executor = "shell"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
复制代码

1.3 验证环境

[root@iZj6c56sisrhp6wdto3autZ ~]# gitlab-runner verify

Runtime platform                                    arch=amd64 os=linux pid=21733 revision=ce065b93 version=12.10.1
Running in system-mode.

Verifying runner... is alive                        runner=KM2x1z2g

[root@iZj6c56sisrhp6wdto3autZ ~]# gitlab-runner list

Runtime platform                                    arch=amd64 os=linux pid=21741 revision=ce065b93 version=12.10.1
Listing configured runners                          ConfigFile=/etc/gitlab-runner/config.toml
mbp13-local-runner                                  Executor=shell Token=KM2x1z2gmF_Np78Eos7r URL=https://gitlab.com/

 返回gitlab页面,就可以看到注册的信息

已经注册成功,查看准备的项目

1.4 环境说明

这里的项目时是直接从原作者的github项目中导入,实验使用https://github.com/yangshun2005/gitlab-cicd

直接导入,在这里出现一个问题,本来老师的github中,真正的实验项目只有testlong,所以这样的话,不管你怎么修改testgolang的内容,也无法触发CDCD功能,这一块卡了很久,只需要把下面的两个文件拷贝出来,根据自己的实际进行部分修改调整,就可以使用

 调整后的.gitlab-ci.yml文件

复制代码
stages:
  - deploy

docker-deploy:
  stage: deploy
# 执行Job内容
  script:
# 通过Dockerfile生成cicd-demo镜像
    - docker build -t testgolang .
# 删除已经在运行的容器
    - if [ $(docker ps -aq --filter name= testgolang) ]; then docker rm -f testgolang;fi
# 通过镜像启动容器,并把本机8001端口映射到容器8001端口
    - docker run -d -p 8001:8001 --name testgolang testgolang
  tags:
# 执行Job的服务器
    - mbp13
  only:
# 只有在master分支才会执行
    - master
复制代码

Dockerfile文件

复制代码
# 镜像文件
FROM golang:latest
# 维修者,这个作者的联系方式
MAINTAINER William "2095686947@qq.com"

# 镜像中项目路径
WORKDIR $GOPATH/src/chinaase.com/testgolang
# 拷贝当前目录代码到镜像,这里需要注意,因为go的项目位置出现变化,需要调整,对比老师的源码
COPY  ./testgolang   $GOPATH/src/chinaase.com/testgolang  
# 制作镜像
RUN go build .

# 暴露端口
EXPOSE 8001

# 程序入口
ENTRYPOINT ["./testgolang"]
复制代码

go项目

复制代码
package main

import "net/http"

func main() {
    http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("hello chinaase willim ,I running in docker-container and buit by gitlab_cicd_test6"))
    })
    http.ListenAndServe(":8001", nil)
}
复制代码
 

二 CICD操作以及问题

然后就可以修改master分支的代码进行提交,就可以触发CICD功能,一般的操作,使用git操作git相关命令

git clone  git地址 #如果右分支,可以使用-b  分支名
修改文件
git  commit -m “描述”
git push origin 分支 #如果是master,直接git push 即可

这里为了方便,就再gitlab上进行修改提交,我这里每次修改的是go项目中的输出部分,test1-test6修改,触发CICD

2.1 git版本问题

出现问题

出现这个问题,原因是git的版本太低,升级git的版本

[root@iZj6c56sisrhp6wdto3autZ ~]# yum install http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm

[root@iZj6c56sisrhp6wdto3autZ ~]# yum install git

[root@iZj6c56sisrhp6wdto3autZ ~]# git version

git version 2.22.0

 再次修改触发

2.2 golang项目的位置

 这个原因是Dockerfile中,去找testgolang中go的项目,但是没有找到,修改Dockerfile中的位置即可,对比园项目中的Dockerfile文件,和这里调整的文件按进行对比

2.3 成功触发

修改触发后,pass

查看job的信息

复制代码
Running with gitlab-runner 12.10.1 (ce065b93)
   on mbp13-local-runner KM2x1z2g
Preparing the "shell" executor
00:00
 Using Shell executor...
Preparing environment
00:00
 Running on iZj6c56sisrhp6wdto3autZ...
Getting source from Git repository
00:09
 Fetching changes with git depth set to 50...
 Reinitialized existing Git repository in /home/gitlab-runner/builds/KM2x1z2g/0/ningherui/gitlab-cicd/.git/
 From https://gitlab.com/ningherui/gitlab-cicd
  * [new ref]         refs/pipelines/139580859 -> refs/pipelines/139580859
    f43e867..052cbcd  master                   -> origin/master
 Checking out 052cbcde as master...
 Skipping Git submodules setup
Restoring cache
00:00
Downloading artifacts
00:00
Running before_script and script
00:02
 $ docker build -t testgolang .
 Step 1/7 : FROM golang:latest
  ---> 07799c7aa10b
 Step 2/7 : MAINTAINER William "2095686947@qq.com"
  ---> Using cache
  ---> fd734b5d17bb
 Step 3/7 : WORKDIR $GOPATH/src/chinaase.com/testgolang
  ---> Using cache
  ---> ce8abbaf1a11
 Step 4/7 : COPY  ./testgolang   $GOPATH/src/chinaase.com/testgolang
  ---> 24a949942b85
 Step 5/7 : RUN go build .
  ---> Running in d6565c39d4fa
 Removing intermediate container d6565c39d4fa
  ---> 76ac407d4c22
 Step 6/7 : EXPOSE 8001
  ---> Running in ac630ef14ce4
 Removing intermediate container ac630ef14ce4
  ---> a9016c405f94
 Step 7/7 : ENTRYPOINT ["./testgolang"]
  ---> Running in 4c7c3b355466
 Removing intermediate container 4c7c3b355466
  ---> e30f956c0da8
 Successfully built e30f956c0da8
 Successfully tagged testgolang:latest
 $ if [ $(docker ps -aq --filter name= testgolang) ]; then docker rm -f testgolang;fi
 "docker ps" accepts no arguments.
 See 'docker ps --help'.
 Usage:  docker ps [OPTIONS]
 List containers
 $ docker run -d -p 8001:8001 --name testgolang testgolang
 b9943d3f61c1a084e1fb49fec48048da28ca0216679e5ec20b1bf3680369a040
Running after_script
00:00
Saving cache
00:00
Uploading artifacts for successful job
00:00
 Job succeeded
复制代码


三 结果验证

3.1 服务器验证

查看容器

[root@iZj6c56sisrhp6wdto3autZ ~]# docker ps -a

[root@iZj6c56sisrhp6wdto3autZ ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                    NAMES
b9943d3f61c1        testgolang          "./testgolang"           20 minutes ago      Up 20 minutes               0.0.0.0:8001->8001/tcp   testgolang

查看镜像

[root@iZj6c56sisrhp6wdto3autZ ~]# docker image ls

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
testgolang          latest              e30f956c0da8        21 minutes ago      824MB

3.2 访问验证

访问http://47.57.21.136:8001/hello测试

基本实现CI/CD的功能 


参考:https://github.com/yangshun2005/gitlab-cicd

视频连接https://ke.qq.com/course/1346488?platform=1&pay_succ=1#term_id=101443874&pf=midas_group_pay-1000-pc-1000&outTradeNo=GP6659314861203312640&tokenId=E-200424120101412572

 
 
原文地址:https://www.cnblogs.com/IT-Evan/p/12826068.html