Gitlab-CI部署流程
-
ssh连接服务器
-
触发条件
当用户推送最新代码至master分支时,trigger gitlab-ci
-
原理
利用gitlab自动化在gitlab打包
打包后的文件利用ssh推送至服务器
-
读取gitlab-ci.yml文件
-
.gitlab-ci.yml
配置文件详解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连接服务器的用户名$USER_PASS
: ssh连接服务器的密码$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去操作~