CircleCI 的缓存

  • restore_cache—-复用缓存
  • save_cache—-保存缓存


配置 shell 文件

创建文件 根目录/scripts/deploy.sh

  1. #!/bin/sh
  2. # 构想 https://gist.github.com/motemen/8595451
  3. # 基于 https://github.com/eldarlabs/ghpages-deploy-script/blob/master/scripts/deploy-ghpages.sh
  4. # MIT许可 https://github.com/eldarlabs/ghpages-deploy-script/blob/master/LICENSE
  5. # abort the script if there is a non-zero error
  6. set -e
  7. # 打印当前的工作路径
  8. pwd
  9. remote=$(git config remote.origin.url)
  10. echo 'remote is: '$remote
  11. # 新建一个发布的目录
  12. mkdir gh-pages-branch
  13. cd gh-pages-branch
  14. # 创建的一个新的仓库
  15. # 设置发布的用户名与邮箱
  16. git config --global user.email "$GH_EMAIL" >/dev/null 2>&1
  17. git config --global user.name "$GH_NAME" >/dev/null 2>&1
  18. git init
  19. git remote add --fetch origin "$remote"
  20. echo 'email is: '$GH_EMAIL
  21. echo 'name is: '$GH_NAME
  22. echo 'sitesource is: '$siteSource
  23. # 切换gh-pages分支
  24. if git rev-parse --verify origin/gh-pages >/dev/null 2>&1; then
  25. git checkout gh-pages
  26. # 删除掉旧的文件内容
  27. git rm -rf .
  28. else
  29. git checkout --orphan gh-pages
  30. fi
  31. # 把构建好的文件目录给拷贝进来
  32. cp -a "../${siteSource}/." .
  33. ls -la
  34. # 把所有的文件添加到git
  35. git add -A
  36. # 添加一条提交内容
  37. git commit --allow-empty -m "Deploy to GitHub pages [ci skip]"
  38. # 推送文件
  39. git push --force --quiet origin gh-pages
  40. # 资源回收,删除临时分支与目录
  41. cd ..
  42. rm -rf gh-pages-branch
  43. echo "Finished Deployment!"

添加 vue.config.js 文件,资源文件的目录配置

  1. module.exports = {
  2. publicPath: process.env.NODE_ENV === "production" ? "/CircleCI-Demo" : "/",
  3. };

config.yml 文件中应用 shell 文件

  1. version: 2
  2. jobs:
  3. build:
  4. docker:
  5. - image: circleci/node:12
  6. filters:
  7. branches:
  8. only:
  9. - master
  10. steps:
  11. - add_ssh_keys:
  12. fingerprints:
  13. - "f6:81:b7:24:86:07:78:df:5e:ba:28:dd:5f:1e:4f:ba"
  14. - checkout
  15. - restore_cache:
  16. keys:
  17. - dependencies_imooc
  18. - run:
  19. name: Install
  20. command: npm install
  21. - save_cache:
  22. key: dependencies_imooc
  23. paths:
  24. - node_modules
  25. - run:
  26. name: Build
  27. command: npm run build
  28. - run:
  29. name: Prepare Shell commands
  30. # shell chmod +x 赋予执行权限
  31. # 执行 shell 脚本
  32. command: chmod +x scripts/deploy.sh
  33. - run:
  34. name: Run Deploy to GitHub Pages
  35. command: ./scripts/deploy.sh

总结

  • GitHub 账号注册、授权
  • 新建项目添加 .circleci/config.yml 文件
  • 配置脚本任务,流程:环境 -> 构建 -> (缓存) -> 发布