如何部署项目到 github

816 阅读1分钟
  1. 在 vue.config.js 设置将要部署的 publicPath
module.exports = {
  publicPath: process.env.NODE_ENV === 'production'
    ? '/项目名-website/'
    : '/'
}
  1. 先 yarn build

yarn build

  1. 点击进去看文档

image.png

  1. 按照文档,接下来运行

yarn global add serve

serve -s dist

  1. 检查打包没问题后

  2. 到 github 新建仓库(第一次新建,用来放全部代码)

  3. 然后执行

git remote add origin git@github.com:用户名/项目名.git

git push -u origin master

  1. 再次到 github 新建仓库,仓库名与第1步里的 publicPath 保持一致,如:项目名-website(第二次新建,用来部署)

  2. 在项目根目录新建 deploy.sh 文件(用来部署的),写入如下代码

#!/usr/bin/env sh

# 当发生错误时中止脚本
set -e

# 构建
yarn build

# cd 到构建输出的目录下
cd dist

# 部署到自定义域域名
# echo 'www.example.com' > CNAME

git init
git add -A
git commit -m 'deploy'

# 部署到 https://<USERNAME>.github.io (部署到源码仓库)
git push -f git@github.com:用户名/项目名.git master

# 部署到 https://<USERNAME>.github.io/<REPO> (部署到预览仓库)
git push -f git@github.com:用户名/项目名-website.git master:gh-pages

cd -
  1. 最后命令行执行

sh deploy.sh

  1. 刷新第二次新建的仓库页面,已经部署完成

  2. 项目预览链接在此

image.png