为GitLab项目配置CI/CD持续集成
前提
1.已经安装好了Gitlab服务
2.准备一台linux服务器(示例以CentOS7)
3.这里以安装配置Gitlab-Runner,并且为Gitlab上某一项目配置Runner为例
4.安装的Runner以基于Linux为例,对应的Executor以docker为例
相关说明:Gitlab的CI/CD需要在项目下增加一个.gitlab-ci.yml文件,而该文件中定义的任务(job)是由Gitlab-Runner来调度的,
而真正执行任务是注册Gitlab-Runner时指定的Executor。可以根据项目实际情况,注册多个Gitlab-Runner对应多个Executor,每一个Executor都是独立环境。
准备工作
由于安装过程中,会使用到sudo命令,因此确保sudo命令可以执行。
编辑/etc/sudoers文件,找到
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
修改为:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
开始安装GitLab-Runner (Linux,root用户模式下)
下载gitlab-runner 二进制安装文件
# Linux x86-64
sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
# Linux x86
sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386
# Linux arm
sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm
给gitlab-runner 赋予权限
sudo chmod +x /usr/local/bin/gitlab-runner
官方教程会在这里创建一个gitlab-runner用户,用于后面job的执行,但由于权限问题,导致后续deploy会出现不少问题,这里暂时用root用户
sudo gitlab-runner install --user=root --working-directory=/app/gitlab-runner
sudo gitlab-runner start
至此,gitlab-runner 已经安装成功,可执行 gitlab-runner status 查看运行状态
注册Gitlab-Runner
首先,打开你的GitLab-Runner,找到你要配置Gitlab CI/CD 持续集成的项目,如:gitlab-demo
依次打开: gitlab-demo -> setting -> CI/CD -> Runners -> Set up a specific Runner manually
找到以下内容:URL ,Token
回到linux终端(root模式下),执行:
# gitlab-runner register
Please enter the gitlab-ci coordinator URL:
# 输入上面步骤找到的URL,示例:http://gitlab.alibaba-inc.com/ci
Please enter the gitlab-ci token for this runner:
# 输入上面步骤找到的Token
Please enter the gitlab-ci description for this runner:
# 为该gitlab runner自定义一个名称,示例:gitlab-runner-docker-dev (表示该runner是基于docker的executer的dev环境)
Please enter the gitlab-ci tags for this runner (comma separated):
# 为该gitlab runner定义一组tag,以逗号隔开,表示只有匹配到这些tag,才会用改runner执行job。示例:docker-dev
Whether to run untagged builds [true/false]:
# true
Please enter the executor: docker, parallels, shell, kubernetes, docker-ssh, ssh, virtualbox, docker+machine, docker-ssh+machine:
# 为该runner指定一个executor,由于这里用docker作为示例,输入:docker
Please enter the default Docker image (e.g. ruby:2.1):
# 输入默认的docker镜像,根据项目需要自定义,官方默认为alpine:latest,这里我们的项目是基于maven的java项目,所以输入:maven:3-jdk-8
到这里,Gitlab-Runner已注册完成,但需要重启一下,输入:
gitlab-runner restart
这时,可以在项目中看到该runner已经配置好,且处于运行中(绿色图标)
依次打开: gitlab-demo -> setting -> CI/CD -> Runners -> Set up a specific Runner manually 下面可以看到上面配置的Runner,已在运行中。
配置gitlab-runner环境
由于Gitlab-Runner的Executor在执行每一个job时,是在独立环境中执行的,而我们实际项目中,经常需要配置多个执行job,或者项目是由多个maven项目构成的。
这里就会出现需要共享maven的.m2仓库问题,可以通过为Runner配置映射关系,指定共享目录即可。
编辑GitLab-Runner的配置文件:
vi /etc/gitlab-runner/config.toml
找到上面配置的Runner (gitlab-runner-docker-dev),在配置项volumes添加:"/root/.m2:/root/.m2"和pull_policy = "if-not-present" 修改后如下:
[[runners]]
name = "gitlab-runner-docker-dev"
url = "https://www.keaitupian.com"
token = "10c11056ca68897428370c4e992051"
executor = "docker"
[runners.docker]
tls_verify = false
image = "maven:3-jdk-8"
privileged = false
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/cache","/home/gitlab-runner-new/.m2:/root/.m2"]
pull_policy = "if-not-present"
shm_size = 0
[runners.cache]
[runners.cache.s3]
[runners.cache.gcs]
Gitlab-Runner配置完成,重启:
gitlab-runner restart
至此,基于Linux配置GitLab-Runner已全部完成。
基于Docker配置GitLab-Runner
默认环境已安装了docker
拉取gitlab-runner镜像
sudo docker pull gitlab/gitlab-runner:latest
启动容器:
sudo docker run -d --name gitlab-runner --restart always \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
gitlab/gitlab-runner:latest
注册Gitlab-runner,通过以下命令进入注册:
sudo docker exec -it gitlab-runner gitlab-ci-multi-runner register
注册过程,跟上面linux一致。
网友评论