是否有人在Jenkins Pipeline中使用PowerShell时遇到问题,例如当您尝试获取环境变量(例如$env:CHANGE_ID
)时,它会评估为类似于这样的内容?
org.jenkinsci.plugins.workflow.cps.EnvActionImpl@1b69f5bb:VARIABLE_NAME
看起来有人在这个问题中遇到了类似的问题,但我不确定那里是否得到了答案(它们展示了如何打印所有环境变量,但没有展示当toString
未实现时如何获取特定环境变量):在Jenkinsfile中检索所有env属性
我的Jenkins流水线文件:
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'
}
}
}
}
"环境变量所在的步骤将以以下方式运行:"
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
powershell "dotnet sonarscanner begin /k:project-key /d:sonar.branch.name=$($env:BRANCH_NAME)"
。 - Benjamin HubbardWorkflowScript: 11: $符号后面出现非法字符串字符; 解决方案:要么转义文字中的$符号"\$5",要么将值表达式用大括号括起来"${5}" @行11,列3。
然后我尝试了:powershell "dotnet sonarscanner begin /k:project-key /d:sonar.branch.name=${env:BRANCH_NAME}"
,最终可以工作。 - olucafont6