简体   繁体   English

如何将 Github 操作用户输入传递到 python 脚本

[英]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.我正在尝试将用户输入从 Github 操作传递给 python 脚本,但我似乎无法使其正常工作。

Here is my yml:这是我的 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:这是我的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?我在这里做错了什么,如何将 Python 打印出用户输入变量?

The problem occurs because you set the variable as INPUT_STORE in your workflow and extract as INPUT_MYINPUT in your python script.出现问题是因为您在工作流程中将变量设置为INPUT_STORE并在 python 脚本中提取为INPUT_MYINPUT 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: test.py文件:

import os

input_variable = os.environ['INPUT_STORE']

print("Input Variable:", input_variable)

Result using Test as input:使用Test作为输入的结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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