使用Docker Compose搭建GitLab的CI/CD集成化部署环境以及使用
at 2年前 ca gitlab pv 1383 by touch
一、安装gitlab-ce
Docker Compose安装gitlab-ce官方教程:
GitLab Docker images | GitLab
Documentation for GitLab Community Edition, GitLab Enterprise Edition, Omnibus GitLab, and GitLab Runner.
1、建立gitlab目录
mkdir -p /data/soft/gitlab
2、在/data/soft/gitlab目录下创建docker-compose.yml
docker-compose.yml的内容如下:
version: "3.9" services: gitlab-ce: image: "gitlab/gitlab-ce:latest" ports: - "8888:8888" - "2222:22" environment: # 此处 ip 须与上面 hostname 一致,端口号自定义 GITLAB_OMNIBUS_CONFIG: | external_url 'http://ljxwtl.cn:8888' gitlab_rails['gitlab_shell_ssh_port'] = 2222 restart: always volumes: - "/data/soft/gitlab/config:/etc/gitlab" - "/data/soft/gitlab/logs:/var/log/gitlab" - "/data/soft/gitlab/data:/var/opt/gitlab"
3、使用docker compose up -d进行安装
docker-compose up -d
4、查看安装进度
docker-compose logs -f gitlab-ce
5、获取初始 root 密码
docker exec -it $(docker ps | grep gitlab | awk '{print $1}') grep 'Password:' /etc/gitlab/initial_root_password
6、访问对应IP:端口(或者域名:端口)(
Sign in · GitLab
GitLab Community Edition
)的Gitlab网站
二、安装gitlab-runner
Docker Compose安装gitlab-runner官方教程:
Run GitLab Runner in a container | GitLab
Documentation for GitLab Community Edition, GitLab Enterprise Edition, Omnibus GitLab, and GitLab Runner.
第一种安装方式:docker compose
1、创建gitlab-ce目录
mkdir -p /data/soft/gitlab-runner
2、在/data/soft/gitlab-runner目录下创建docker-compose.yml
docker-compose.yml的内容如下:
version: "3.9" services: gitlab-runner: image: "gitlab/gitlab-runner:latest" restart: always privileged: true volumes: - "/data/soft/gitlab-runner/config:/etc/gitlab-runner" - "/var/run/docker.sock:/var/run/docker.sock"
3、使用docker compose up -d进行安装
docker-compose up -d
4、查看安装进度
docker-compose logs -f gitlab-runner
第二种安装方式:rpm安装
Install GitLab Runner manually on GNU/Linux | GitLab
https://docs.gitlab.com/runner/install/linux-manually.html
GitLab Runner :: 15.0.0
https://gitlab-runner-downloads.s3.amazonaws.com/latest/index.html
1、下载gitlab-runner的二进制文件
wget https://gitlab-runner-downloads.s3.amazonaws.com/latest/rpm/gitlab-runner_amd64.rpm
2、安装gitlab-runner的rpm安装包
rpm -ivh gitlab-runner_amd64.rpm
3、更改Gitlab-Runner的配置
vim /etc/gitlab-runner/config.toml concurrent = 30 check_interval = 0 [session_server] session_timeout = 1800
4、启动Gitlab-Runner
gitlab-runner start
5、rpm 安装后的git版本可能比较低,需要升级较高版本:
(1)首先,卸载git
yum remove -y git
yum 安装高版本Git:
Index of /centos/7/git/x86_64/
http://opensource.wandisco.com/centos/7/git/x86_64/
yum install http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm yum -y install git
(2) 然后 安装gitlab-runner的rpm安装包
rpm -ivh gitlab-runner_amd64.rpm
(3)更改Gitlab-Runner的配置
vim /etc/gitlab-runner/config.toml concurrent = 30 check_interval = 0 [session_server] session_timeout = 1800
(4)启动Gitlab-Runner
gitlab-runner start
然后就可以正常使用了!!!
推荐使用第二种rpm本地安装Gitlab-Runner!!!
三、Gitlab操作细节
1、新建群组
2、创建用户
3、以管理员身份为用户tlzs设置密码
4、将用户tlzs添加到某个组里
5、GitLab用户权限管理
GitLab用户在组中有五种权限:Guest、Reporter、Developer、Master、Owner。
Guest
可以创建issue、发表评论、不能读写版本库。
Reporter
可以克隆代码,不能提交,QA、PM可以赋予这个权限。
Developer
可以克隆代码、开发、提交、push、RD可以赋予这个权限。
Master
可以创建项目、添加 tag 、保护分支、添加项目成员、编辑项目、核心RD负责人可以赋予这个权限。
Owner
可以设置项目的访问权限-Visibility Level、删除项目、迁移项目、管理组成员、开发组leader可以赋予这个权限。
6、GitLab中的组和项目有三种访问权限
GitLab中的组和项目有三种访问权限:Private、Internal、Public。
private
只有组成员可以看到。
internal
只要登录的用户就能看到。
public
开源的所有的人都可以看到。
7、设置为开发者
8、为群组创建项目
9、clone项目
.gitlab-ci.yml:
stages: - stage1 - stage2 - stage3 stage1: before_script: - echo "hello,stage1---before_script" script: - echo "hello,stage1---script" after_script: - echo "hello,stage1---after_script" stage2: before_script: - echo "hello,stage2---before_script" script: - echo "hello,stage2---script" after_script: - echo "hello,stage2---after_script" stage3: before_script: - echo "hello,stage3---before_script" script: - echo "hello,stage3---script" after_script: - echo "hello,stage3---after_script"
10、为新项目配置gitlab-runner:
11、注册Runner信息到gitlab
docker ps -a
docker exec -it a80ec576152c gitlab-runner register
[root@localhost gitlab]# docker exec -it a80ec576152c gitlab-runner register Runtime platform arch=amd64 os=linux pid=713 revision=f761588f version=14.10.1 Running in system-mode. Enter the GitLab instance URL (for example, https://gitlab.com/): http://ljxwtl.cn:8888/ Enter the registration token: GR13489417-h8xcSZ6VHyYzLZknrw Enter a description for the runner: [a80ec576152c]: MyProject的Gitlab-Runner Enter tags for the runner (comma-separated): Java,IT,Golang,JavaScript Enter optional maintenance note for the runner: Registering runner... succeeded runner=GR13489417-h8xcSZ Enter an executor: shell, virtualbox, docker-ssh+machine, ssh, docker+machine, kubernetes, custom, docker, docker-ssh, parallels: shell Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
'New runner. Has not connected yet.' for any gitlab-runner newer than 11.0.2-1 (#3750) · Issues · GitLab.org / gitlab-runner · GitLab
Summary Cannot use freshly registered runners. They are not picking jobs. Immediately after running register command:
https://gitlab.com/gitlab-org/gitlab-runner/-/issues/3750
gitlab-runner register --non-interactive --executor 'shell' --url 'https://gitlab.com' --registration-token 'TOKEN'
docker exec -it a80ec576152c gitlab-runner register --non-interactive --executor 'shell' --url 'http://ljxwtl.cn:8888/' --registration-token 'GR13489417-h8xcSZ6VHyYzLZknrw'
以上方法解决不了的话,使用以下方案:
1、Runner未激活问题
有时候管理员注册共享runner后,查看面板上的Runner信息,可能会发现Runner处在未激活状态。
解决方法:
运行以下命令重新启动Runner。
gitlab-runner verify gitlab-runner restart
2、Job一直挂起,没有Runner来处理,原因:
1)首先考虑的是不是Runner没有激活
2)还可能是tags没有匹配到, Runner注册时是要填写绑定tags的,如果你在YML里面编写Job没有带上tags是不会有自定义Runner来处理。解决方法:给Job加tags。
3)最后一种可能:你连续注册了多个Runner,这些Runner冲突了,或者是新注册的Runner和旧Runner使用了同一个token,这时候的解决方法如下:
先删掉本地其他旧的Runner。
sudo gitlab-runner unregister --all-runners
然后重置Token,并使用更新后的Token重新注册一个Runner
(1)进入/data/soft/gitlab-runner目录,
(2)进入config目录,
(3)编辑config.toml文件
将concurrent = 1改成concurrent = 100
成功解决!!!
12、rpm安装的Gitlab-Runner注册Runner信息到gitlab
'New runner. Has not connected yet.' for any gitlab-runner newer than 11.0.2-1 (#3750) · Issues · GitLab.org / gitlab-runner · GitLab
Summary Cannot use freshly registered runners. They are not picking jobs. Immediately after running register command:
gitlab-runner register --non-interactive --executor 'shell' --url 'https://gitlab.com' --registration-token 'TOKEN'
四、项目CI/CD自动运行脚本
1、增加项目目录下的.gitlab-ci.yml文件
.gitlab-ci.yml:
stages: - stage1 - stage2 - stage3 stage1: before_script: - echo "hello,stage1---before_script" script: - echo "hello,stage1---script" after_script: - echo "hello,stage1---after_script" stage2: before_script: - echo "hello,stage2---before_script" script: - echo "hello,stage2---script" after_script: - echo "hello,stage2---after_script" stage3: before_script: - echo "hello,stage3---before_script" script: - echo "hello,stage3---script" after_script: - echo "hello,stage3---after_script"
2、提交代码到main主分支上
禁用共享Runner!!!
版权声明
本文仅代表作者观点,不代表码农殇立场。
本文系作者授权码农殇发表,未经许可,不得转载。