温馨提示×

centos gitlab CI/CD实践

小樊
35
2025-02-26 19:45:14
栏目: 智能运维
Centos服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在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

安装GitLab

你可以通过多种方式在CentOS上安装GitLab,包括使用官方脚本和Docker。以下是使用官方脚本的方法:

  1. 添加GitLab软件包存储库:
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
  1. 安装GitLab CE:
sudo yum install -y gitlab-ce
  1. 启动GitLab并设置外部URL(例如:http://your_server_ip:8080):
sudo gitlab-ctl reconfigure

注册GitLab Runner

  1. 在GitLab项目中,进入Settings -> CI/CD -> Runners,点击“Register an instance runner”。

  2. 复制提供的Registration token。

  3. 在你的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文件

在项目的根目录下创建一个名为.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

触发CI/CD流水线

当你将代码推送到GitLab仓库时,GitLab Runner将自动执行.gitlab-ci.yml文件中定义的流水线。你可以在GitLab CI/CD页面上查看CI过程以及结果。

注意事项

  • 确保GitLab Runner已经正确注册并在线。
  • 根据需要调整.gitlab-ci.yml文件中的配置。
  • 如果遇到权限问题,可以尝试以root用户身份运行相关命令。

以上步骤应该可以帮助你在CentOS上成功实践GitLab CI/CD。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

0