最近開發(fā)vue項目時經(jīng)常需要打包部署到測試環(huán)境,甚至每天都要部署三四次,然后便想到了使用GITLAB-CI的方式做持續(xù)集成.一來不用每次手動打包了,二來也方便管理維護(題主本地開發(fā)系統(tǒng)為win10, 服務器系統(tǒng)為CentOS 7.6.1810)
gitlab CentOS版本 - 14.6.1
gitlab-runner CentOS版本 -> 15.5.0
- 服務器安裝git
- 服務器配置ssh免密登錄
- 服務器配置docker
- 服務器docker配置node鏡像
- 服務器安裝gitlab-runner
- 服務器注冊runner
- 配置gitlab-ci變量
- 編寫.gitlab-ci.yml腳本文件
一. 服務器安裝git
1. yum install http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm
2. yum -y install git
3. git version
二. 服務器配置ssh免密登錄
本地機打開git窗口后依次運行
1. 生成秘鑰對執(zhí)行 ssh-keygen -t rsa -C "username@163.com" -f C:/Users/Admin/.ssh/id_rsa_129 后直接一路回車就可以
2. 在 C:/Users/Admin/.ssh/config 文件來指定需要訪問服務器的用戶名, IP, 端口, 秘鑰(如果沒有config文件,需自己創(chuàng)建一個)
Host 192.168.159.129
Port 22
HostName 192.168.159.129
User xiaoheihei
IdentityFile ~/.ssh/id_rsa_129
3. 使用 ssh-copy-id 192.168.159.129 命令免密登錄服務器
ps: 為什么生成秘鑰對的時候要加郵箱和指定秘鑰名稱?
因為后續(xù)我們可能會有多個項目都需要使用CI來管理, 這樣可以方便區(qū)分
三. 服務器配置docker
1. 安裝 yum -y install docker
2. 運行 service docker start
3. 檢查是否啟動成功 docker ps
四. 服務器docker配置node鏡像
1. 拉取鏡像 docker pull node:16.17.0
2. 查看是否安裝成功 docker images
五. 服務器安裝gitlab-runner
下面為 root 用戶操作
1. curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
2. chmod +x /usr/local/bin/gitlab-runner
3. useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
4. gitlab-runner install --user=xiaoheihei --working-directory=/home/gitlab-runner
5. systemctl start gitlab-runner
六. 服務器注冊runner
url 和 token 在 gitlab當前項目 -> settings -> CI/CD -> Runners 下
1. Enter the GitLab instance URL (for example, https://gitlab.com/)
- http://192.168.21.26/
2. Enter the registration token:
- iF3yjzXgxjD8p6xt81oJ
3. Enter a description for the runner:
- 描述
4. Enter tags for the runner (comma-separated):
- test
ps: tag非常重要,.gitlab-ci.yml腳本的執(zhí)行就是根據(jù)tag判斷的
5. Enter optional maintenance note for the runner:
- 隨意
6. Enter an executor: instance, kubernetes, docker, docker-ssh, parallels, virtualbox, docker-ssh+machine, custom, shell, ssh, docker+machine:
- docker
7. Enter the default Docker image (for example, ruby:2.7):
- node:16.17.0
ps: 之前 docker 安裝的 node 鏡像
完整步驟如下:
七. 配置gitlab-ci變量
配置變量的位置在 gitlab當前項目 -> settings -> CI/CD -> Variables 下
依次創(chuàng)建變量
ENV_129_SSH_PRIVATE_KEY 值為 C:/Users/Admin/.ssh/id_rsa_129 文件內(nèi)容
ENV_129_SSH_CONFIG 值為 C:/Users/Admin/.ssh/config 文件內(nèi)容
ENV_129_SSH_KNOWN_HOST 值為 C:/Users/Admin/.ssh/known_hosts 文件內(nèi)容
ENV_129_USERNAME 值為 服務器用戶名(eg: xiaoheihei)
ENV_129_HOST 值為 服務器IP(eg: 192.168.159.129)
ENV_129_PATH 值為 ~/html/
八. 編寫.gitlab-ci.yml腳本文件
image: node:16.17.0 # 使用 docker node:16.17.0鏡像
stages: # pipeline 執(zhí)行步驟
- build_129
- deploy_129
cache: # 緩存
paths:
- node_modules
- dist
build_129:job: # 執(zhí)行 build_129 階段
tags:
- "test" # 對應服務器注冊 runner 時輸入的 tag
only:
- main # 僅在main分支有提交代碼時執(zhí)行該階段
stage: build_129 # 階段標識
before_script: # 執(zhí)行命令前
- yarn -v
script: # 執(zhí)行命令
- yarn
- yarn build
artifacts: # 產(chǎn)物
paths: # 產(chǎn)物生成地址
- dist
when: on_success # 生命周期鉤子
expire_in: 1 days # 存儲失效
deploy_129:job: # 執(zhí)行 deploy_129 階段
tags:
- "test" # 對應服務器注冊 runner 時輸入的 tag
only:
- main # 僅在main分支有提交代碼時執(zhí)行該階段
stage: deploy_129 # 階段標識
environment: # 環(huán)境變量
name: "129" # 環(huán)境變量名稱
before_script: # 執(zhí)行命令前
- mkdir -p ~/.ssh # 創(chuàng)建 .ssh 文件夾
- chmod 700 ~/.ssh # 權限改為 700
- eval $(ssh-agent -s) # 私鑰程序
- echo "$ENV_129_SSH_KNOWN_HOST" > ~/.ssh/known_hosts # 將 ENV_129_SSH_KNOWN_HOST 變量的內(nèi)容輸出到 known_hosts 文件中
- chmod 644 ~/.ssh/known_hosts # 將 known_hosts 文件權限改為 644
- echo "$ENV_129_SSH_CONFIG" > ~/.ssh/config # 將 ENV_129_SSH_CONFIG 變量的內(nèi)容輸出到 config 文件中
- chmod 600 ~/.ssh/config # 將 config 文件權限改為 600
- echo "$ENV_129_SSH_PRIVATE_KEY" > ~/.ssh/id_rsa_129 # 將 ENV_129_SSH_PRIVATE_KEY 變量的內(nèi)容輸出到 id_rsa_129 文件中
- chmod 600 ~/.ssh/id_rsa_129 # 將 id_rsa_129 文件權限改為 600
- ssh-add ~/.ssh/id_rsa_129 # 將密鑰添加到 ssh-agent 的高速緩存中
script: # 執(zhí)行命令
- scp -r ./dist $ENV_129_USERNAME@$ENV_129_HOST:$ENV_129_PATH # 將當前 dist 文件夾遞歸復制到 xiaoheihei@192.168.159.129:~/html/ 下
整理遇到的問題:
- 免密登錄無響應
嘗試配置服務器 /etc/ssh/sshd_config 文件中
StrictModes no
RSAAuthentication yes
PubkeyAuthentication yes - 免密登錄配置后仍需要輸入密碼
嘗試配置服務器 /etc/ssh/sshd_config 文件中
PasswordAuthentication no - 注冊好runner后在gitlab中提示New runner. Has not connected yet
查看/etc/gitlab-runner/config.toml中是否有剛剛注冊的runner信息,如果沒有嘗試切換root賬戶重新注冊