ssh鏈接服務器css
觸發條件html
當用戶推送最新代碼至master分支時,trigger gitlab-ci前端
原理vue
利用gitlab自動化在gitlab打包node
打包後的文件利用ssh推送至服務器linux
讀取gitlab-ci.yml文件webpack
.gitlab-ci.yml
配置文件詳解git
image: node:8 # 拉取node version:8 的docker鏡像
cache: # 緩存文件夾 or 文件
paths:
- node_modules/
before_script: # 執行腳本前的工做
- apt-get update -qq && apt-get install -y -qq sshpass # 更新apt-get工具並安裝sshpass
deploy_stage: # deploy_stage腳本
stage: deploy
environment: Staging
only:
- master # 配置觸發條件
script:
- npm install # 步驟1: 安裝前端依賴包
- npm run build # 步驟2: 打包前端文件夾
- export SSHPASS=$USER_PASS # 步驟3: 導出 USER_PASS環境變量供sshpass使用
- sshpass -e scp -o stricthostkeychecking=no -r dist/ $USER_NAME@$SERVER_IP:$SERVER_PATH # 步驟4: ssh鏈接服務器並推送dist/文件夾至SERVER_PATH下
複製代碼
$USER_NAME
: ssh鏈接服務器的用戶名web
$USER_PASS
: ssh鏈接服務器的密碼vue-cli
$SERVER_IP
: 服務器IP地址
$SERVER_PATH
: 推送服務器絕對路徑
以上變量皆在Gitlab項目 setting
=> CI
=> Environment Variables
裏配置
推送完成後Gitlab
=> CI
=> Pipeline
查看日誌
日誌詳解 (帶藍標的爲gitlab中配置好的script步驟)
Running with gitlab-runner 11.9.0-rc2 (227934c0)
on docker-auto-scale 0277ea0f
Using Docker executor with image node:8 ...
Pulling docker image node:8 ...
Using docker image sha256:8c51cec97ebfc94d85daf702377acb73afcfc6cfd5a85e4fe2816732e25ec229 for node:8 ...
Running on runner-0277ea0f-project-11375217-concurrent-0 via runner-0277ea0f-srm-1553699604-3058a32d...
Initialized empty Git repository in /builds/xxx/doer/.git/
Fetching changes...
Created fresh repository.
From https://gitlab.com/xxx/doer
* [new branch] develop -> origin/develop
* [new branch] master -> origin/master
Checking out 017588b7 as master...
Skipping Git submodules setup
Checking cache for default-2...
Downloading cache.zip from https://storage.googleapis.com/gitlab-com-runners-cache/project/11375217/default-2
Successfully extracted cache
$ apt-get update -qq && apt-get install -y -qq sshpass
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package sshpass.
(Reading database ...
(Reading database ... 5%
(Reading database ... 10%
(Reading database ... 15%
(Reading database ... 20%
(Reading database ... 25%
(Reading database ... 30%
(Reading database ... 35%
(Reading database ... 40%
(Reading database ... 45%
(Reading database ... 50%
(Reading database ... 55%
(Reading database ... 60%
(Reading database ... 65%
(Reading database ... 70%
(Reading database ... 75%
(Reading database ... 80%
(Reading database ... 85%
(Reading database ... 90%
(Reading database ... 95%
(Reading database ... 100%
(Reading database ... 29978 files and directories currently installed.)
Preparing to unpack .../sshpass_1.06-1_amd64.deb ...
Unpacking sshpass (1.06-1) ...
Setting up sshpass (1.06-1) ...
$ npm install
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.7 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.7: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
audited 27888 packages in 10.616s
found 0 vulnerabilities
$ npm run build
> doer@0.1.0 build /builds/xxx/doer
> vue-cli-service build
- Building for production...
Starting type checking and linting service...
Using 1 worker with 2048MB memory limit
WARNING Compiled with 2 warnings15:15:42
warning
entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web performance.
Entrypoints:
app (247 KiB)
js/chunk-vendors.419d8636.js
css/app.20dafc28.css
js/app.40b96980.js
warning
webpack performance recommendations:
You can limit the size of your bundles by using import() or require.ensure to lazy load some parts of your application.
For more info visit https://webpack.js.org/guides/code-splitting/
File Size Gzipped
dist/js/chunk-vendors.419d8636.js 240.07 KiB 82.82 KiB
dist/js/app.40b96980.js 6.46 KiB 2.51 KiB
dist/css/app.20dafc28.css 0.35 KiB 0.24 KiB
Images and other types of assets omitted.
DONE Build complete. The dist directory is ready to be deployed.
INFO Check out deployment instructions at https://cli.vuejs.org/guide/deployment.html
$ export SSHPASS=$USER_PASS
$ sshpass -e scp -o stricthostkeychecking=no -r dist/ $USER_NAME@$SERVER_IP:$SERVER_PATH
Warning: Permanently added '139.196.98.25' (ECDSA) to the list of known hosts.
Creating cache default-2...
node_modules/: found 25362 matching files
Uploading cache.zip to https://storage.googleapis.com/gitlab-com-runners-cache/project/11375217/default-2
Created cache
Job succeeded
複製代碼
ssh登陸服務器查看文件,成功~
這樣就完成自動部署的流程啦
後續有待使用docker去操做~