简体   繁体   中英

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/checkout@v2.3.4
      - name: Setup Python
        uses: actions/setup-python@v2.2.2
        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?

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/checkout@v2.3.4
  
  - name: Setup Python
    uses: actions/setup-python@v2.2.2
    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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM