adoazure-pipelines-yaml

Not able to pass secret variable to script


When I mark pipeline variable (Pipeline -> Library) as secret, I cannot able to use it in my pipeline runs.

In Pipeline, I am trying to pass the secret variable to PowerShell script. But it always show as empty.

Refer this doc: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/set-secret-variables?view=azure-devops&tabs=yaml%2Cbash#use-a-secret-variable-in-the-ui

I have mapped the secret variable to Task environment. But it still shows as empty.

Powershell:

param (
$Secret = $Env:SECRET_VAR
)
...

Yaml Task:

- task: PowerShell@2
  displayName: 'Post Restquest'
  inputs:
    targetType: filePath
    filePath: ./PostRequest.ps1
  env:
     SECRET_VAR: $(token) 

Solution

  • Test the same YAML sample and I can reproduce the same issue.

    The cause of the issue can be that the prefix: SECRET of the environment variable name.

    If the mapped environment variable in task is using SECRET as prefix, the environment variable will show as empty value.

    To solve this issue, you need to change the environment name prefix(Avoid using SECRET).

    For example: Change SECRET_VAR -> MAPPED_VAR

    YAML sample:

    - task: PowerShell@2
      displayName: 'Post Restquest'
      inputs:
        targetType: filePath
        filePath: ./PostRequest.ps1
      env:
         MAPPED_VAR: $(token) 
    

    PowerShell script:

    param (
    $Secret = $Env:MAPPED_VAR
    )
    
    echo $Secret
    

    Result:

    enter image description here