使用 Github Action
通过本章节的指南,您可以快速在 Github 仓库集成 Actions 工作流,实现自动构建并部署至 EdgeOne Pages。
设置 Github 仓库 Secret
要运行此 Action,您需要在 GitHub 中创建仓库的 Secret:
访问您的 GitHub 仓库页面
前往 "Settings" > "Secrets and variables" > "Actions"
点击 "New repository secret"
在“Name”输入 EDGEONE_API_TOKEN,在“Secret”输入 EdgeOne API token 的值
完整的
.github/workflows/deploy.yml
配置如下:name: Build and Deployon:push:branches:- mainjobs:build-and-deploy:runs-on: ubuntu-lateststeps:- name: Checkout repositoryuses: actions/checkout@v4- name: Setup Node.jsuses: actions/setup-node@v4with:node-version: '22.11.0' # 选择适合的 Node.js 版本- name: Install dependenciesrun: npm install- name: Build projectrun: npm run build- name: Deploy to EdgeOne Pagesrun: npx edgeone pages deploy <outputDirectory> -n <projectName> -t ${{ secrets.EDGEONE_API_TOKEN }} [-e <env>]env:EDGEONE_API_TOKEN: ${{ secrets.EDGEONE_API_TOKEN }}
说明:
npx edgeone pages deploy
参数说明:<outputDirectory>: 项目构建后产物所在的文件夹(必填)
-n, --name: 需要部署的项目名称,项目不存在则自动创建新项目(必填)
-e, --env: 部署目标环境,可选值: production 或 preview(默认 production)
示例:npx edgeone pages deploy ./dist -n project-name -t ${{ secrets.EDGEONE_API_TOKEN }}
GitHub Actions 构建
将上述的
deploy.yml
文件配置到您的项目根目录下。当代码推送到 main 分支时,会触发以下构建过程:1. checkout 到目标仓库
2. 设置 Node.js 版本为 22.11.0
3. 安装项目依赖
4. 构建项目
EdgeOne Pages 部署
构建完成后,项目将通过以下过步骤自动部署到 EdgeOne Pages:
构建阶段生成 ./out 目录
使用 EdgeOne 命令行工具进行部署:
npx edgeone pages deploy ./out -n my-edgeone-pages-project -t ${{ secrets.EDGEONE_API_TOKEN }}