在CentOS上实践GitLab CI/CD涉及多个步骤,包括安装必要的软件包、配置GitLab、注册GitLab Runner以及编写和触发CI/CD流水线。以下是一个详细的指南:
首先,确保你的CentOS服务器已经安装了必要的软件包。你可以使用以下命令来安装这些软件包:
sudo yum install -y curl policycoreutils openssh-server openssh-client postfix
然后,启动并设置SSH服务开机自启:
sudo systemctl enable sshd
sudo systemctl start sshd
接下来,配置防火墙以允许HTTP和HTTPS流量:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
你可以通过多种方式在CentOS上安装GitLab,包括使用官方脚本和Docker。以下是使用官方脚本的方法:
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
sudo yum install -y gitlab-ce
sudo gitlab-ctl reconfigure
在GitLab项目中,进入Settings -> CI/CD -> Runners,点击“Register an instance runner”。
复制提供的Registration token。
在你的CentOS服务器上,以root用户身份执行以下命令来注册Runner:
sudo gitlab-runner register --non-interactive --executor "docker" --docker-image alpine:latest --url "http://your_server_ip" --registration-token "your_registration_token" --description "docker-runner" --tag-list "newdocker" --run-untagged "true" --locked "false" --docker-volumes /var/run/docker.sock:/var/run/docker.sock --docker-privileged "true" --access-level "not_protected"
在项目的根目录下创建一个名为.gitlab-ci.yml
的文件,定义CI/CD流程。以下是一个简单的示例:
stages:
- build
- test
- deploy
build:
stage: build
script:
- echo "Building the application..."
artifacts:
paths:
- build/
test:
stage: test
script:
- echo "Running tests..."
artifacts:
paths:
- test/
deploy:
stage: deploy
script:
- echo "Deploying the application..."
only:
- master
当你将代码推送到GitLab仓库时,GitLab Runner将自动执行.gitlab-ci.yml
文件中定义的流水线。你可以在GitLab CI/CD页面上查看CI过程以及结果。
.gitlab-ci.yml
文件中的配置。以上步骤应该可以帮助你在CentOS上成功实践GitLab CI/CD。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>