溫馨提示×

GitLab CI/CD在Linux環境下如何搭建

小樊
59
2025-04-15 18:12:14
欄目: 智能運維

在Linux環境下搭建GitLab CI/CD涉及幾個主要步驟,包括安裝GitLab、配置GitLab Runner以及設置.gitlab-ci.yml文件。以下是詳細的步驟指南:

安裝GitLab

CentOS 7

  1. 下載GitLab CE安裝包

    curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
    
  2. 安裝依賴包

    sudo yum install -y curl policycoreutils-python openssh-serversudo systemctl enable sshdsudo systemctl start sshdsudo firewall-cmd --permanent --add-service=httpsudo systemctl reload firewalldsudo yum install postfixsudo systemctl enable postfixsudo systemctl start postfix
    
  3. 配置外部URL

    curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
    sudo EXTERNAL_URL="http://gitlab.example.com" yum install -y gitlab-ce
    
  4. 重啟服務

    sudo gitlab-ctl reconfigure
    sudo gitlab-ctl start
    

CentOS 6

  1. 下載GitLab CE安裝包

    curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
    
  2. 安裝依賴包

    sudo yum install -y curl policycoreutils-python openssh-server cronieyum install lokkitsudo lokkit -s http -s sshsudo yum install postfixsudo service postfix startsudo chkconfig postfix on
    
  3. 配置外部URL

    curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
    sudo EXTERNAL_URL="http://gitlab.example.com" yum -y install gitlab-ce
    
  4. 重啟服務

    sudo gitlab-ctl reconfigure
    sudo gitlab-ctl start
    

配置GitLab Runner

  1. 下載并安裝GitLab Runner

    curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
    sudo apt-get install gitlab-runner
    
  2. 注冊Runner

    sudo gitlab-runner register
    

設置.gitlab-ci.yml文件

在項目根目錄下創建.gitlab-ci.yml文件,定義CI/CD流程:

stages:
  - build
  - test
  - deploy

build-job:
  stage: build
  script:
    - echo "編譯構建中..."
    - mkdir -p build
    - touch build/info.txt

test-job:
  stage: test
  script:
    - echo "測試運行中..."
    - test -f build/info.txt

deploy-job:
  stage: deploy
  script:
    - echo "部署應用中..."
  only:
    - master

使用Docker優化CI環境

GitLab CI可以使用Docker容器來運行任務,這樣可以避免為每個項目配置不同的環境:

test:
  image: node:14
  services:
    - postgres:13
  script:
    - npm test

防火墻配置

確保開放必要的端口,如SSH(默認22)、HTTP(默認80)和HTTPS(默認443):

sudo firewall-cmd --add-service=ssh --permanent
sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --reload

通過以上步驟,你可以在Linux環境下成功搭建GitLab CI/CD環境。初次登錄時需要為GitLab的root用戶設置密碼。

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