溫馨提示×

centos gitlab CI/CD實踐

小樊
39
2025-02-26 19:45:14
欄目: 智能運維

在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。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女