1. GitLab CI概述与环境规划
GitLab CI是GitLab集成的持续集成/持续部署(CI/CD)工具,提供了自动化构建、测试和部署的能力。GitLab CI基于GitLab Runner,支持多种执行环境和构建策略。更多学习教程www.fgedu.net.cn
1.1 GitLab版本说明
GitLab目前主要版本为16.x系列,本教程以GitLab 16.10.0为例进行详细讲解。GitLab 16.x版本相比之前版本在性能、稳定性和功能方面都有显著提升,支持更多的CI/CD特性。
$ gitlab-rake gitlab:env:info
System information
System: Oracle Linux 8.9
Current User: git
Using RVM: no
Ruby Version: 2.7.8p225
Gem Version: 3.1.6
Bundler Version:2.3.26
Rake Version: 13.0.6
Redis Version: 7.0.15
PostgreSQL Version: 14.8
GitLab Version: 16.10.0
GitLab Shell Version: 14.25.0
GitLab Workhorse Version: v16.10.0
GitLab API Version: v4
Ruby Version: 2.7.8p225
Rails Version: 6.1.7.7
PostgreSQL Version: 14.8
# 查看系统版本
$ cat /etc/os-release
NAME=”Oracle Linux Server”
VERSION=”8.9″
ID=”ol”
PRETTY_NAME=”Oracle Linux Server 8.9″
# 查看内核版本
$ uname -r
5.4.17-2136.302.7.2.el8uek.x86_64
1.2 环境规划
本次安装环境规划如下:
gitlab.fgedu.net.cn (192.168.1.81) – GitLab主机
GitLab版本:16.10.0
操作系统:Oracle Linux 8.9
安装目录:/opt/gitlab
数据目录:/var/opt/gitlab
端口:80, 443, 22
2. 硬件环境要求
GitLab作为代码仓库和CI/CD平台,对硬件资源要求根据代码库大小和构建任务的复杂度而定。学习交流加群风哥微信: itpux-com
2.1 物理主机环境要求
# free -h
total used free shared buff/cache available
Mem: 32G 4.2G 26G 256M 1.8G 27G
Swap: 8G 0B 8G
# 检查磁盘空间
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 12G 39G 24% /
/dev/sdb1 500G 50G 451G 10% /var/opt/gitlab
/dev/sdc1 200G 20G 181G 10% /backup
# 检查CPU核心数
# nproc
8
# 检查系统架构
# uname -m
x86_64
# 检查内核版本
# uname -r
5.4.17-2136.302.7.2.el8uek.x86_64
2.2 vSphere虚拟主机环境要求
– vCPU:4核
– 内存:16GB
– 磁盘:系统盘50GB + 数据盘300GB
– 网络:VMXNET3网卡,千兆网络
– 存储:建议使用SSD存储以提高I/O性能
资源池配置:
– CPU预留:2GHz
– 内存预留:8GB
– 内存限制:16GB
– CPU份额:正常
– 内存份额:正常
2.3 云平台主机环境要求
– 实例规格:ecs.g6.xlarge或同等规格
– vCPU:4核
– 内存:16GB
– 系统盘:高效云盘 100GB
– 数据盘:SSD云盘 300GB
– 网络带宽:5Mbps以上
存储配置:
– OSS对象存储:用于存储构建产物
– NAS文件存储:用于共享构建资源
– 云盘快照:定期备份GitLab数据
3. 操作系统环境准备
在安装GitLab之前,需要对操作系统进行必要的配置和优化。
3.1 操作系统版本检查
# cat /etc/os-release
NAME=”Oracle Linux Server”
VERSION=”8.9″
ID=”ol”
PRETTY_NAME=”Oracle Linux Server 8.9″
# 检查内核版本
# uname -r
5.4.17-2136.302.7.2.el8uek.x86_64
# 检查SELinux状态
# getenforce
Enforcing
# 检查防火墙状态
# systemctl status firewalld
● firewalld.service – firewalld – dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running)
3.2 依赖服务安装
# yum install -y curl policycoreutils-python-utils openssh-server openssh-clients postfix
# 启动并启用Postfix
# systemctl start postfix
# systemctl enable postfix
# 验证依赖安装
# rpm -qa | grep -E “curl|policycoreutils|openssh|postfix”
curl-7.88.1-10.el9_2.2.x86_64
policycoreutils-3.3-11.el9.x86_64
policycoreutils-python-utils-3.3-11.el9.noarch
openssh-server-8.7p1-21.el9_2.x86_64
openssh-clients-8.7p1-21.el9_2.x86_64
postfix-3.5.8-4.el9.x86_64
3.3 防火墙配置
# firewall-cmd –permanent –add-service=http
# firewall-cmd –permanent –add-service=https
# firewall-cmd –permanent –add-port=22/tcp
# firewall-cmd –reload
# 验证防火墙规则
# firewall-cmd –list-all
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh dhcpv6-client http https
ports:
protocols:
forward: yes
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
4. GitLab CI安装配置
完成环境准备后,开始安装GitLab。
4.1 安装GitLab
# curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | sudo bash
# 安装GitLab
# EXTERNAL_URL=”https://gitlab.fgedu.net.cn” yum install -y gitlab-ee
# 配置GitLab
# gitlab-ctl reconfigure
# 验证安装
# gitlab-ctl status
run: alertmanager: (pid 1234) 1234s; run: log: (pid 5678) 1234s
run: gitaly: (pid 9012) 1234s; run: log: (pid 3456) 1234s
run: gitlab-exporter: (pid 7890) 1234s; run: log: (pid 1234) 1234s
run: gitlab-workhorse: (pid 5678) 1234s; run: log: (pid 9012) 1234s
run: grafana: (pid 3456) 1234s; run: log: (pid 7890) 1234s
run: logrotate: (pid 1234) 1234s; run: log: (pid 5678) 1234s
run: nginx: (pid 9012) 1234s; run: log: (pid 3456) 1234s
run: node-exporter: (pid 7890) 1234s; run: log: (pid 1234) 1234s
run: postgres-exporter: (pid 5678) 1234s; run: log: (pid 9012) 1234s
run: postgresql: (pid 3456) 1234s; run: log: (pid 7890) 1234s
run: prometheus: (pid 1234) 1234s; run: log: (pid 5678) 1234s
run: redis: (pid 9012) 1234s; run: log: (pid 3456) 1234s
run: redis-exporter: (pid 7890) 1234s; run: log: (pid 1234) 1234s
run: sidekiq: (pid 5678) 1234s; run: log: (pid 9012) 1234s
run: unicorn: (pid 3456) 1234s; run: log: (pid 7890) 1234s
4.2 初始化GitLab
# cat /etc/gitlab/initial_root_password
Password: 1234567890abcdef1234567890abcdef
# 访问GitLab Web UI
# 打开浏览器访问 https://gitlab.fgedu.net.cn
# 输入初始root密码
# 更改root密码
# 配置GitLab设置
# 完成初始化
4.3 安装GitLab Runner
# curl -L –output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
# 添加执行权限
# chmod +x /usr/local/bin/gitlab-runner
# 创建GitLab Runner用户
# useradd –comment ‘GitLab Runner’ –create-home gitlab-runner –shell /bin/bash
# 安装GitLab Runner
# gitlab-runner install –user=gitlab-runner –working-directory=/home/gitlab-runner
# 启动GitLab Runner
# gitlab-runner start
# 验证安装
# gitlab-runner status
gitlab-runner: Service is running!
5. GitLab CI配置优化
为了提高GitLab CI的性能和稳定性,需要进行一些配置优化。
5.1 GitLab配置优化
# vi /etc/gitlab/gitlab.rb
# 内存配置
unicorn[‘worker_processes’] = 4
unicorn[‘worker_timeout’] = 60
# 数据库配置
postgresql[‘shared_buffers’] = “256MB”
postgresql[‘work_mem’] = “16MB”
# 缓存配置
gitlab_rails[‘artifacts_enabled’] = true
gitlab_rails[‘artifacts_path’] = “/var/opt/gitlab/gitlab-rails/shared/artifacts”
# 重新配置GitLab
# gitlab-ctl reconfigure
# 验证配置
# gitlab-ctl status
5.2 GitLab Runner配置优化
# vi /etc/gitlab-runner/config.toml
concurrent = 4
scheck_interval = 0
[[runners]]
name = “gitlab-runner”
url = “https://gitlab.fgedu.net.cn/”
token = “1234567890abcdef1234567890abcdef”
executor = “shell”
[runners.cache]
[runners.cache.s3]
[runners.cache.gcs]
# 重启GitLab Runner
# gitlab-runner restart
# 验证配置
# gitlab-runner status
6. GitLab CI流水线配置
GitLab CI通过.gitlab-ci.yml文件定义流水线,支持多种构建策略和阶段。
6.1 基本流水线配置
# vi .gitlab-ci.yml
stages:
– build
– test
– deploy
build:
stage: build
script:
– echo “Building application…”
– mvn clean package -DskipTests
artifacts:
paths:
– target/*.jar
test:
stage: test
script:
– echo “Running tests…”
– mvn test
deploy:
stage: deploy
script:
– echo “Deploying application…”
environment:
name: production
only:
– main
6.2 高级流水线配置
stages:
– build
– test
– deploy
variables:
MAVEN_OPTS: “-Dmaven.repo.local=.m2/repository”
cache:
paths:
– .m2/repository/
build:
stage: build
script:
– mvn clean package -DskipTests
artifacts:
paths:
– target/*.jar
tags:
– maven
test:
stage: test
script:
– mvn test
tags:
– maven
dependencies:
– build
deploy:
stage: deploy
script:
– echo “Deploying to production…”
– scp target/*.jar user@server:/path/to/deploy
environment:
name: production
only:
– main
tags:
– deploy
6.3 流水线变量配置
# 在GitLab Web UI中配置
# 1. 进入项目 -> 设置 -> CI/CD -> 变量
# 2. 添加变量
# 使用变量
variables:
DATABASE_URL: “${DATABASE_URL}”
API_KEY: “${API_KEY}”
build:
script:
– echo “Database URL: ${DATABASE_URL}”
– echo “API Key: ${API_KEY}”
7. GitLab Runner配置
GitLab Runner是GitLab CI的执行器,负责运行构建任务。
7.1 注册GitLab Runner
# gitlab-runner register
# 输入GitLab URL: https://gitlab.fgedu.net.cn/
# 输入注册令牌: 1234567890abcdef1234567890abcdef
# 输入Runner描述: gitlab-runner
# 输入Runner标签: maven, docker
# 输入执行器: shell
# 验证注册
# gitlab-runner list
7.2 配置GitLab Runner
# vi /etc/gitlab-runner/config.toml
concurrent = 4
scheck_interval = 0
[[runners]]
name = “gitlab-runner”
url = “https://gitlab.fgedu.net.cn/”
token = “1234567890abcdef1234567890abcdef”
executor = “shell”
[runners.cache]
[runners.cache.s3]
[runners.cache.gcs]
# 重启GitLab Runner
# gitlab-runner restart
# 验证配置
# gitlab-runner status
7.3 GitLab Runner类型
[[runners]]
executor = “shell”
# Docker执行器
[[runners]]
executor = “docker”
[runners.docker]
image = “maven:3.8.8”
privileged = false
volumes = [“/cache”]
# Docker Machine执行器
[[runners]]
executor = “docker+machine”
[runners.docker]
image = “maven:3.8.8”
[runners.machine]
MachineDriver = “digitalocean”
# Kubernetes执行器
[[runners]]
executor = “kubernetes”
[runners.kubernetes]
image = “maven:3.8.8”
8. GitLab CI安全配置
GitLab CI提供了多种安全功能,包括密钥管理、访问控制、安全扫描等。
8.1 密钥管理
# 在GitLab Web UI中配置
# 1. 进入项目 -> 设置 -> CI/CD -> 变量
# 2. 添加变量
# 使用变量
variables:
DATABASE_PASSWORD: “${DATABASE_PASSWORD}”
API_KEY: “${API_KEY}”
# 保护变量
# 在GitLab Web UI中设置变量为”受保护”
8.2 访问控制
# 在GitLab Web UI中配置
# 1. 进入项目 -> 设置 -> 成员
# 2. 配置成员权限
# 配置分支保护
# 1. 进入项目 -> 设置 -> 仓库 -> 分支保护
# 2. 配置分支保护规则
8.3 安全扫描
# 在GitLab Web UI中配置
# 1. 进入项目 -> 安全与合规 -> 配置
# 2. 启用安全扫描
# 安全扫描示例
include:
– template: Security/SAST.gitlab-ci.yml
– template: Security/Secret-Detection.gitlab-ci.yml
– template: Security/Dependency-Scanning.gitlab-ci.yml
9. GitLab CI性能优化
在生产环境中,需要对GitLab CI进行性能优化以提高构建效率。from:www.itpux.com
9.1 内存配置优化
# vi /etc/gitlab/gitlab.rb
# 内存配置
unicorn[‘worker_processes’] = 4
unicorn[‘worker_timeout’] = 60
unicorn[‘worker_memory_limit_min’] = “400M”
unicorn[‘worker_memory_limit_max’] = “800M”
# 重新配置GitLab
# gitlab-ctl reconfigure
# 验证配置
# gitlab-ctl status
9.2 缓存配置
# 在.gitlab-ci.yml中配置
cache:
paths:
– .m2/repository/
– node_modules/
key: ${CI_COMMIT_REF_SLUG}
# 配置分布式缓存
# 编辑GitLab Runner配置
# vi /etc/gitlab-runner/config.toml
[[runners]]
[runners.cache]
Type = “s3”
Path = “gitlab-runner”
Shared = true
[runners.cache.s3]
ServerAddress = “s3.amazonaws.com”
BucketName = “gitlab-runner-cache”
BucketLocation = “us-east-1”
AccessKey = “AKIA…”
SecretKey = “secret…”
9.3 并行构建
# 在.gitlab-ci.yml中配置
build:
stage: build
script:
– mvn clean package -DskipTests
parallel:
matrix:
– OS: [ubuntu, centos]
JAVA_VERSION: [11, 17]
# 配置并发数
# 编辑GitLab Runner配置
# vi /etc/gitlab-runner/config.toml
concurrent = 8
10. GitLab CI升级迁移
本节介绍GitLab CI的版本升级和数据迁移方法。
10.1 GitLab版本升级
# gitlab-rake gitlab:backup:create
# 升级GitLab
# yum update gitlab-ee
# 重新配置GitLab
# gitlab-ctl reconfigure
# 验证升级
# gitlab-rake gitlab:env:info
# 访问GitLab Web UI
# 打开浏览器访问 https://gitlab.fgedu.net.cn
10.2 GitLab配置迁移
# gitlab-ctl backup-etc
# 在新服务器上导入配置
# scp /etc/gitlab/config_backup/gitlab_config_1234567890.tar /etc/gitlab/
# gitlab-ctl restore-etc /etc/gitlab/gitlab_config_1234567890.tar
# 验证配置
# gitlab-ctl status
11. GitLab CI备份恢复
本节介绍GitLab CI的备份和恢复方法。
11.1 GitLab备份
# gitlab-rake gitlab:backup:create
# 备份GitLab配置
# gitlab-ctl backup-etc
# 验证备份
# ls -la /var/opt/gitlab/backups/
11.2 GitLab恢复
# gitlab-ctl stop unicorn
# gitlab-ctl stop sidekiq
# 恢复GitLab数据
# gitlab-rake gitlab:backup:restore BACKUP=1234567890_2024_04_05_16.10.0
# 恢复GitLab配置
# gitlab-ctl restore-etc /etc/gitlab/gitlab_config_1234567890.tar
# 启动GitLab服务
# gitlab-ctl start
# 验证恢复
# gitlab-ctl status
# 打开浏览器访问 https://gitlab.fgedu.net.cn
11.3 GitLab监控脚本
# vi /data/gitlab/scripts/gitlab_monitor.sh
#!/bin/bash
LOG_FILE=”/var/log/gitlab_monitor.log”
ALERT_EMAIL=”admin@fgedu.net.cn”
check_gitlab_status() {
echo “$(date): Checking gitlab status…” >> $LOG_FILE
status=$(gitlab-ctl status | grep -o “run:” | wc -l)
if [ “$status” -gt 10 ]; then
echo “$(date): GitLab status: OK” >> $LOG_FILE
else
echo “$(date): GitLab status: FAILED” >> $LOG_FILE
echo “GitLab service failed” | mail -s “GitLab Alert” $ALERT_EMAIL
fi
}
check_gitlab_web() {
echo “$(date): Checking gitlab web…” >> $LOG_FILE
status=$(curl -s -o /dev/null -w “%{http_code}” https://gitlab.fgedu.net.cn)
if [ “$status” = “200” ]; then
echo “$(date): GitLab web: OK” >> $LOG_FILE
else
echo “$(date): GitLab web: FAILED” >> $LOG_FILE
echo “GitLab web failed” | mail -s “GitLab Alert” $ALERT_EMAIL
fi
}
check_disk_usage() {
echo “$(date): Checking disk usage…” >> $LOG_FILE
usage=$(df -h /var/opt/gitlab | awk ‘NR==2 {print $5}’ | sed ‘s/%//’)
echo “$(date): Disk usage: $usage%” >> $LOG_FILE
if [ “$usage” -gt “80” ]; then
echo “$(date): Disk usage too high: $usage%” >> $LOG_FILE
echo “Disk usage too high: $usage%” | mail -s “GitLab Alert” $ALERT_EMAIL
fi
}
main() {
check_gitlab_status
check_gitlab_web
check_disk_usage
}
main
# 添加执行权限
# chmod +x /data/gitlab/scripts/gitlab_monitor.sh
# 添加定时任务
# crontab -e
*/15 * * * * /data/gitlab/scripts/gitlab_monitor.sh
通过以上步骤,GitLab CI安装配置、性能优化、升级迁移、备份恢复等内容已全部完成。GitLab CI作为CI/CD工具,能够高效地自动化构建、测试和部署流程,是DevOps实践的重要组成部分。
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
