環境准備:
- gitlab賬號公網賬號:代碼倉庫和編譯器
- 目標機:裝有
docker
和gitlab-runner
環境的服務器(Linux或類unix機器,我使用的時centos - 項目代碼:testgolang為例(gitlab官網倉庫)
- Dockerfile:對程序編譯后打鏡像
- .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