一、GitLab安装配置
1. 系统准备
bash
# 安装依赖
apt update
apt install -y curl openssh-server ca-certificates postfix
# 添加GitLab源
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | bash
# 安装GitLab
apt install gitlab-ce
2. 初始配置
ruby
# /etc/gitlab/gitlab.rb
external_url 'http://gitlab.example.com'
gitlab_rails['gitlab_shell_ssh_port']=22
gitlab_rails['time_zone']='Asia/Shanghai'
# 应用配置
gitlab-ctl reconfigure
二、Runner安装配置
1. Runner安装
bash
# 添加Runner源
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh"| bash
# 安装Runner
apt install gitlab-runner
# 启动服务
systemctl enable gitlab-runner
systemctl start gitlab-runner
2. Runner注册
bash
# 注册Runner
gitlab-runner register \
--url "https://gitlab.example.com/" \
--registration-token "PROJECT_REGISTRATION_TOKEN" \
--description "docker-runner" \
--executor "docker" \
--docker-image "docker:latest"
三、CI/CD配置
1. GitLab CI配置
yaml
# .gitlab-ci.yml
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- mvn clean package
artifacts:
paths:
- target/*.jar
test_job:
stage: test
script:
- mvn test
deploy_job:
stage: deploy
script:
- bash deploy.sh
only:
- master
2. Runner执行器配置
toml
# config.toml
[[runners]]
name ="docker-runner"
url ="https://gitlab.example.com/"
token ="PROJECT_TOKEN"
executor ="docker"
[runners.docker]
tls_verify =false
image ="docker:latest"
privileged =true
disable_cache =false
volumes =["/cache"]
四、环境变量与缓存
1. 变量配置
yaml
variables:
MAVEN_OPTS:"-Dmaven.repo.local=.m2/repository"
DOCKER_HOST:"tcp://docker:2375"
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
-.m2/repository/
- node_modules/
2. Docker缓存
yaml
build:
stage: build
script:
- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
cache:
paths:
-.docker
五、Pipeline优化
1. 并行任务
yaml
test_unit:
stage: test
parallel:3
script:
- mvn test -Dtest=TestClass
test_integration:
stage: test
script:
- mvn verify
2. 条件执行
yaml
deploy_prod:
stage: deploy
script:
- deploy_to_production
rules:
-if:'$CI_COMMIT_BRANCH == "master"'
when: manual
-when: never
六、监控与通知
1. 监控配置
ruby
# gitlab.rb
prometheus['enable']=true
grafana['enable']=true
# 应用配置
gitlab-ctl reconfigure
2. 通知配置
yaml
notification:
stage:.post
script:
-|
curl -X POST -H 'Content-Type: application/json' \
--data '{"text":"Pipeline finished"}' \
$WEBHOOK_URL
rules:
-when: always