Skip to content

Commit 482763d

Browse files
Enhance frontend CI workflow with detailed steps
Updated frontend CI workflow to include detailed steps for building and scanning the project.
1 parent ee18ffe commit 482763d

1 file changed

Lines changed: 80 additions & 28 deletions

File tree

.github/workflows/frontend-ci.yml

Lines changed: 80 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,92 @@
1+
# 工作流名称
12
name: Frontend CI
23

3-
# 触发条件:当代码推送到 main/master 分支,或发起 PR 时自动执行
4+
# 触发条件
45
on:
6+
# 当代码 push 到 main/master 分支时自动执行
57
push:
68
branches: [ "main", "master" ]
9+
10+
# 当提交 Pull Request 到 main/master 时自动执行
711
pull_request:
812
branches: [ "main", "master" ]
913

1014
jobs:
1115
build:
12-
runs-on: ubuntu-latest # 在最新的 Ubuntu 环境下运行
16+
# 运行环境:GitHub 提供的 Ubuntu 虚拟机
17+
runs-on: ubuntu-latest
1318

1419
steps:
15-
# 1. 拉取代码库
16-
- name: Checkout code
17-
uses: actions/checkout@v4
18-
19-
# 2. 配置 Node.js 环境
20-
- name: Setup Node.js
21-
uses: actions/setup-node@v4
22-
with:
23-
node-version: 20 # 根据你项目的情况,Node.js 20 是目前最稳定的长期支持版本
24-
cache: 'npm' # 开启 npm 缓存,加快以后的构建速度
25-
26-
# 3. 安装项目依赖
27-
- name: Install dependencies
28-
run: npm ci # 使用 npm ci 代替 npm install,它会严格按照 package-lock.json 安装,更稳定且速度更快
29-
30-
# 4. 执行类型检查与代码打包
31-
- name: Build project
32-
run: npm run build # 对应你 package.json 里的 vue-tsc -b && vite build
33-
34-
# 5. (可选)上传构建产物 (dist 目录)
35-
# 这样你就可以在每次 Action 运行成功后,直接下载打包好的前端静态文件
36-
- name: Upload build artifacts
37-
uses: actions/upload-artifact@v4
38-
with:
39-
name: frontend-dist
40-
path: dist/
20+
21+
# 第一步:拉取 GitHub 仓库代码到 Runner
22+
- name: Checkout code
23+
uses: actions/checkout@v4
24+
with:
25+
# 拉取完整 git 历史
26+
# SonarCloud 分析代码变更时需要
27+
fetch-depth: 0
28+
29+
# 第二步:安装 Node.js 环境
30+
- name: Setup Node.js
31+
uses: actions/setup-node@v4
32+
with:
33+
# 使用 Node.js 20 LTS 版本
34+
node-version: 20
35+
36+
# 开启 npm 依赖缓存
37+
# 加快后续 CI 构建速度
38+
cache: npm
39+
40+
# 第三步:安装前端依赖
41+
- name: Install dependencies
42+
# 如果前端项目在 frontend 目录下
43+
working-directory: frontend
44+
45+
# npm ci:
46+
# 严格按照 package-lock.json 安装依赖
47+
# 比 npm install 更稳定、更适合 CI
48+
run: npm ci
49+
50+
# 第四步:构建前端项目
51+
- name: Build project
52+
working-directory: frontend
53+
54+
# 执行 package.json 中的 build 命令
55+
# 一般对应:
56+
# vue-tsc -b && vite build
57+
#
58+
# 包含:
59+
# 1. TypeScript 类型检查
60+
# 2. Vite 正式环境打包
61+
run: npm run build
62+
63+
# 第五步:SonarCloud 代码质量扫描
64+
- name: SonarCloud Scan
65+
66+
# 官方 SonarCloud GitHub Action
67+
uses: SonarSource/sonarqube-scan-action@v6
68+
69+
# 环境变量
70+
env:
71+
# 从 GitHub Secrets 中读取 SONAR_TOKEN
72+
# 避免 Token 泄露
73+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
74+
75+
with:
76+
# 指定 Sonar 扫描的项目目录
77+
# 因为前端代码在 frontend 目录
78+
projectBaseDir: frontend
79+
80+
# 第六步:上传前端构建产物
81+
- name: Upload build artifacts
82+
83+
# GitHub 官方 Artifact 上传 Action
84+
uses: actions/upload-artifact@v4
85+
86+
with:
87+
# Artifact 名称
88+
name: frontend-dist
89+
90+
# 上传 dist 打包目录
91+
# 后续可以在 Actions 页面直接下载
92+
path: frontend/dist/

0 commit comments

Comments
 (0)