Search code examples
jsonpowershellgithub-actionsuser-inputfile-writing

How do we write GITHUB ACTIONS workflow_dispatch user inputs to a file and read it back


Below is the workflow that tries to first read all user inputs in one go and write to a file; later read back allInuts from the file using github actions powershell.

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"

Unfortunately, I get errors writing user inputs to a file as below:

Output:

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.

Note: I do not wish to write each user input individually as this solution should work for any user inputs for any workflow in the file.

Can you please suggest how I can?


Solution

  • Your GitHub Actions workflow file is pre-processing the inline PowerShell script and replacing ${{ toJson(github.event.inputs) }} with the json representation of github.event.inputs before it tries to execute the PowerShell:

    So,

    $Inputs = ${{ toJson(github.event.inputs) }}  | ConvertFrom-Json
    

    becomes

      $Inputs = {
        "input1": "Default Value 1",
        "input2": "Default Value 2",
        "input3": "Default Value 3"
      }  | ConvertFrom-Json
    

    but unfortunately, that's not valid PowerShell and it's giving you the error from your post when the Actions Runner tries to execute it:

    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.
    

    The actual PowerShell script you probably want to execute is this:

      $Inputs = @"
    {
        "input1": "Default Value 1",
        "input2": "Default Value 2",
        "input3": "Default Value 3"
    }
    "@ | ConvertFrom-Json
    

    which wraps the json text from the workflow in multi-line PowerShell "Here-String" delimiters before deserializing that back into in-memory objects with ConvertFrom-Json

    so working backwards, your GitHub Actions workflow needs to be this:

    $Inputs = @"
    ${{ toJson(github.event.inputs) }}
    "@ | ConvertFrom-Json