GitHub Actions 缓存项目使用教程
1. 项目的目录结构及介绍
GitHub Actions 缓存项目的目录结构相对简单,主要包含以下几个部分:
.
├── README.md
├── action.yml
├── package.json
├── src
│ ├── cache.ts
│ ├── constants.ts
│ ├── main.ts
│ ├── restore.ts
│ └── save.ts
└── tsconfig.json
- README.md: 项目说明文档,包含项目的基本介绍、使用方法和示例。
- action.yml: GitHub Actions 的配置文件,定义了 Action 的输入、输出和运行环境。
- package.json: Node.js 项目的依赖管理文件,包含项目的依赖和脚本命令。
- src: 源代码目录,包含项目的核心逻辑。
- cache.ts: 缓存操作的核心逻辑。
- constants.ts: 常量定义。
- main.ts: 主入口文件。
- restore.ts: 恢复缓存的操作逻辑。
- save.ts: 保存缓存的操作逻辑。
- tsconfig.json: TypeScript 的配置文件,定义了 TypeScript 编译选项。
2. 项目的启动文件介绍
项目的启动文件是 src/main.ts
,它是整个项目的入口点。该文件主要负责解析输入参数,并根据参数调用相应的缓存操作逻辑。
import * as core from '@actions/core';
import { restoreCache, saveCache } from './cache';
async function run() {
try {
const path = core.getInput('path');
const key = core.getInput('key');
const restoreKeys = core.getInput('restore-keys').split(',');
if (core.getInput('restore') === 'true') {
await restoreCache(path, key, restoreKeys);
}
if (core.getInput('save') === 'true') {
await saveCache(path, key);
}
} catch (error) {
core.setFailed(error.message);
}
}
run();
3. 项目的配置文件介绍
项目的配置文件主要是 action.yml
,它定义了 GitHub Actions 的输入、输出和运行环境。
name: 'Cache'
description: 'Cache dependencies and build outputs'
inputs:
path:
description: 'A list of files, directories, and wildcard patterns to cache and restore'
required: true
key:
description: 'An explicit key for restoring and saving the cache'
required: true
restore-keys:
description: 'An ordered list of keys to use for restoring the cache if no cache hit occurred for key'
required: false
default: ''
restore:
description: 'Whether to restore the cache'
required: false
default: 'true'
save:
description: 'Whether to save the cache'
required: false
default: 'true'
runs:
using: 'node12'
main: 'dist/index.js'
- inputs: 定义了 Action 的输入参数,包括
path
、key
、restore-keys
、restore
和save
。 - runs: 定义了 Action 的运行环境,使用 Node.js 12 版本,并指定主入口文件为
dist/index.js
。
通过以上配置,用户可以在 GitHub Actions 工作流中使用该缓存 Action,以提高构建和依赖安装的效率。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考