Google Cloud Functions 部署 GitHub Actions 项目教程
1. 项目目录结构及介绍
deploy-cloud-functions/
├── bin/
├── dist/
├── src/
├── tests/
├── .gitignore
├── .prettierrc.js
├── CHANGELOG.md
├── CODEOWNERS
├── LICENSE
├── README.md
├── action.yml
├── eslint-config.mjs
├── package-lock.json
├── package.json
└── tsconfig.json
目录结构说明
- bin/: 存放项目的可执行文件或脚本。
- dist/: 存放编译后的代码,通常是 TypeScript 编译后的 JavaScript 文件。
- src/: 存放项目的源代码,通常是 TypeScript 文件。
- tests/: 存放项目的测试代码。
- .gitignore: 指定 Git 版本控制系统忽略的文件和目录。
- .prettierrc.js: Prettier 代码格式化配置文件。
- CHANGELOG.md: 记录项目的变更日志。
- CODEOWNERS: 指定项目的代码所有者。
- LICENSE: 项目的开源许可证文件。
- README.md: 项目的说明文档。
- action.yml: GitHub Actions 的工作流配置文件。
- eslint-config.mjs: ESLint 配置文件。
- package-lock.json: 锁定项目依赖的版本。
- package.json: 项目的依赖管理文件。
- tsconfig.json: TypeScript 配置文件。
2. 项目启动文件介绍
项目的启动文件通常位于 src/
目录下,具体文件名取决于项目的结构。假设项目的入口文件为 src/index.ts
,则该文件是项目的启动文件。
src/index.ts
文件示例
import { deployCloudFunction } from './deploy';
async function main() {
await deployCloudFunction();
}
main().catch(console.error);
启动文件说明
src/index.ts
: 项目的入口文件,负责调用部署函数deployCloudFunction
并处理异常。
3. 项目配置文件介绍
action.yml
action.yml
是 GitHub Actions 的工作流配置文件,定义了该 Action 的输入、输出、运行环境等信息。
name: 'Deploy Cloud Functions'
description: 'A GitHub Action that deploys source code to Google Cloud Functions'
inputs:
project_id:
description: 'ID of the Google Cloud project'
required: false
region:
description: 'Region in which the function should be deployed'
required: false
default: 'us-central1'
name:
description: 'Name of the Cloud Function'
required: true
runtime:
description: 'Runtime for the function'
required: true
outputs:
url:
description: 'URL of the deployed Cloud Function'
runs:
using: 'node20'
main: 'dist/index.js'
配置文件说明
action.yml
: 定义了 GitHub Action 的输入参数(如project_id
、region
、name
、runtime
)和输出参数(如url
),以及运行环境(node20
)和入口文件(dist/index.js
)。
通过以上配置,用户可以在 GitHub Actions 工作流中使用该 Action 来部署 Google Cloud Functions。