GitHub Actions 缓存项目使用教程

GitHub Actions 缓存项目使用教程

cacheCache dependencies and build outputs in GitHub Actions项目地址:https://gitcode.com/gh_mirrors/cach/cache

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 的输入参数,包括 pathkeyrestore-keysrestoresave
  • runs: 定义了 Action 的运行环境,使用 Node.js 12 版本,并指定主入口文件为 dist/index.js

通过以上配置,用户可以在 GitHub Actions 工作流中使用该缓存 Action,以提高构建和依赖安装的效率。

cacheCache dependencies and build outputs in GitHub Actions项目地址:https://gitcode.com/gh_mirrors/cach/cache

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宗鲁宽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值