CircleCI 的缓存
- restore_cache—-复用缓存
- save_cache—-保存缓存
配置 shell 文件
创建文件 根目录/scripts/deploy.sh
#!/bin/sh# 构想 https://gist.github.com/motemen/8595451# 基于 https://github.com/eldarlabs/ghpages-deploy-script/blob/master/scripts/deploy-ghpages.sh# MIT许可 https://github.com/eldarlabs/ghpages-deploy-script/blob/master/LICENSE# abort the script if there is a non-zero errorset -e# 打印当前的工作路径pwdremote=$(git config remote.origin.url)echo 'remote is: '$remote# 新建一个发布的目录mkdir gh-pages-branchcd gh-pages-branch# 创建的一个新的仓库# 设置发布的用户名与邮箱git config --global user.email "$GH_EMAIL" >/dev/null 2>&1git config --global user.name "$GH_NAME" >/dev/null 2>&1git initgit remote add --fetch origin "$remote"echo 'email is: '$GH_EMAILecho 'name is: '$GH_NAMEecho 'sitesource is: '$siteSource# 切换gh-pages分支if git rev-parse --verify origin/gh-pages >/dev/null 2>&1; thengit checkout gh-pages# 删除掉旧的文件内容git rm -rf .elsegit checkout --orphan gh-pagesfi# 把构建好的文件目录给拷贝进来cp -a "../${siteSource}/." .ls -la# 把所有的文件添加到gitgit add -A# 添加一条提交内容git commit --allow-empty -m "Deploy to GitHub pages [ci skip]"# 推送文件git push --force --quiet origin gh-pages# 资源回收,删除临时分支与目录cd ..rm -rf gh-pages-branchecho "Finished Deployment!"
添加 vue.config.js 文件,资源文件的目录配置
module.exports = {publicPath: process.env.NODE_ENV === "production" ? "/CircleCI-Demo" : "/",};
config.yml 文件中应用 shell 文件
version: 2jobs:build:docker:- image: circleci/node:12filters:branches:only:- mastersteps:- add_ssh_keys:fingerprints:- "f6:81:b7:24:86:07:78:df:5e:ba:28:dd:5f:1e:4f:ba"- checkout- restore_cache:keys:- dependencies_imooc- run:name: Installcommand: npm install- save_cache:key: dependencies_imoocpaths:- node_modules- run:name: Buildcommand: npm run build- run:name: Prepare Shell commands# shell chmod +x 赋予执行权限# 执行 shell 脚本command: chmod +x scripts/deploy.sh- run:name: Run Deploy to GitHub Pagescommand: ./scripts/deploy.sh
总结
- GitHub 账号注册、授权
- 新建项目添加 .circleci/config.yml 文件
- 配置脚本任务,流程:环境 -> 构建 -> (缓存) -> 发布
