本文主要是介绍GitLab Runner 安装、CI/CD流水线配置指南,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
GitLab Runner 安装与配置指南
介绍
在本指南中,我们将介绍如何在GitLab服务器上安装并配置GitLab Runner,以及如何编写一个简单的GitLab CI/CD流水线配置文件,来实现自动化的测试、编译、打包和发布。
安装 GitLab Runner
下载并安装 GitLab Runner
- 下载 GitLab Runner 二进制文件
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
- 赋予执行权限
sudo chmod +x /usr/local/bin/gitlab-runner
- 创建 GitLab Runner 用户
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
- 安装并以服务的形式运行
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
解决权限问题
如果在执行流水线任务时遇到以下错误:
ERROR: Job failed: prepare environment: exit status 1.
可以注释掉 /home/gitlab-runner/.bash_logout
中的以下代码段:
if [ "$SHLVL" = 1 ]; then[ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi
若遇到以下权限问题:
ERROR: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock:
需要给 gitlab-runner 用户赋予 Docker 权限:
sudo usermod -aG docker gitlab-runner
sudo systemctl restart docker
编写流水线配置文件
在项目的根目录下,创建 .gitlab-ci.yml
文件。以下是一个示例配置:
stages: # 定义阶段- mvn-test- mvn-package-docker-pushvariables: # 设置常量DOCKER_IMAGE_NAME_ENGINE: engine:1.0.0DOCKER_IMAGE_NAME_ENGINE_SCHEDULER: engine-scheduler:1.0.0DOCKER_IMAGE_NAME_ENGINE_SCHEDULER_UI: engine-scheduler-ui:1.0.0DOCKER_IMAGE_PUSH_NAME_ENGINE: 192.168.1.76:8083/engine:1.0.0DOCKER_IMAGE_PUSH_NAME_ENGINE_SCHEDULER: 192.168.1.76:8083/engine-scheduler:1.0.0DOCKER_IMAGE_PUSH_NAME_ENGINE_SCHEDULER_UI: 192.168.1.76:8083/engine-scheduler-ui:1.0.0mvn-test:stage: mvn-testtags:- enginescript:- echo "Start mvn-test... "- mvn test- echo "End mvn-test."mvn-package-docker-push:stage: mvn-package-docker-pushtags:- enginewhen: manualscript:- echo "Start mvn-package... "- mvn clean package- echo "End mvn-package. "- echo "Start docker-build... "- cd engine/engine-app- docker build -t $DOCKER_IMAGE_NAME_ENGINE .- cd ../../scheduler/scheduler-server- docker build -t $DOCKER_IMAGE_NAME_ENGINE_SCHEDULER .- cd ../../scheduler/scheduler-ui- docker build -t $DOCKER_IMAGE_NAME_ENGINE_SCHEDULER_UI .- echo "End docker-build. "- echo "Start docker-push-sit... "- docker tag $DOCKER_IMAGE_NAME_ENGINE $DOCKER_IMAGE_PUSH_NAME_ENGINE- docker tag $DOCKER_IMAGE_NAME_ENGINE_SCHEDULER $DOCKER_IMAGE_PUSH_NAME_ENGINE_SCHEDULER- docker tag $DOCKER_IMAGE_NAME_ENGINE_SCHEDULER_UI $DOCKER_IMAGE_PUSH_NAME_ENGINE_SCHEDULER_UI- docker login 192.168.1.76:8083 -u nexus_app -p lianyu_123- docker push $DOCKER_IMAGE_PUSH_NAME_ENGINE- docker push $DOCKER_IMAGE_PUSH_NAME_ENGINE_SCHEDULER- docker push $DOCKER_IMAGE_PUSH_NAME_ENGINE_SCHEDULER_UI- echo "End docker-push-sit. "
创建 Runner
- 在 GitLab 界面中创建 Runner
在“CI/CD Settings -> Runners”页面,点击“New project runner”,配置以下主要选项:
- Tags:按产品命名,如
engine
- Runner description:同上
- Maximum job timeout:设置超时时间,最小600秒
- 注册 Runner
在 GitLab 服务器上执行以下命令来注册Runner:
sudo gitlab-runner register --url http://192.168.1.74 --token glrt-WfmzN-7izBW81ZF_x7Xw
注册过程如下:
Runtime platform arch=amd64 os=linux pid=2085600 revision=81ab07f6 version=16.10.0
Running in system-mode. There might be a problem with your config based on jsonschema annotations in common/config.go (experimental feature):
jsonschema: '/runners/0/Monitoring' does not validate with https://gitlab.com/gitlab-org/gitlab-runner/common/config#/$ref/properties/runners/items/$ref/properties/Monitoring/$ref/type: expected object, but got nullEnter the GitLab instance URL (for example, https://gitlab.com/):
[http://192.168.1.74]:
Verifying runner... is valid runner=WfmzN-7iz
Enter a name for the runner. This is stored only in the local config.toml file:
[ubuntu-gitlab]: integration
Enter an executor: kubernetes, docker-autoscaler, instance, custom, ssh, docker, docker-windows, docker+machine, shell, parallels, virtualbox:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"
查看流水线执行情况
在合并请求(Merge Requests)中可以查看流水线状态。如果显示绿色,表示代码执行成功。点击流水线编号,可以查看各阶段的详细运行情况。
总结
通过以上步骤,我们完成了GitLab Runner的安装与配置,并编写了一个示例的GitLab CI/CD流水线配置文件。这样可以实现代码的自动化测试、编译、打包和发布,提高开发效率。希望本指南对您有所帮助。
这篇关于GitLab Runner 安装、CI/CD流水线配置指南的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!