【发布时间】:2021-11-11 07:06:57
【问题描述】:
我正在尝试使用官方 GitHub 缓存操作 (https://github.com/actions/cache) 来缓存一些二进制文件以加快我的一些工作流程,但是在指定多个缓存路径时我无法让它工作。
这是我使用单个缓存路径设置的一个简单的工作测试: 有一种用于写入缓存的操作,一种用于读取缓存的操作(两者都在不同的工作流中执行,但在同一个存储库和分支上)。 write-action首先执行,并创建一个文件“subdir/a.txt”,然后用“actions/cache@v2”动作缓存:
# Test with single path
- name: Create file
shell: bash
run: |
mkdir subdir
cd subdir
printf '%s' "Lorem ipsum" >> a.txt
- name: Write cache (Single path)
uses: actions/cache@v2
with:
path: "D:/a/cache_test/cache_test/**/*.txt"
key: test-cache-single-path
read-action 检索缓存,递归打印目录中所有文件的列表以确认它已从缓存中恢复文件,然后打印缓存的 txt 文件的内容:
- name: Get cached file
uses: actions/cache@v2
id: get-cache
with:
path: "D:/a/cache_test/cache_test/**/*.txt"
key: test-cache-single-path
- name: Print files
shell: bash
run: |
echo "Cache hit: ${{steps.get-cache.outputs.cache-hit}}"
cd "D:/a/cache_test/cache_test"
ls -R
cat "D:/a/cache_test/cache_test/subdir/a.txt"
这没有任何问题。
现在,缓存操作的描述包含一个指定多个缓存路径的示例:
- uses: actions/cache@v2
with:
path: |
path/to/dependencies
some/other/dependencies
key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
但是当我尝试对我的示例操作进行此操作时,它无法正常工作。 在新的写操作中,我创建了两个文件,“subdir/a.txt”和“subdir/b.md”,然后通过指定两个路径来缓存它们:
# Test with multiple paths
- name: Create files
shell: bash
run: |
mkdir subdir
cd subdir
printf '%s' "Lorem ipsum" >> a.txt
printf '%s' "dolor sit amet" >> b.md
#- name: Write cache (Multi path)
uses: actions/cache@v2
with:
path: |
"D:/a/cache_test/cache_test/**/*.txt"
"D:/a/cache_test/cache_test/**/*.md"
key: test-cache-multi-path
新的读取操作与旧的相同,但同时指定了两个路径:
# Read cache
- name: Get cached file
uses: actions/cache@v2
id: get-cache
with:
path: |
"D:/a/cache_test/cache_test/**/*.txt"
"D:/a/cache_test/cache_test/**/*.md"
key: test-cache-multi-path
- name: Print files
shell: bash
run: |
echo "Cache hit: ${{steps.get-cache.outputs.cache-hit}}"
cd "D:/a/cache_test/cache_test"
ls -R
cat "D:/a/cache_test/cache_test/subdir/a.txt"
cat "D:/a/cache_test/cache_test/subdir/b.md"
这次我仍然得到缓存已被读取的确认:
Cache restored successfully
Cache restored from key: test-cache-multi-path
Cache hit: true
但是“ls -R”没有列出文件,并且“cat”命令失败,因为文件不存在。
我的错误在哪里?使用缓存操作指定多个路径的正确方法是什么?
【问题讨论】:
标签: windows caching github-actions