1

How can I save the output of a Python function in mine GitHub Action code?

def example():
    return "a"


if __name__ == "__main__":
    example()

I tried to save to a variable, output and environment variable but it does not work. It only saves if I print something in the function.

name: "Check Renamed files"
"on":
  pull_request:
    branches:
      - main

jobs:
  prose:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: 3.8

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
      - name: Check renamed files
        run: |
          INPUT_STORE=$(python3 test.py)
          echo $INPUT_STORE

Also, tried this for multi-line output:

          MY_STRING="{$(python test.py }})} EOF"
          echo "MY_STRING<<EOF" >> $GITHUB_ENV
          echo "$MY_STRING" >> $GITHUB_ENV

But nothing worked

may
  • 1,073
  • 4
  • 14
  • 31
  • 1
    Would it be an option to save the value as env variable or output variable? If yes, I suggest to check the answer [here](https://stackoverflow.com/questions/74125082/new-format-of-output-in-custom-github-actions-since-set-output-is-going-to-be-de/74126650#74126650) – GuiFalourd Nov 26 '22 at 21:20
  • @GuiFalourd I still don't get it why write to a file to get this output. – may Nov 27 '22 at 13:13
  • 1
    It's the way the github workflow works to store env and output variables. You can get more informations here: https://docs.github.com/en/actions/learn-github-actions/environment-variables – GuiFalourd Nov 27 '22 at 18:57

1 Answers1

0

You could set it has a environment variable.

def example():
    return "a"


if __name__ == "__main__":
    print(example())

and then run:

python3 test.py

will print to your console:

"a"

In your Gitlab Actions, I would expect something like:

name: GitHub Actions Demo
run-name: ${{ github.actor }} is testing out GitHub Actions 
on: [push]
jobs:
  Explore-GitHub-Actions:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: 3.8
      - name: Print from test.py
        run: |
           export INPUT_STORE=$(python test.py)
           echo "Access direct: "
           echo $INPUT_STORE

And the result:

result

André Guerra
  • 486
  • 7
  • 22
  • Sadly, this does not work. - name: display string run: | echo "Access direct: " echo $INPUT_STORE echo "The string is: ${{ env.INPUT_STORE }}" – may Nov 26 '22 at 20:56
  • I run but I can not retrieve the environment variable – may Nov 26 '22 at 21:14
  • This does not work, but thank you for helping. I tried above suggestions. – may Nov 27 '22 at 13:13
  • I added the `.github/workflows' file to the question. This definitely works and I make a screenshot to the result. – André Guerra Nov 27 '22 at 13:38
  • This will only work if you print the output. I do not want to consider output print because in case someone adds a debug printer, this will break the workflow. – may Nov 27 '22 at 16:17