Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass build variables to powerhshell inline script in Azure DevOps

I have set a build variable which I would like to consume in my inline powershell script.

I tried $($myvariable) but this comes out blank.

like image 593
kumar Avatar asked Jan 27 '19 17:01

kumar


People also ask

How do you pass variables between tasks in Azure DevOps?

Share variables between Tasks across the Jobs (of the same Stage) We need to use the isOutput=true flag when you desire to use the variable in another Task located in another Job. Navigate to Stage1_Job1_Task1 and add isoutput = true flag to the Logging Command which let's us to access the value outside the Job.

How do you use pipeline variables in PowerShell?

Windows PowerShell has a unique pipeline variable $_ or $PSItem . Windows PowerShell's ability to pipe entire objects from one command to another is needed to represent that object traversing the pipeline. When one Windows PowerShell cmdlet pipes something to another command, that cmdlet can send an object.

Why use PowerShell arguments in Azure DevOps build and release pipelines?

Sometimes there is a need to add PowerShell as one of the steps in these pipelines. Why? For instance to update content of the files from the repository or to use some Azure PowerShell cmdlets to make some updates. In this article I would like to present how to use PowerShell Arguments in the Azure DevOps build and release pipelines.

How to get the build variables with inline power shell script?

To get the build variables with inline power shell script, you can try to use following syntax $env:variable: Note:At this moment, the value of nested variables (like $ (myvariable$ (Build.SourceBranchName))) on build variables table are not yet supported in the build pipelines. Hope this helps.

Can I run script variables in PowerShell without azdo?

You can still define and manage “script variables” like $var = 123 in PowerShell and maintain environment variables without AzDo being involved. However, AzDo allows you to set and reference pipeline variables in scripts too. Throughout this article, you’ll see references to running “scripts.”

How do I run a PowerShell script in a PowerShell pipeline?

To run a PowerShell script in a pipeline requires using the PowerShell task. The PowerShell task takes a script or PowerShell code from the pipeline and runs it on a pipeline agent. Depending on the options chosen, the pipeline agent will either be on Windows or Linux.


3 Answers

Pass build variables to powerhshell inline script in Azure DevOps

To get the build variables with inline power shell script, you can try to use following syntax $env:variable:

$env:myvariable

enter image description here

The build variable:

enter image description here

The build result:

enter image description here

Note:At this moment, the value of nested variables (like $(myvariable$(Build.SourceBranchName))) on build variables table are not yet supported in the build pipelines.

Hope this helps.

like image 90
Leo Liu-MSFT Avatar answered Nov 03 '22 09:11

Leo Liu-MSFT


here's what's working for me:

$(variableName)

for example built-in variable called Build.BuildNumber can be accessed like so:

$(Build.BuildNumber)

full example with format function:

- task: AzurePowerShell@3
  displayName: UpdatePrereq
  inputs:
    azureSubscription: ${{ parameters.azureSubscription }}
    ScriptType: InlineScript
    Inline: |
        ${{ format('. $(Build.Repository.LocalPath)\scripts\_helpers.ps1
        Update-DeploymentPrereq -resourceGroup {1} -location {3}
        Update-Prereq -pathSuffix {0} -pathBase $(Build.Repository.LocalPath) -resourceGroup {1} -buildId $(Build.BuildNumber) -paramFile {2}
        Update-DeploymentConcurrency -resourceGroup {1} -buildId $(Build.BuildNumber)',
            parameters.buildDir, parameters.resourceGroupName, parameters.paramFile, parameters.location ) }}
    azurePowerShellVersion: LatestVersion
like image 42
4c74356b41 Avatar answered Nov 03 '22 10:11

4c74356b41


If you need to use nested variables you can use something like this:

$name = "CONNECTIONSTRING_$(Agent.MachineName)"
$val = [System.Environment]::GetEnvironmentVariable($name)

And then you get the value of the variable ConnectionString.MACHINENAME

like image 26
dave Avatar answered Nov 03 '22 10:11

dave