Github Actions don't reuse cache
Question:
I have a pretty simple step for CI on Github Actions, which is supposed to cache Python dependencies, so it would save a lot of computing time.
some-step:
name: 'Test step'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: pipx install poetry
- name: Set up Python 3.8
uses: actions/setup-python@v4
with:
python-version: "3.8"
architecture: "x64"
cache: "poetry"
- name: Install dependencies
run: poetry install
- run: poetry run ...
Every time when I create a new PR, new cache is generated, even if dependencies didn’t change. As I found out it happens because of cache branch restrictions.
My question is how to create a common cache? Or how to remove branch restrictions?
I rarely have to rerun my actions, so this caching implementation doesn’t give any benefits.
Answers:
You get a new, completely new runner for every run, so you need to tell GitHub to store the cached files. You do so by using this action: https://github.com/actions/cache
Then use indeed this as a cache key: which will not change as long as that file does not change: poetry-${{ hashFiles(‘poetry.lock’) }}
With this action, the first run will generate the cache and upload them to GitHub. Then the next run will download the cache and the next steps can use the files from it.
I reused action (it’s really light and does a simple check quickly) and created a new workflow, which runs on lock file changes in master or could be run manually.
on:
push:
branches:
- master
paths:
- 'poetry.lock'
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: false
default: 'warning'
type: choice
options:
- info
- warning
- debug
jobs:
some-step:
name: 'Test step'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: pipx install poetry
- name: Set up Python 3.8
uses: actions/setup-python@v4
with:
python-version: "3.8"
architecture: "x64"
cache: "poetry"
- name: Install dependencies
run: poetry install
- run: poetry run ...
I have a pretty simple step for CI on Github Actions, which is supposed to cache Python dependencies, so it would save a lot of computing time.
some-step:
name: 'Test step'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: pipx install poetry
- name: Set up Python 3.8
uses: actions/setup-python@v4
with:
python-version: "3.8"
architecture: "x64"
cache: "poetry"
- name: Install dependencies
run: poetry install
- run: poetry run ...
Every time when I create a new PR, new cache is generated, even if dependencies didn’t change. As I found out it happens because of cache branch restrictions.
My question is how to create a common cache? Or how to remove branch restrictions?
I rarely have to rerun my actions, so this caching implementation doesn’t give any benefits.
You get a new, completely new runner for every run, so you need to tell GitHub to store the cached files. You do so by using this action: https://github.com/actions/cache
Then use indeed this as a cache key: which will not change as long as that file does not change: poetry-${{ hashFiles(‘poetry.lock’) }}
With this action, the first run will generate the cache and upload them to GitHub. Then the next run will download the cache and the next steps can use the files from it.
I reused action (it’s really light and does a simple check quickly) and created a new workflow, which runs on lock file changes in master or could be run manually.
on:
push:
branches:
- master
paths:
- 'poetry.lock'
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: false
default: 'warning'
type: choice
options:
- info
- warning
- debug
jobs:
some-step:
name: 'Test step'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: pipx install poetry
- name: Set up Python 3.8
uses: actions/setup-python@v4
with:
python-version: "3.8"
architecture: "x64"
cache: "poetry"
- name: Install dependencies
run: poetry install
- run: poetry run ...