centos

GitLab在CentOS上的CI/CD如何配置

小樊
47
2025-08-26 17:43:00
栏目: 智能运维

在CentOS上配置GitLab CI/CD,可按以下步骤进行:

安装GitLab

添加GitLab仓库:curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash。安装GitLab CE:sudo yum install -y gitlab-ce。启动并启用GitLab服务:sudo systemctl start gitlabsudo systemctl enable gitlab。访问http://<your_server_ip>完成初始设置。

配置GitLab Runner

安装Runner:curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bashsudo yum install gitlab-runner。注册Runner:在GitLab项目页面的Settings -> CI/CD -> Runners中,点击New Runner,按提示输入URL、注册令牌等信息,选择执行器类型(如Shell或Docker)。以Shell执行器为例,注册命令为sudo gitlab-runner register --non-interactive --url "http://your_gitlab_instance" --registration-token "your_token" --executor "shell" --description "your_runner_description"

创建.gitlab-ci.yml文件

在项目根目录下创建该文件,定义CI/CD流程。例如:

stages:
  - build
  - test
  - deploy
build_job:
  stage: build
  script:
    - echo "Building the project..."
test_job:
  stage: test
  script:
    - echo "Running tests..."
deploy_job:
  stage: deploy
  script:
    - echo "Deploying the project..."
  only:
    - master

提交代码触发流程

.gitlab-ci.yml文件提交到GitLab仓库并推送,GitLab Runner会自动检测并执行配置的流水线任务。可在GitLab的CI/CD -> Pipelines页面查看执行状态和日志。

0
看了该问题的人还看了