Search code examples
github-actionsbuilding-github-actionsgithub-actions-reusable-workflows

Github Composite Action to run Python script in repo


I have a Python script that scrapes files to search for a regex pattern. I would like to run this script using a Github Action. Furthermore, I want this same action and script to be used in several private GH repos without copying/pasting anything.

I have created a repo with a composite GHA

composite-actions-repo
|.
├── action.yml
└── main.py
### action.yml

name: 'RegEx check'
description: 'Scans files for regex pattern'
inputs:
  python_version:
    description: |
      Python Version
      Default is '3.10'.
    default: '3.10'
runs:
  using: "composite"
  steps:
    - id: setup-python
      name: setup python
      uses: actions/setup-python@v4
      with:
        python-version: ${{ inputs.python_version }}
    - id: regex-check
      name: Find regex pattern
      shell: bash
      run: python main.py

I want the main.py script checks contents of all files in the repo that is running action for a regex pattern.

There are several private repos I want to run this composite action and I'm trying to use this workflow:

### a-private-repo/.github/workflows/main.yml

name: CI

on: [push]

jobs:
  py_job:
    runs-on: ubuntu-latest
    name: Check for RegEx
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: execute py script # run main.py
        uses: company/composite-actions-repo@v1

I'm currently getting an error in GHA logs that it can't find the main.py file

python: can't open file '/home/runner/work/a-private-repo/a-private-repo/main.py': [Errno 2] No such file or directory
Error: Process completed with exit code 2.

The thing I can't wrap my head around is how to reference the main.py in composite-actions-repo from a-private-repo and at the same time use main.py to scan the files in a-private-repo.


Solution

  • Per GuiFalourd's comment, I just had to add ${{ github.action_path }} in action.yml

    name: 'RegEx check'
    description: 'Scans files for regex pattern'
    inputs:
      python_version:
        description: |
          Python Version
          Default is '3.10'.
        default: '3.10'
    runs:
      using: "composite"
      steps:
        - id: setup-python
          name: setup python
          uses: actions/setup-python@v4
          with:
            python-version: ${{ inputs.python_version }}
        - id: regex-check
          name: Find regex pattern
          shell: bash
          run: python ${{ github.action_path }}/main.py