0

We are moving to GitHub actions (I'm new to actions) and would like to convert the current $json into environment name value pairs. I would like to extract and Name-Value and set/export as windows environment variables. Below is the extract and output of my code. Appreciate any help!

name: Check Build Properties JSON Var File
        id: read-json
        shell: pwsh
        run: |
          $jsonfilePath = Get-ChildItem ${{github.workspace }}\buildtools -Recurse | where {$_.name -match 'buildproperties.json'} |  % { $_.FullName }
          echo "The Value of the declared variable filepath is:- $jsonfilePath"
          #$json = Get-Content $jsonfilePath | ConvertFrom-Json
          $json = Get-Content $jsonfilePath -Raw
          $JsonParameters = ConvertFrom-Json -InputObject $json

This is the current output

the contents of the RAW JSON file from buildTools is: {
  "UPACKPATH": "C:\\Users\\builduser\\upack.exe",
  "NUGETPATH": "C:\\Users\\builduser\\Nuget.exe",
  "NugetPackageUrl": "https://api.nuget.org/v3/index.json",
  "SemVer": '1.0.0.0',
  "PackagePortal": false
}
mklement0
  • 382,024
  • 64
  • 607
  • 775
Princy
  • 613
  • 1
  • 6
  • 11
  • Relevant: https://stackoverflow.com/questions/75691648/how-to-inject-all-github-environment-specific-variables-from-vars-to-env-context – Azeem Jun 20 '23 at 14:45

1 Answers1

1

For this you might Iterate over PSObject properties and use the [Environment]::SetEnvironmentVariable method:

$Json = '{
  "UPACKPATH": "C:\\Users\\builduser\\upack.exe",
  "NUGETPATH": "C:\\Users\\builduser\\Nuget.exe",
  "NugetPackageUrl": "https://api.nuget.org/v3/index.json",
  "SemVer": "1.0.0.0",
  "PackagePortal": false
}'
$Parameters = Convertfrom-Json $Json
$Parameters.PSObject.Properties |Foreach-Object {
    [Environment]::SetEnvironmentVariable($_.Name, $_.Value)
}
iRon
  • 20,463
  • 10
  • 53
  • 79