centos

在CentOS上使用GitLab进行CI/CD实践

小樊
51
2025-06-22 09:36:08
栏目: 智能运维

在CentOS上使用GitLab进行CI/CD实践涉及多个步骤,包括安装GitLab、配置CI/CD流水线、以及运行CI/CD流程。以下是详细的步骤指南:

安装GitLab

方法一:使用官方GitLab仓库和YUM包管理器

  1. 更新系统

    sudo yum update -y
    
  2. 安装必要的依赖包

    sudo yum install -y curl openssh-server openssh-clients postfix policycoreutils-python perl git nodejs rsync cronie
    
  3. 配置防火墙

    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
    
  4. 设置时区(如果需要):

    sudo timedatectl set-timezone Asia/Shanghai
    
  5. 添加GitLab的软件源

    curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
    
  6. 安装GitLab CE

    EXTERNAL_URL="http://your_server_ip" sudo yum install -y gitlab-ce
    
  7. 配置GitLab: 编辑 /etc/gitlab/gitlab.rb 文件,设置外部URL和其他配置,例如SMTP邮件服务器。

    sudo vi /etc/gitlab/gitlab.rb
    
  8. 重新配置并重启GitLab

    sudo gitlab-ctl reconfigure
    sudo gitlab-ctl restart
    

方法二:使用Docker安装GitLab

  1. 安装Docker

    sudo yum install -y docker
    sudo systemctl start docker
    sudo systemctl enable docker
    
  2. 添加GitLab Docker仓库

    curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
    
  3. 安装GitLab容器注册表

    EXTERNAL_URL="http://your-server-ip" sudo yum install gitlab-ce
    
  4. 启动GitLab容器

    sudo gitlab-ctl start
    
  5. 访问GitLab: 打开浏览器,访问 http://your-server-ip,你应该能够看到GitLab的登录页面。

配置CI/CD流水线

  1. 创建 .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
    
  2. 提交并运行流水线: 使用以下命令提交更改并触发流水线:

    git add .
    git commit -m 'Initial CI/CD configuration'
    git push --set-upstream origin main
    glab ci status
    

运行CI/CD流程

在GitLab仓库中,每当有代码提交时,GitLab CI/CD会自动运行 .gitlab-ci.yml 中定义的流程。开发人员可以在GitLab的CI/CD页面查看流程的运行状态和日志。

最佳实践

通过以上步骤,你可以在CentOS上成功安装和配置GitLab,并实现CI/CD实践。

0
看了该问题的人还看了