伺服器配置gitlab-runner流程問題總結

songxianling1992發表於2021-12-22
目的:擺脫Jenkins部署;如果前端有自己的伺服器環境;可以藉助gitlab-runner實現CI自動部署並且傳送結果通知訊息

1. 伺服器安裝gitlab-runner

先確認伺服器環境資訊之後找到對應的gitlab-runner安裝包
  1. 使用uname -a 檢視伺服器版本資訊
    image.png
  2. 在此處找到符合自己版本資訊的安裝包
  3. 使用安裝包安裝gitlab-runner程式;如果提示sudo: curl:找不到命令, 則使用yum安裝curl後重新安裝程式yum update -y && yum install curl -y,(因為安裝curl之前會先進行yum的update所以這個時間挺長的)

    sudo curl -L --output /usr/local/bin/gitlab-runner "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64"
  4. 給定執行許可權

    sudo chmod +x /usr/local/bin/gitlab-runner
  5. 建立一個使用者

    sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
  6. 安裝執行服務

    sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
    sudo gitlab-runner start

    如果這一步也提示sudo: gitlab-runner:找不到命令;需要

    # 修改/etc/sudoers檔案;找到這一行
    Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin  
    # 將要執行的命令所在的目錄新增到後面,即可
    Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin 

    重新執行安裝執行服務就可以繼續了

  7. 驗證安裝資訊gitlab-runner -v
    image.png

2. 配置註冊Runner

  1. 在自己gitlab倉庫找到對應的資訊 > Settings -> CI/CD -> Runners -> Specific Runners
    image.png
  2. 在伺服器下執行註冊命令sudo gitlab-runner register
    image.png
  3. 檢視正在啟用的Runner gitlab-runner verify
    image.png
  4. 也可以在CI處看到已經被啟用
    image.png

3. 編寫yml配置檔案

在自己專案的根路徑下編寫.gitlab-ci.yml指令碼檔案
image.png
編寫符合自己的程式

stages: # 分階段
  - install
  - build
  - deploy
  - notify

cache: # 快取內容
  paths:
    - node_modules/
    - dist/

install-job:
  tags: # 觸發runner(這個就是註冊的時候填寫的tag)
    - master
  only: # 哪些分支觸發
    - master
  stage: install # 當前階段
  script:
    - echo '安裝依賴階段....'
    - npm install

build-job:
  tags:
    - master
  only:
    - master
  stage: build
  script:
    - npm run build:dev

deploy-job:
  tags:
    - master
  only:
    - master
  stage: deploy
  script:
    - whoami
    - sudo scp -r ./dist/* /www/html/kangshifu/kangshifu-vue/mars/dist


# 構建成功時的通知訊息
notifySuccessWeChat:
  stage: notify
  script:
    - echo $title $sucContent
    - curl 'http://pushplus.hxtrip.com/send' -H 'Content-Type:application/json' -d "{\"token\":\"130a550e3a6d4dd2a019a98a5c022900\",\"title\":\"康師傅_前端_智雲系統-部署結果(ci)\",\"content\":\"成功\",\"template\":\"html\",\"topic\":\"kangshifuFeZhiyun\"}"
  only:
    - master
  tags:
    - master
  when: on_success    

# 構建失敗時的通知訊息
notifyFailWeChat:
  stage: notify
  script:
    - curl 'http://pushplus.hxtrip.com/send' -H 'Content-Type:application/json' -d "{\"token\":\"130a550e3a6d4dd2a019a98a5c022900\",\"title\":\"康師傅_前端_智雲系統-部署結果(ci)\",\"content\":\"失敗\",\"template\":\"html\",\"topic\":\"kangshifuFeZhiyun\"}"
  only:
    - master
  tags:
    - master
  when: on_failure  
此指令碼中包含了部署結果的通知;具體配置方式可以檢視對應的文件進行配置,也可以使用釘釘訊息或者企業微信訊息等方式

此處可能遇到的問題

  1. git版本問題

    報錯:
    Getting source from Git repository
    00:01
    Fetching changes with git depth set to 50...
    Reinitialized existing Git repository in /home/gitlab-runner/builds/ErNGnaLr/0/ivm/mars/.git/
    fatal: git fetch-pack: expected shallow list
    fatal: The remote end hung up unexpectedly
    Cleaning up project directory and file based variables
    00:00
    ERROR: Job failed: exit status 1
    
    原因:
    centos7 基礎倉庫,提供的 git 版本只有到 1.8.3,沒辦法使用 git 2 的一些新功能
    
    解決方法:
    安裝最新版本的git就可以了
  2. 拷貝檔案問題
    因為沒有使用sshpass 的方式操作檔案;當前使用的scp 進行檔案的操作;可能會提示

    報錯: 
    We trust you have received the usual lecture from the local System
    Administrator. It usually boils down to these three things:
     #1) Respect the privacy of others.
     #2) Think before you type.
     #3) With great power comes great responsibility.
    sudo: no tty present and no askpass program specified
    Cleaning up project directory and file based variables
    00:00
    ERROR: Job failed: exit status 1
    
    原因:
    由於嘗試使用sudo執行命令引起的 ,但是呼叫使用者沒有被授權使用 sudo
    
    解決:
    開啟 sudoers 檔案`vim /etc/sudoers` ;將以下內容新增到檔案底部
    gitlab-runner ALL=(ALL) NOPASSWD: ALL

至此gitlab-runner已經可以成功的執行了「手動撒花?」

相關文章