How do I cache steps in GitHub actions?

后端 未结 6 1813
野趣味
野趣味 2021-01-30 10:54

Say I have a GitHub actions workflow with 2 steps.

  1. Download and compile my application\'s dependencies.
  2. Compile and test my application

My

6条回答
  •  后悔当初
    2021-01-30 11:02

    The cache action can only cache the contents of a folder. So if there is such a folder, you may win some time by caching it.

    For instance, if you use some imaginary package-installer (like Python's pip or virtualenv, or NodeJS' npm, or anything else that puts its files into a folder), you can win some time by doing it like this:

        - uses: actions/cache@v2
          id: cache-packages  # give it a name for checking the cache hit-or-not
          with:
            path: ./packages/  # what we cache: the folder
            key: ${{ runner.os }}-packages-${{ hashFiles('**/packages*.txt') }}
            restore-keys: |
              ${{ runner.os }}-packages-
        - run: package-installer packages.txt
          if: steps.cache-packages.outputs.cache-hit != 'true'
    

    So what's important here:

    1. We give this step a name, cache-packages
    2. Later, we use this name for conditional execution: if, steps.cache-packages.outputs.cache-hit != 'true'
    3. Give the cache action a path to the folder you want to cache: ./packages/
    4. Cache key: something that depends on the hash of your input files. That is, if any packages.txt file changes, the cache will be rebuilt.
    5. The second step, package installer, will only be run if there was no cache

    For users of virtualenv: if you need to activate some shell environment, you have to do it in every step. Like this:

    - run: . ./environment/activate && command
    

提交回复
热议问题