使用具有多个缓存路径的 GitHub 缓存操作?

Sil*_*lan 13 windows caching github-actions

我正在尝试使用官方 GitHub 缓存操作(https://github.com/actions/cache)来缓存一些二进制文件以加快我的一些工作流程,但是在指定多个时我无法让它工作缓存路径。

这是我使用单个缓存路径设置的一个简单的工作测试:有一个用于写入缓存的操作,一个用于读取缓存的操作(两者都在单独的工作流程中执行,但在同一存储库和分支上)。首先执行写入操作,并创建一个文件“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
Run Code Online (Sandbox Code Playgroud)

读取操作检索缓存,递归打印目录中所有文件的列表,以确认已从缓存中恢复文件,然后打印缓存的 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"
Run Code Online (Sandbox Code Playgroud)

这可以正常工作,没有任何问题。

现在,缓存操作的描述包含指定多个缓存路径的示例:

  - uses: actions/cache@v2
    with:
      path: | 
        path/to/dependencies
        some/other/dependencies 
      key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试将其用于示例操作时,它不起作用。在新的写入操作中,我创建两个文件“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
Run Code Online (Sandbox Code Playgroud)

新的读取操作与旧的相同,但也指定了两个路径:

    # 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"
Run Code Online (Sandbox Code Playgroud)

这次我仍然得到缓存已被读取的确认:

Cache restored successfully
Cache restored from key: test-cache-multi-path
Cache hit: true
Run Code Online (Sandbox Code Playgroud)

但是,“ls -R”不会列出这些文件,并且“cat”命令会失败,因为这些文件不存在。

我的错误在哪里?使用缓存操作指定多个路径的正确方法是什么?

Яро*_*лин 11

我可以通过一些修改使其工作;

\n
    \n
  • 使用相对路径而不是绝对路径
  • \n
  • 使用内容的哈希值作为密钥
  • \n
\n

至少 bash 的绝对路径看起来像这样:

\n
    \n
  • /d/a/so-foobar-cache/so-foobar-cache/cache_test/cache_test/subdir
  • \n
\n

其中 so-foobar-cache 是存储库的名称。

\n

.github/workflows/foobar.yml

\n
\nname: Store and Fetch cached files\non: [push]\njobs:\n  store:\n    runs-on: windows-2019\n    steps:\n      - name: Create files\n        shell: bash\n        id: store\n        run: |\n          mkdir -p \'cache_test/cache_test/subdir\'\n          cd \'cache_test/cache_test/subdir\'\n          echo pwd $(pwd)\n          printf \'%s\' "Lorem ipsum" >> a.txt\n          printf \'%s\' "dolor sit amet" >> b.md\n          cat a.txt b.md\n      - name: Store in cache\n        uses: actions/cache@v2\n        with:\n          path: |\n            cache_test/cache_test/**/*.txt\n            cache_test/cache_test/**/*.md\n          key: multiple-files-${{ hashFiles(\'cache_test/cache_test/**\') }}\n      - name: Print files (A)\n        shell: bash\n        run: |\n          echo "Cache hit: ${{steps.store.outputs.cache-hit}}"\n          find cache_test/cache_test/subdir\n          cat cache_test/cache_test/subdir/a.txt\n          cat cache_test/cache_test/subdir/b.md\n\n\n  fetch:\n    runs-on: windows-2019\n    needs: store\n    steps:\n      - name: Restore\n        uses: actions/cache@v2\n        with:\n          path: |\n            cache_test/cache_test/**/*.txt\n            cache_test/cache_test/**/*.md\n          key: multiple-files-${{ hashFiles(\'cache_test/cache_test/**\') }}\n          restore-keys: |\n            multiple-files-${{ hashFiles(\'cache_test/cache_test/**\') }}\n            multiple-files-\n      - name: Print files (B)\n        shell: bash\n        run: |\n          find cache_test -type f | xargs -t grep -e.\n
Run Code Online (Sandbox Code Playgroud)\n

日志

\n
$ gh run view 1446486801 \n\n\xe2\x9c\x93 master Store and Fetch cached files \xc2\xb7 1446486801\nTriggered via push about 3 minutes ago\n\nJOBS\n\xe2\x9c\x93 store in 5s (ID 4171907768)\n\xe2\x9c\x93 fetch in 10s (ID 4171909690)\n
Run Code Online (Sandbox Code Playgroud)\n

第一份工作

\n
$ gh run view 1446486801 --log --job=4171907768 | grep -e Create  -e Store -e Print\nstore   Create files    2021-11-10T22:59:32.1396931Z ##[group]Run mkdir -p \'cache_test/cache_test/subdir\'\nstore   Create files    2021-11-10T22:59:32.1398025Z mkdir -p \'cache_test/cache_test/subdir\'\nstore   Create files    2021-11-10T22:59:32.1398695Z cd \'cache_test/cache_test/subdir\'\nstore   Create files    2021-11-10T22:59:32.1399360Z echo pwd $(pwd)\nstore   Create files    2021-11-10T22:59:32.1399936Z printf \'%s\' "Lorem ipsum" >> a.txt\nstore   Create files    2021-11-10T22:59:32.1400672Z printf \'%s\' "dolor sit amet" >> b.md\nstore   Create files    2021-11-10T22:59:32.1401231Z cat a.txt b.md\nstore   Create files    2021-11-10T22:59:32.1623649Z shell: C:\\Program Files\\Git\\bin\\bash.EXE --noprofile --norc -e -o pipefail {0}\nstore   Create files    2021-11-10T22:59:32.1626211Z ##[endgroup]\nstore   Create files    2021-11-10T22:59:32.9569082Z pwd /d/a/so-foobar-cache/so-foobar-cache/cache_test/cache_test/subdir\nstore   Create files    2021-11-10T22:59:32.9607728Z Lorem ipsumdolor sit amet\nstore   Store in cache  2021-11-10T22:59:33.9705422Z ##[group]Run actions/cache@v2\nstore   Store in cache  2021-11-10T22:59:33.9706196Z with:\nstore   Store in cache  2021-11-10T22:59:33.9706815Z   path: cache_test/cache_test/**/*.txt\nstore   Store in cache  cache_test/cache_test/**/*.md\nstore   Store in cache  \nstore   Store in cache  2021-11-10T22:59:33.9708499Z   key: multiple-files-25c0e6413e23766a3681413625169cee1ca3a7cd2186cc1b1df5370fb43bce55\nstore   Store in cache  2021-11-10T22:59:33.9709961Z ##[endgroup]\nstore   Store in cache  2021-11-10T22:59:35.1757943Z Received 260 of 260 (100.0%), 0.0 MBs/sec\nstore   Store in cache  2021-11-10T22:59:35.1761565Z Cache Size: ~0 MB (260 B)\nstore   Store in cache  2021-11-10T22:59:35.1781110Z [command]C:\\Windows\\System32\\tar.exe -z -xf D:/a/_temp/653f7664-e139-4930-9710-e56942f9fa47/cache.tgz -P -C D:/a/so-foobar-cache/so-foobar-cache\nstore   Store in cache  2021-11-10T22:59:35.2069751Z Cache restored successfully\nstore   Store in cache  2021-11-10T22:59:35.2737840Z Cache restored from key: multiple-files-25c0e6413e23766a3681413625169cee1ca3a7cd2186cc1b1df5370fb43bce55\nstore   Print files (A) 2021-11-10T22:59:35.3087596Z ##[group]Run echo "Cache hit: "\nstore   Print files (A) 2021-11-10T22:59:35.3088324Z echo "Cache hit: "\nstore   Print files (A) 2021-11-10T22:59:35.3088983Z find cache_test/cache_test/subdir\nstore   Print files (A) 2021-11-10T22:59:35.3089571Z cat cache_test/cache_test/subdir/a.txt\nstore   Print files (A) 2021-11-10T22:59:35.3090176Z cat cache_test/cache_test/subdir/b.md\nstore   Print files (A) 2021-11-10T22:59:35.3104465Z shell: C:\\Program Files\\Git\\bin\\bash.EXE --noprofile --norc -e -o pipefail {0}\nstore   Print files (A) 2021-11-10T22:59:35.3106449Z ##[endgroup]\nstore   Print files (A) 2021-11-10T22:59:35.3494703Z Cache hit: \nstore   Print files (A) 2021-11-10T22:59:35.4456032Z cache_test/cache_test/subdir\nstore   Print files (A) 2021-11-10T22:59:35.4456852Z cache_test/cache_test/subdir/a.txt\nstore   Print files (A) 2021-11-10T22:59:35.4459226Z cache_test/cache_test/subdir/b.md\nstore   Print files (A) 2021-11-10T22:59:35.4875011Z Lorem ipsumdolor sit amet\nstore   Post Store in cache 2021-11-10T22:59:35.6109511Z Post job cleanup.\nstore   Post Store in cache 2021-11-10T22:59:35.7899690Z Cache hit occurred on the primary key multiple-files-25c0e6413e23766a3681413625169cee1ca3a7cd2186cc1b1df5370fb43bce55, not saving cache.\n\n
Run Code Online (Sandbox Code Playgroud)\n

第二份工作

\n
$ gh run view 1446486801 --log --job=4171909690  | grep -e Restore -e Print\nfetch   Restore 2021-11-10T22:59:50.8498516Z ##[group]Run actions/cache@v2\nfetch   Restore 2021-11-10T22:59:50.8499346Z with:\nfetch   Restore 2021-11-10T22:59:50.8499883Z   path: cache_test/cache_test/**/*.txt\nfetch   Restore cache_test/cache_test/**/*.md\nfetch   Restore \nfetch   Restore 2021-11-10T22:59:50.8500449Z   key: multiple-files-\nfetch   Restore 2021-11-10T22:59:50.8501079Z   restore-keys: multiple-files-\nfetch   Restore multiple-files-\nfetch   Restore \nfetch   Restore 2021-11-10T22:59:50.8501644Z ##[endgroup]\nfetch   Restore 2021-11-10T22:59:53.1143793Z Received 257 of 257 (100.0%), 0.0 MBs/sec\nfetch   Restore 2021-11-10T22:59:53.1145450Z Cache Size: ~0 MB (257 B)\nfetch   Restore 2021-11-10T22:59:53.1163664Z [command]C:\\Windows\\System32\\tar.exe -z -xf D:/a/_temp/30b0dc24-b25f-4713-b3d3-cecee7116785/cache.tgz -P -C D:/a/so-foobar-cache/so-foobar-cache\nfetch   Restore 2021-11-10T22:59:53.1784328Z Cache restored successfully\nfetch   Restore 2021-11-10T22:59:53.5197756Z Cache restored from key: multiple-files-\nfetch   Print files (B) 2021-11-10T22:59:53.5483939Z ##[group]Run find cache_test -type f | xargs -t grep -e.\nfetch   Print files (B) 2021-11-10T22:59:53.5484730Z find cache_test -type f | xargs -t grep -e.\nfetch   Print files (B) 2021-11-10T22:59:53.5498140Z shell: C:\\Program Files\\Git\\bin\\bash.EXE --noprofile --norc -e -o pipefail {0}\nfetch   Print files (B) 2021-11-10T22:59:53.5498674Z ##[endgroup]\nfetch   Print files (B) 2021-11-10T22:59:55.8119800Z grep -e. cache_test/cache_test/subdir/a.txt cache_test/cache_test/subdir/b.md\nfetch   Print files (B) 2021-11-10T22:59:56.1777887Z cache_test/cache_test/subdir/a.txt:Lorem ipsum\nfetch   Print files (B) 2021-11-10T22:59:56.1784138Z cache_test/cache_test/subdir/b.md:dolor sit amet\nfetch   Post Restore    2021-11-10T22:59:56.3890391Z Post job cleanup.\nfetch   Post Restore    2021-11-10T22:59:56.5481739Z Cache hit occurred on the primary key multiple-files-, not saving cache.\n\n
Run Code Online (Sandbox Code Playgroud)\n