温馨提示×

centos gitlab CI/CD流程

小樊
57
2025-05-27 01:33:14
栏目: 智能运维

在CentOS系统上配置GitLab CI/CD流程通常涉及以下步骤:

1. 安装和配置GitLab Runner

  • 添加GitLab Runner的Yum源
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
  • 安装GitLab Runner
sudo yum install gitlab-ci-multi-runner -y
  • 配置并启动GitLab Runner
sudo vi /etc/systemd/system/gitlab-runner.service
  • 启动并启用GitLab Runner服务
sudo systemctl daemon-reload
sudo systemctl start gitlab-runner.service
sudo gitlab-runner install --user root
sudo gitlab-runner start
  • 注册GitLab Runner
gitlab-runner register --url https://gitlab.com --token your-token

2. 创建 .gitlab-ci.yml 文件

在项目的根目录下创建一个名为 .gitlab-ci.yml 的文件,用于定义CI/CD流程。以下是一个简单的示例:

stages:
  - build
  - test
  - deploy

build_job:
  stage: build
  script:
    - echo "Building the application..."
    - ./gradlew build
  artifacts:
    paths:
      - build/

test_job:
  stage: test
  script:
    - echo "Running tests..."
    - ./gradlew test

deploy_job:
  stage: deploy
  script:
    - echo "Deploying the application..."
    - scp -r /path/to/build user@server:/path/to/deploy
  only:
    - master

3. 配置GitLab Runner

在GitLab项目的Settings CI/CD Runners中,确保已经注册并配置了Runner。你可以选择使用Docker镜像来运行Runner。

4. 触发CI/CD流水线

每当你向Git仓库推送代码时,GitLab Runner将自动执行 .gitlab-ci.yml 文件中定义的流水线。你可以在GitLab的CI/CD页面查看流水线的状态和日志。

5. 高级配置

  • 使用模板库:为了复用常见的CI/CD模板,可以创建一个模板库,并在需要时引用这些模板。
  • 环境变量:在 .gitlab-ci.yml 文件中使用变量来提高灵活性和安全性。
  • 依赖管理:确保在 .gitlab-ci.yml 文件中正确管理项目的依赖,例如通过Maven或npm缓存来加速构建过程。

通过以上步骤,你可以在CentOS上成功设置GitLab的CI/CD流程,实现代码的自动化构建、测试和部署。

0