send multiple parameter to Azure-Devops pipeline job via Powershell

你说的曾经没有我的故事 提交于 2021-01-29 20:52:28

问题


I am able to kick off an Azure Devops Build job and send a text parameter but I only seem to be able to send a single parameter and not more. I need to send a token parameter Ok that works but I also want to send a text param that contains a json payload to be processed by a Powershell script in the Build job. So I have a Hellow World definition setup with two variables in Pipeline variables. First one is tokentext the second one is jsonInput. Both have the checkbox "Settable at queue time" checked. I have a PowerShell Inline script in the job definition with this:

Write-Host "Hello World"
Write-Host "tokentext: $(tokentext) `n"

Write-Host "Json Input"
Write-Host "---------------------------------------------------"
$(jsonInput)

My Body that I am sending to the Invoke-RestMethod is:

$body = @{ 
definition = @{
    id = $buildDefID
}    
parameters = "{`"tokentext`" :$mytoken}
              {`"jsonInput`" :$j}  
"
}
$b = $body  | ConvertTo-Json

This works but the above does not:

$body = @{ 
definition = @{
    id = $buildDefID
}    
parameters = "{`"tokentext`" :$mytoken}
"
}

$b = $body  | ConvertTo-Json

I have tried it with a comma separating the parameters etc.. All kids of things I have tried. I could sure use some assistance if anyone is sening multiple parameters to variables in a build Definition from a script.

Thanks


回答1:


Below request body works for me. Please check it out.

$body = @{
  definition= @{id = $buildDefID};

  parameters ="{`"jsonInput`":`"$jsonInput`", `"tokentext`":`"$tokentext`"}"
 }
$b = $body  | ConvertTo-Json

You can also run your pipeline via Runs - Run Pipeline rest api which is less complicated.

POST https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=6.1-preview.1

pipelineId is the $buildDefID

You can put the parameters in your request body as below:

 $body=@{
     
     variables = @{
 
         jsonInput= @{value = $jsonInput};
     
         tokentext= @{value = $tokentext}
      }
  }

$b = $body  | ConvertTo-Json



回答2:


Please try this:

$body = @{ 
  definition = @{
    id = $buildDefID
  }    
  parameters = "{`"tokentext`" :$mytoken, `"jsonInput`" :$j}"
}
$b = $body  | ConvertTo-Json


来源:https://stackoverflow.com/questions/64213867/send-multiple-parameter-to-azure-devops-pipeline-job-via-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!