GitLab CI/CD 配置

时间:2021-12-23 05:03:58

GitLab CI/CD 配置

概念

持续集成的相关概念,可以看这篇文章 持续集成是什么? - 阮一峰的网络日志

操作示例

创建测试项目 sample-web,然后打开项目的 Runners 配置

GitLab CI/CD 配置

找到这个地方,后边要用

GitLab CI/CD 配置

然后搭建 gitlab-runner,CI/CD 需要通过它实现

# 创建 gitlab-runner 目录
mkdir -p /usr/local/gitlab-runner && cd /usr/local/gitlab-runner # 映射目录
mkdir -p /usr/local/gitlab-runner/config # 启动
docker run -d --name gitlab-runner --restart always \
-v /usr/local/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
gitlab/gitlab-runner:v12.10.0

注册 runner

docker exec -it gitlab-runner gitlab-ci-multi-runner register

根据提示逐步输入信息

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.10.227/ # 这是上边 GitLab Runners 里的地址
Please enter the gitlab-ci token for this runner:
be1FkhGu1nN3dsoa5Wnv # 这是上边 GitLab Runners 里的Token
Please enter the gitlab-ci description for this runner:
[32e99182c32c]: helloworld # 描述,*发挥
Please enter the gitlab-ci tags for this runner (comma separated):
java8 # tags,相当于这个环境的名字,后边用的到
Registering runner... succeeded runner=be1FkhGu
Please enter the executor: docker, docker-ssh, ssh, docker+machine, docker-ssh+machine, custom, parallels, shell, virtualbox, kubernetes:
docker # 执行方式,我用的 docker
Please enter the default Docker image (e.g. ruby:2.6):
maven:3-jdk-8 # docker 镜像,这是个 java 项目,我用的 maven:3-jdk-8,根据自己需要填写
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

提示成功,可以在 GitLab Runners 里看到我们刚刚注册的 runner

GitLab CI/CD 配置

在项目中添加 .gitlab-ci.yml 文件

文件描述了有哪些步骤,每步的名称,执行了什么命令,使用哪个分支,使用哪个 runner 等信息

stages:
- build
- test
- deploy job_build:
stage: build
script:
- mvn clean install -s settings.xml
only:
- master
tags:
- java8 job_test:
stage: test
script:
- mvn test -s settings.xml
tags:
- java8 job_deploy:
stage: deploy
script:
- echo "deploy over..."
tags:
- java8

提交代码,可以看到 CI/CD 的执行情况

GitLab CI/CD 配置