下面是尝试首先一次性读取所有用户输入并写入文件的工作流程;稍后使用 github actions powershell 从文件中读回 allInuts。
name: Redirect Inputs to File
on:
workflow_dispatch:
inputs:
input1:
description: 'Input 1'
required: true
default: 'Default Value 1'
input2:
description: 'Input 2'
required: true
default: 'Default Value 2'
input3:
description: 'Input 3'
required: true
default: 'Default Value 3'
jobs:
redirect-inputs:
runs-on: windows-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Run PowerShell script to dump inputs to file
shell: pwsh
run: |
# Convert inputs to PowerShell hashtable
$Inputs = ${{ toJson(github.event.inputs) }} | ConvertFrom-Json
#$Inputs = $env:INPUTS_JSON | ConvertFrom-Json
# Convert hashtable to JSON
$InputData = $Inputs | ConvertTo-Json
# Redirect inputs to a file
$InputData | Out-File -FilePath inputs.json
# Display the content of the file for verification
Get-Content -Path inputs.json
- name: Run PowerShell script to read inputs from file
shell: pwsh
run: |
# Read inputs from inputs.json file
$InputData = Get-Content -Path inputs.json | ConvertFrom-Json
# Assign variables from input data
$Input1 = $InputData.input1
$Input2 = $InputData.input2
$Input3 = $InputData.input3
# Display the values of variables for verification
Write-Output "Input1: $Input1"
Write-Output "Input2: $Input2"
Write-Output "Input3: $Input3"
不幸的是,我在将用户输入写入文件时遇到错误,如下所示:
输出:
1s
Run # Convert inputs to PowerShell hashtable
# Convert inputs to PowerShell hashtable
$Inputs = {
"input1": "Default Value 1",
"input2": "Default Value 2",
"input3": "Default Value 3"
} | ConvertFrom-Json
#$Inputs = $env:INPUTS_JSON | ConvertFrom-Json
# Convert hashtable to JSON
$InputData = $Inputs | ConvertTo-Json
# Redirect inputs to a file
$InputData | Out-File -FilePath inputs.json
# Display the content of the file for verification
Get-Content -Path inputs.json
shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
ParserError: D:\a\_temp\e6cfac29-49f5-49da-ba05-c1025775e5a1.ps1:4
Line |
4 | "input1": "Default Value 1",
| ~
| Unexpected token ':' in expression or statement.
Error: Process completed with exit code 1.
注意:我不希望单独编写每个用户输入,因为此解决方案应该适用于文件中任何工作流程的任何用户输入。
你能建议我该怎么做吗?
您的 GitHub Actions 工作流程文件正在预处理内联 PowerShell 脚本,并在尝试执行 PowerShell 之前将
${{ toJson(github.event.inputs) }}
替换为 github.event.inputs
的 json 表示形式:
$Inputs = ${{ toJson(github.event.inputs) }} | ConvertFrom-Json
不幸的是,生成的 PowerShell 无效:
$Inputs = {
"input1": "Default Value 1",
"input2": "Default Value 2",
"input3": "Default Value 3"
} | ConvertFrom-Json
这给了你帖子中的错误:
ParserError: D:\a\_temp\e6cfac29-49f5-49da-ba05-c1025775e5a1.ps1:4
Line |
4 | "input1": "Default Value 1",
| ~
| Unexpected token ':' in expression or statement.
Error: Process completed with exit code 1.
您可能想要的最终 PowerShell 脚本是这样的:
$Inputs = @"
{
"input1": "Default Value 1",
"input2": "Default Value 2",
"input3": "Default Value 3"
}
"@ | ConvertFrom-Json
所以逆向工作,您的 GitHub Actions 工作流程需要是这样的:
$Inputs = @"
${{ toJson(github.event.inputs) }}
"@ | ConvertFrom-Json