在CentOS上使用GitLab进行CI/CD实践涉及多个步骤,包括安装GitLab、配置CI/CD流水线、以及运行CI/CD流程。以下是详细的步骤指南:
更新系统:
sudo yum update -y
安装必要的依赖包:
sudo yum install -y curl openssh-server openssh-clients postfix policycoreutils-python perl git nodejs rsync cronie
配置防火墙:
sudo systemctl enable sshd
sudo systemctl start sshd
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --permanent --add-service=http
sudo systemctl reload firewalld
设置时区(如果需要):
sudo timedatectl set-timezone Asia/Shanghai
添加GitLab的软件源:
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
安装GitLab CE:
EXTERNAL_URL="http://your_server_ip" sudo yum install -y gitlab-ce
配置GitLab:
编辑 /etc/gitlab/gitlab.rb 文件,设置外部URL和其他配置,例如SMTP邮件服务器。
sudo vi /etc/gitlab/gitlab.rb
重新配置并重启GitLab:
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
安装Docker:
sudo yum install -y docker
sudo systemctl start docker
sudo systemctl enable docker
添加GitLab Docker仓库:
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
安装GitLab容器注册表:
EXTERNAL_URL="http://your-server-ip" sudo yum install gitlab-ce
启动GitLab容器:
sudo gitlab-ctl start
访问GitLab:
打开浏览器,访问 http://your-server-ip,你应该能够看到GitLab的登录页面。
创建 .gitlab-ci.yml 文件:
在项目根目录下创建 .gitlab-ci.yml 文件,定义CI/CD流程。以下是一个简单的示例:
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Building."
artifacts:
paths:
- build/
test_job:
stage: test
script:
- echo "Testing."
dependencies:
- build_job
deploy_job:
stage: deploy
script:
- echo "Deploying."
dependencies:
- test_job
提交并运行流水线: 使用以下命令提交更改并触发流水线:
git add .
git commit -m 'Initial CI/CD configuration'
git push --set-upstream origin main
glab ci status
在GitLab仓库中,每当有代码提交时,GitLab CI/CD会自动运行 .gitlab-ci.yml 中定义的流程。开发人员可以在GitLab的CI/CD页面查看流程的运行状态和日志。
通过以上步骤,你可以在CentOS上成功安装和配置GitLab,并实现CI/CD实践。