gitlab使用runner来实现CI/CD

1:安装runner

比如,我们需要在192.168.3.129服务器上来实现自动部署,那我们就在这台服务器上安装runner

在gitlab后台,比如tn项目,那我们进入项目,在设置中,找到CI/CD

点击展开,新建项目runner

 选择项目信息,Linux、标签自己填写、下面的勾记得勾选一下(运行未打标签的作业),后面的可写可不写,然后创建runner就可以了

 点击如何安装Runner?右侧会弹出来安装步骤,按照步骤进行安装,安装完成后,执行步骤1即可。直至下方出现已完成,就可以点击进入runners了。

 2:为项目开启Runner

因为我们是在当前项目下直接创建的,所以就不需要操作了,记得跟下图一直即可

 3:编写gitlab-ci文件

我是想在dev分支合并到master分支后,自动更新到服务器:192.168.3.129(runner服务器)的/root/tn,就是合并后在/root/tn目录下自动git pull

1:在dev分支编写gitlab-ci文件

这个看清楚,我是先进入了目录,然后切换到了分支,然后在更新。这样一个流程

[root@centos7-2 tn]# git branch
* dev
  master
[root@centos7-2 tn]# cat .gitlab-ci.yml 
stages:
  - deploy

deploy:
  stage: deploy
  script:
    - cd /root/tn
    - git checkout master
    - git pull origin master

2:上传文件

[root@centos7-2 tn]# touch 3.txt
[root@centos7-2 tn]# 
[root@centos7-2 tn]# git add --all
[root@centos7-2 tn]# git commit -m 'test'
[dev 420c213] test
 2 files changed, 1 insertion(+), 1 deletion(-)
 create mode 100644 3.txt
[root@centos7-2 tn]# git push -u origin dev
Counting objects: 12, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (7/7), 599 bytes | 0 bytes/s, done.
Total 7 (delta 0), reused 0 (delta 0)
remote: 
remote: To create a merge request for dev, visit:
remote:   http://gitlab.xxxxx.com/hello/tn/-/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote: 
To git@gitlab.xxxxx.com:hello/tn.git
   7c8c2b2..420c213  dev -> dev
Branch dev set up to track remote branch dev from origin.

3:gitlab页面合并代码

 4:查看是否更新成功

[root@centos7-2 tn]# git branch
* master
[root@centos7-2 tn]# ll
total 112
-rw-r--r--. 1 root root 107134 Jul 11 05:35 20230710.png
-rw-r--r--. 1 root root      0 Jul 11 05:59 3.txt
drwxr-xr-x. 3 root root     23 Jul 11 05:45 builds
-rw-r--r--. 1 root root     12 Jul 11 05:45 test

 5:错误

如出现如下错误

Running with gitlab-runner 16.1.0 (b72e108d)
  on centos7-2 CGeRVAydQ, system ID: s_7e6f1c3075a0
Preparing the "shell" executor
00:00
Using Shell (bash) executor...
Preparing environment
00:00
Running on centos7-2...
Getting source from Git repository
00:01
Fetching changes with git depth set to 20...
Reinitialized existing Git repository in /home/gitlab-runner/builds/CGeRVAydQ/0/hello/adk/.git/
Checking out 1aa24cc1 as detached HEAD (ref is main)...
Skipping Git submodules setup
Executing "step_script" stage of the job script
00:00
$ ssh root@192.168.3.129 "cd /home/adk && git pull -u origin main"
Host key verification failed.
ERROR: Job failed: exit status 1

在gitlab-runner服务器执行如下命令,然后在web页面重新执行流水线即可

[root@centos7-2 tn]# gitlab-runner uninstall
Runtime platform                                    arch=amd64 os=linux pid=24209 revision=b72e108d version=16.1.0
[root@centos7-2 tn]# gitlab-runner install --user root
Runtime platform                                    arch=amd64 os=linux pid=24228 revision=b72e108d version=16.1.0
[root@centos7-2 tn]# systemctl restart gitlab-runner
如出现以下错误:
Running with gitlab-runner 16.1.0 (b72e108d)
  on centos7-2 YggtSCMy4, system ID: s_7e6f1c3075a0
Preparing the "shell" executor
00:00
Using Shell (bash) executor...
Preparing environment
00:00
Running on centos7-2...
Getting source from Git repository
00:01
Fetching changes with git depth set to 20...
Reinitialized existing Git repository in /home/gitlab-runner/builds/YggtSCMy4/0/hello/adk/.git/
fatal: git fetch-pack: expected shallow list
fatal: The remote end hung up unexpectedly
ERROR: Job failed: exit status 1

按照如下执行,然后在web页面重新执行流水线即可

rm -rf /home/gitlab-runner/builds/YggtSCMy4/0/hello/adk/.git/

6:builds

如果我们的项目比较大的时候,在该runner第一次执行的项目目录下有一个builds的文件夹,这个文件夹有着我们的代码,如果我们的项目较大,那么文件都会存放进去,这样就会占用很多内容

所以我们在执行gitlab-ci的时候,再加一条删除的命令即可

[root@centos7-2 ta]# cat .gitlab-ci.yml 
stages:
  - deploy

deploy:
  stage: deploy
  script:
    - cd /home/ta
    - git checkout master
    - git pull origin master
  after_script:
    - rm -rf /home/tn/builds/YggtSCMy4/0/hello/ta/

这个目录也可以在web页面流水线执行的时候看到

 

posted @ 2023-07-11 18:00  Old·Artist  阅读(35)  评论(0编辑  收藏  举报