Home > database >  Unable to pass on Env variables to powershell script (not inline) in azure pipelines yaml
Unable to pass on Env variables to powershell script (not inline) in azure pipelines yaml

Time:12-11

My azure pipelines yaml runs a powershell script which is stored inside the repo.

That powershell script expects 3 variables : the working directory, the Oauth access token and the source branch name (which triggered the pipeline).

But it seems , whenever I try to pass on parameters, the powershell script does not recognize them and I get an error

The term 'env:SYSTEM_ACCESSTOKEN' is not recognized as the 
name of a cmdlet, function, script file, or operable program

The term 'env:BUILD_SOURCEBRANCHNAME' is not recognized as the 
name of a cmdlet, function, script file, or operable program

My yaml looks like this:

name: $(Build.DefinitionName)_$(Build.SourceBranchName)_$(Build.BuildId)

trigger:
  branches:
    include:
      - '*'

variables:
  system_accesstoken: $(System.AccessToken)

jobs:
  - job: NoteBookMergeAndOnPremSync
    displayName: Merge Notebooks to Notebooks branch and sync to on prem git
    pool:
      name: Poolname
    steps:
    - task: PowerShell@2
      displayName: 'Merge to Notebooks branch in Azure and Sync to On Prem'
      inputs:
        targetType: filePath
        filePath: ./deploy/MergeAndSync.ps1
        arguments: '-workingdir $(System.DefaultWorkingDirectory)\ -featurebranch $(env:BUILD_SOURCEBRANCHNAME) -accesstoken $(env:SYSTEM_ACCESSTOKEN)'
    

I was able to run the powershell script successfully as an "in line powershell script" when using a "release definition" using the GUI, but I would like all that to be in an azure pipeline (yaml)in yaml but unfortunately, I just can't find a way to pass on these env variables.

How do I pass on the BUILD_SOURCEBRANCHNAME and env:SYSTEM_ACCESSTOKEN to the powershell script from the azure pipeline yaml?

Also, I would like to avoid an "inline powershell script" and rather have the logic saved in my repo.

CodePudding user response:

It looks like you're mixing Azure macro syntax ($(name)) with PowerShell variable-reference syntax ($env:name, for environment variables).

That said, since you're invoking a script file with arguments - which presumably means that the -File parameter of the PowerShell CLI is used - you cannot reference environment variables in the arguments, because PowerShell then interprets something like $env:BUILD_SOURCEBRANCHNAME verbatim (as a literal string) rather than as an environment-variable reference (the latter would only work inside the script or in CLI calls using -Command).

Therefore, I presume the solution is to use only Azure macro syntax to pass the value of the variables of interest as arguments:

arguments: >-
  -workingdir $(System.DefaultWorkingDirectory)\
  -featurebranch $(Build.SourceBranchName)
  -accesstoken $(system_accesstoken)

Update: As you state, you didn't need any variables definition: referencing $(System.AccessToken) directly works too:

arguments: >-
  -workingdir $(System.DefaultWorkingDirectory)\
  -featurebranch $(Build.SourceBranchName)
  -accesstoken $(System.AccessToken)
  • Related