Has anyone run into a problem when using PowerShell in a Jenkins Pipeline, where when you try to pull in an environmental variable (e.g., $env:CHANGE_ID
), it evaluates to something like this?
org.jenkinsci.plugins.workflow.cps.EnvActionImpl@1b69f5bb:VARIABLE_NAME
It looks like someone else was running into the same problem in this question, but I'm not sure that it was answered there (they show how to print all environment variables, but not how to get a specific one when toString
is not implemented):
Retrieve all properties of env in Jenkinsfile
My Jenkins pipeline file:
pipeline {
agent {
node {
label 'jenkins_managed_windows'
}
}
stages {
stage('SonarQube Analysis') {
steps {
powershell "dotnet sonarscanner begin /k:project-key /d:sonar.branch.name=$env:BRANCH_NAME"
}
}
stage('Build') {
steps {
powershell 'dotnet build'
}
}
stage('SonarQube End') {
steps {
powershell 'dotnet sonarscanner end'
}
}
}
}
The step with the environment variable gets run as:
dotnet sonarscanner begin /k:project-key /d:sonar.branch.name=org.jenkinsci.plugins.workflow.cps.EnvActionImpl@54ee3a8:BRANCH_NAME
powershell 'dotnet sonarscanner begin /k:project-key /d:sonar.branch.name=$env:BRANCH_NAME'
It doesn't even evaluate the environment variable at all, and just gets run as:
dotnet sonarscanner begin /k:project-key /d:sonar.branch.name=$env:BRANCH_NAME