Gitlab CI/CD 實現(xiàn)前端自動化部署

簡介:gitlab-runner是一個開源項目,用于運行Pipeline作業(yè)并將結(jié)果發(fā)送回GitLab。它與GitLab CI一起使用,GitLab CI是GitLab隨附的開源持續(xù)集成服務(wù),用于協(xié)調(diào)作業(yè)。gitlab-runner是用Go編寫,可以作為單個二進(jìn)制文件運行,不需要語言特定的要求。

環(huán)境:centOS

1.在web服務(wù)器上安裝所需工具Nodejs,yarn,git,Gitlab Runner##

# 下載node包
wget https://nodejs.org/dist/v16.13.0/node-v16.13.0-linux-x64.tar.xz
# 解壓Node包
tar -xf node-v16.13.0-linux-x64.tar.xz
# 在配置文件(位置多在/etc/profile)末尾添加:
export PATH=$PATH:/root/node-v16.13.0-linux-x64/bin
# 刷新shell環(huán)境:
source /etc/profile
# 查看版本(輸出版本號則安裝成功):
node -v


npm i yarn -g
#or
npm i pnpm -g


# 利用yum安裝git
yum -y install git
# 查看git版本
git --version


# 安裝程序 gitlab-runner
wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

# 等待下載完成后分配權(quán)限
chmod +x /usr/local/bin/gitlab-runner

# 創(chuàng)建runner用戶
useradd --comment 'test' --create-home gitlab-runner --shell /bin/bash

# 安裝程序
gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner

# 啟動程序
gitlab-runner start

# 安裝完成后可使用gitlab-runner --version查看是否成功

2.在gitlab配置項目的Runner及CI/CD##

2.1配置Gitlab Runner
首先打開待添加自動部署功能的gitlab倉庫,在其中設(shè)置 > CI/CD > Runner中找到runner配置信息備用:


image.png

在web服務(wù)器中配置runner:

gitlab-runner register

>> Enter the GitLab instance URL (for example, https://gitlab.com/):
# 輸入剛才獲取到的gitlab倉庫地址
>> Enter the registration token:
# 輸入剛才獲取到的token
>> Enter a description for the runner:
# 自定runner描述
>> Enter tags for the runner (comma-separated):
# 自定runner標(biāo)簽
>> Enter an executor: docker-ssh, docker+machine, docker-ssh+machine, docker, parallels, shell, ssh, virtualbox, kubernetes, custom:
# 選擇執(zhí)行器,此處我們輸入shell

完整示例:


image.png

2.2配置.gitlab-ci.yml
.gitlab-ci.yml文件是流水線執(zhí)行的流程文件,Runner會據(jù)此完成規(guī)定的一系列流程。

我們在項目根目錄中創(chuàng)建.gitlab-ci.yml文件,然后在其中編寫內(nèi)容:

# 階段
stages:
  - install
  - build
  - deploy

cache:
  paths:
    - node_modules/

# 安裝依賴
install:
  stage: install
  # 此處的tags必須填入之前注冊時自定的tag
  tags: 
    - deploy
  # 規(guī)定僅在package.json提交時才觸發(fā)此階段
  only:
    changes:
      - package.json
  # 執(zhí)行腳本
  script:
    yarn

# 打包項目
build:
  stage: build 
  tags: 
    - deploy
  script: 
    - yarn build
  # 將此階段產(chǎn)物傳遞至下一階段 
  artifacts: 
    paths:
        - dist/

# 部署項目
deploy:
  stage: deploy
  tags: 
    - deploy
  script: 
    # 清空網(wǎng)站根目錄,目錄請根據(jù)服務(wù)器實際情況填寫
    - rm -rf /www/wwwroot/stjerne/salary/*
    # 復(fù)制打包后的文件至網(wǎng)站根目錄,目錄請根據(jù)服務(wù)器實際情況填寫
    - cp -rf ${CI_PROJECT_DIR}/dist/* /www/wwwroot/stjerne/salary/ 

保存并推送至gitlab后即可自動開始構(gòu)建部署。

構(gòu)建中可在gitlab CI/CD面板查看構(gòu)建進(jìn)程


image.png

3.運行時報錯##

運行時報類似:"bash: yarn: command not found" 的錯誤,其實是權(quán)限的問題,需要將默認(rèn)的gitlab-runner,改為root。


image.png

3.1查看gitlab-runner進(jìn)程

ps aux|grep gitlab-runner
image.png

3.2卸載gitlab-runner默認(rèn)用戶

gitlab-runner uninstall

3.3將用戶設(shè)置為root

gitlab-runner install --working-directory /home/gitlab-runner --user root

3.4重啟服務(wù)

systemctl restart gitlab-runner.service

3.5再次查看gitlab-runner進(jìn)程,確認(rèn)已修改為root

ps aux|grep gitlab-runner
image.png

4.常見問題
4.1“Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters”
確認(rèn)sshpass已安裝在gitlab服務(wù)器
4.2 gitlab項目gitlab-runner獲取不到到設(shè)置CI/CD-變量
建議關(guān)閉,只應(yīng)用于保護(hù)分支或標(biāo)簽

參考:
https://juejin.cn/post/7037022688493338661
https://blog.csdn.net/hzblucky1314/article/details/125966138
Gitlab CI:https://blog.csdn.net/weixin_38080573/category_8715427.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容