Search code examples
pythongithub-actions

How to pass Github Actions user input into a python script


I'm trying to pass a user input from a Github Action to a python script and I can't seem to get it working properly.

Here is my yml:

name: Test Python Input
on:
  workflow_dispatch:
    inputs:
      myInput:
        description: 'User Input Here'
        required: true

jobs:
  run-python-test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/[email protected]
      - name: Setup Python
        uses: actions/[email protected]
        with:
          python-version: 3.8
      - name: Execute Test Script
        run: |
          echo "Store: ${{ github.event.inputs.myInput }}"
          INPUT_STORE=${{ github.event.inputs.myInput }} python3 test.py

And here is my test.py:

import os
inputvariable = os.environ['INPUT_MYINPUT']
print(inputvariable)
print('Hello World!')

What am I doing wrong here and how can I put Python to print out the user input variable?


Solution

  • The problem occurs because you set the variable as INPUT_STORE in your workflow and extract as INPUT_MYINPUT in your python script. Use the same variable and it should work.

    I made it work like this:

    Workflow file:

    name: Test Python Input
    
    on:
       workflow_dispatch:
         inputs:
           myInput:
             description: 'User Input:'
             required: true
             default: "Hello World"
    
    jobs:
      run-python-test:
       runs-on: ubuntu-latest
        steps:
      
      - name: Checkout
        uses: actions/[email protected]
      
      - name: Setup Python
        uses: actions/[email protected]
        with:
          python-version: 3.8
      
      - name: Execute Test Script
        run: |
          echo "Store: ${{ github.event.inputs.myInput }}"
          INPUT_STORE=${{ github.event.inputs.myInput }} python3 test.py
    

    test.py file:

    import os
    
    input_variable = os.environ['INPUT_STORE']
    
    print("Input Variable:", input_variable)
    

    Result using Test as input:

    enter image description here