3

GitHub repository A contains a reusable GitHub action workflow where myscript is a bash script wrapper around a python dependency:

name: 'A'
[...]
runs:
  using: 'composite'
  steps:
    - uses: actions/setup-python@v4
      with:
        python-version: '3'
        cache: 'pip'
    - run: pip install -r requirements.txt
      shell: bash
    - run: myscript
      shell: bash

Now in repository B I reuse that action:

name: 'B'
on:
  workflow_dispatch:
  push:
    branches:
      - master

jobs:
  shacl:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Build and Validate
        uses: username/repoa@v1
        with: [...]

However now I get an error in the action log:

Run actions/setup-python@v4
  with:
    python-version: 3
    cache: pip
    check-latest: false
    token: ***
    update-environment: true
  


Successfully set up CPython (3.10.6)
Error: No file in /home/runner/work/ontology/ontology matched to [**/requirements.txt], make sure you have checked out the target repository

Now if this was only caused by the file being in the wrong repository this would be problematic enough, but the error even persists when completely removing any usage of requirements.txt:

[...]
runs:
  using: 'composite'
  steps:
    - uses: actions/setup-python@v4
      with:
        python-version: '3'
        cache: 'pip'
    - run: pip install mydependency
      shell: bash
[...]

The error will still occur, because setup-python will try to use requirements.txt for it's cache key and fail. There is a cache-dependency-path attribute for setup-python but I don't have any file I can point it to because all the files are in repository A but I have checked out repository B.

How can I use actions/setup-python in a reusable workflow with caching without getting this error?

Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118

1 Answers1

1

There were two separate problems in the workflow:

1. Local reference to a remote file

This was fixed by using the github.action_path to refer from repository B to the myscript file inside repository A:

- run: ${{ github.action_path }}/myscript
  shell: bash

2. Using actions/setup-python without a file to hash

The default dependency file requirements.txt can be overridden using cache-dependency-path. However this did not work in combination with ${{ github.action_path }} as the file could not be found:

# do not do this, it fails
- uses: actions/setup-python@v4
  with:
    python-version: '3'
    cache: 'pip'
    cache-dependency-path: "${{ github.action_path }}/requirements.txt" # file not found

This could be worked around by installing the dependency directly, disabling the actions/setup-python cache and using the generic cache action instead:

   - uses: actions/setup-python@v4
      with:
        python-version: '3'
    - uses: actions/cache@v3
      with:
        path: ~/.cache/pip
        key: ${{ runner.os }}-pip
    - run: pip install 'mydependency<2'
      shell: bash
    - run: ${{ github.action_path }}/myscript
      shell: bash
Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118