如何配置GitlabCI
迪丽瓦拉
2025-05-29 23:27:25
0

本例主要以Centos7编译Java项目为案例,但同时会附上相关的官方文档链接。

1.准备编译环境

首先需要准备编译环境,此环境为经过测试的可以编译项目的机器(一般设置虚拟机)。
我们需要在此环境上安装编译项目的所有依赖内容,如jdk,python(指定版本确认)等。

2.获取注册令牌

登录gitlab管理员账号,访问地址http://your_ip_addr/admin/runners,在右上侧有标明安装过程中使用的注册令牌,记住该令牌备下面的步骤使用。

3.安装 GitLab Runner

进入到编译环境,执行:

curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

下载完启动程序后,赋予 gitlab-runner 可执行权限,执行:

chmod +x /usr/local/bin/gitlab-runner

执行以下命令将gitlab-runner配置为Centos7服务:

gitlab-runner install --user=root --working-directory=/opt/gitlab-runner

启动runner:

gitlab-runner start

4.注册Runner

(1)在编译环境上执行:

gitlab-runner register

(2)输入gitlab地址:

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
http://your_ip_addr

(3)输入步骤2获取到的注册令牌:

Please enter the gitlab-ci token for this runner
xxx

xxx为输入的令牌,替换为获取到的令牌即可。

(4)输入Runner的描述:

Please enter the gitlab-ci description for this runner
[hostname] seagull-server-builder

这里选择输入一个明显易懂的名称即可。

(5)输入标签:

Please enter the gitlab-ci tags for this runner (comma separated):
my-tag,another-tag

这里的tag会在gitlab-ci.yml文件中配置编译步骤时用到,可以输入多个,以英文逗号分割。
tag可以在gitlab管理中心中手动调整。

(6)输入Executor:

Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
shell

这里一般选择shell即可。

进入http://your_ip_addr/admin/runners,找到刚配置好的gitlab-runner,点击编辑,在左下的Restrict projects for this Runner配置项下指定需要编译的项目到此Runner,点击项目名后面的按钮Enable。

至此,gitlab-runner环节配置完毕,相关参考文档见:

  • Linux上安装Runner:https://docs.gitlab.com/runner/install/linux-manually.html
  • Windows上安装Runner:https://docs.gitlab.com/runner/install/windows.html
  • 获取令牌相关:https://docs.gitlab.com/ee/ci/runners/
  • 注册Runner:https://docs.gitlab.com/runner/register/index.html
  • https://docs.gitlab.com/runner/

5.配置 .gitlab-ci.yml

若想让gitlab-runner可以编译项目,必须要在项目根目录下新建文件.gitlab-ci.yml。(注意文件名前面的点号)
该文件创建完毕后,每次提交变更后都会创建一个编译任务,可以在
http://your_ip_addr/yourproject/pipelines
下看到生成的任务。
以maven项目为例,编译时需要执行mvn clean package,则可以配置如下的ci文件:

.gitlab-ci.ymlstages:- buildbuild-server:stage: buildenvironment:name: devtags:- serverscript: - mvn clean packagewhen: manual
  • stages指定编译需要执行的编译任务;
  • build-server为配置的任务,指定了stage为build,所以编译时会调用此任务;
  • tags下面指定的server为步骤4最后配置的tag,用于指定哪个runner会被调用;
  • script中添加编译要执行的命令行内容;
  • when:manual表示的是此任务创建后不会立刻执行,如果没有此项,每次提交后会立刻开始编译,添加后,需要手动启动。

更详细的配置方式参考:https://docs.gitlab.com/ee/ci/quick_start/README.html#creating-a-gitlab-ciyml-file

相关内容