Github Actions 构建Vue3 + Vite项目

本篇文章以自己创建的项目为例,用Github Actions构建。

Github地址:https://github.com/ling08140814/myCarousel

访问地址:https://ling08140814.github.io/myCarousel/

具体步骤:
1、创建一个Vue3的项目,并完成代码开发。
2、在Github创建仓库,并将代码推到仓库。
3、在package.json配置homepage。
javascript 复制代码
"homepage": "https://<Github用户名>.github.io/<Github仓库名>"

举例:

javascript 复制代码
"homepage": "https://ling08140814.github.io/myCarousel"
4、在vite.config.js配置base。
javascript 复制代码
base: "/<Github仓库名>/"

举例:

javascript 复制代码
base: "/myCarousel/"
5、在Github Actions 创建 workflow,生成.yml文件,进行构建。

5.1 新建workflow

5.2 选择workflow

5.3 打开之后,将.yml默认内容进行替换,然后commit

替换内容:

javascript 复制代码
name: GitHub Actions Build and Deploy Demo        # 大标题
on:
  push:
    branches:
      - master                                   # 监听哪个分支的哪个动作
permissions:
  contents: write                                # 设置权限,可写,也可以不用配置,v4版本新增的
jobs:
  build-and-deploy:                              # 其中一个任务的名称,ID
    runs-on: ubuntu-latest                       # 这个任务运行所需的环境
    steps:
      - name: Checkout                           # 第一步:获取源码
        uses: actions/checkout@v3                # 使用的工具

      - name: Install and Build                  # 第二步:下载依赖和构建
        run: |
          npm install
          npm run build

      - name: Deploy                           # 第三步:部署
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          folder: dist                         # 部署的目录
          branch: deploy                       # 部署到哪个分支

5.4 commit之后,会自动构建,构建成功后展示如下内容

6、配置部署分支,进行访问。

部署分支和.yml里的部署分支保持一致

7、输入第3步中homepage的路径,可以正常访问啦。
相关推荐
lpfasd1233 小时前
系统、详细地介绍 GitHub 官方 API 的能力边界
github
KevinShi_BJ3 小时前
Github Copilot 实践
github·copilot
秋雨雁南飞4 小时前
图床软件 PicGo + Github
github·picgo·图床
moment&forever5 小时前
GitHub 托管 API 地址配置文件:实现零成本云配置托管
github
小龙15 小时前
【Git 报错解决】本地无有效提交无法推送(`src refspec main does not match any`)
git·github·报错
行百里er20 小时前
一个还没写代码的开源项目,我先来“画个饼”:Spring Insight
后端·开源·github
知行力20 小时前
【GitHub每日速递 20260108】告别云服务弊端,Memos隐私至上自托管笔记服务来袭!
笔记·github
无限进步_21 小时前
【数据结构&C语言】对称二叉树的递归之美:镜像世界的探索
c语言·开发语言·数据结构·c++·算法·github·visual studio
CoderJia程序员甲1 天前
GitHub 热榜项目 - 日榜(2026-1-7)
人工智能·ai·大模型·github·ai教程
逛逛GitHub1 天前
GitHub 上 2300 点赞的搜索 Agent,有点惊艳啊。
github