I have a GitHub Actions workflow that is triggered when a pull request is created to dev. It downloads some pip dependencies and lints the project using PyLint. In this action, it looks for a cache of the pip dependencies. If it is found, it is used to download the requirements. After the pull request is merged, I have a post merge workflow that is triggered. This workflow has an action that will cache the pip dependencies on the branch dev if it doesn't already exist. Therefore, any subsequent pull request to dev workflows should have access to this cache.
I had the same intuition as this question here and very closely followed to try and emulate what they had but was unable to produce the same results. Another question I came across was this which recommends the same thing as what I'm trying to do below. Initially, I saw that the cache was being installed to the specific pull request i.e. refs/pull/.../merge
and I understood that this was why no subsequent pull requests had access to the cache. The pull request cache action looks as such:
pylint-backend:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Cache pip packages
id: cache-pip
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Checking existence of pip cache
run: echo "Cache exists ${{ steps.cache-pip.outputs.cache-hit }}"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r ./back-end/requirements.txt
pip install pylint
To get around this, I read through the cache documentation and this question which said that a feature branch would have access to the caches on the base branch. Therefore, I included a post merge to dev action which would create this cache on the dev branch. This action looks as such:
create-pip-cache:
runs-on: ubuntu-latest
steps:
- name: Checkout the code
uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Creating the cache, won't create if it exists
id: cache-pip
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Installing dependencies if the cache doesn't hit
if: ${{ steps.cache-pip.outputs.cache-hit != 'true' }}
run: pip install -r ./back-end/clientdek/requirements.txt
Now, after both workflows have run, as expected, the pull request workflow couldn't find the cache because it did not exist on dev yet. I merged the branch and the post merge action ran. Now, the cache DOES exist on dev. I created another pull request to dev from another branch. The cache action in the pull request workflow looks for the exact same cache with the exact same hash in its key but still says that it was not found. Why is this so ? I've verified that the cache with that exact key exists on dev, so why does a workflow being triggered by a pull request from a child branch of dev unable to find a cache on dev if the documentation says that it can ? My guess is that any push workflow to my child branch will have access to the dev cache but a pull request is running in a different environment and hence, does not have access.