鉴于jenkins 2.1构建管道,jenkins将一个env变量注入到node{}.例如,BRANCH_NAME可以使用
node {
echo ${env.BRANCH_NAME}
...
Run Code Online (Sandbox Code Playgroud)
env属性.我正在寻找像这样的代码
node {
for(e in env){
echo e + " is " + ${e}
}
...
Run Code Online (Sandbox Code Playgroud)
这会像回应一样
BRANCH_NAME is myBranch2
CHANGE_ID is 44
...
Run Code Online (Sandbox Code Playgroud) 在Jenkins Pipeline中使用PowerShell时,是否有人遇到问题?当您尝试引入环境变量(例如$env:CHANGE_ID)时,它会得出类似的结果?
org.jenkinsci.plugins.workflow.cps.EnvActionImpl@1b69f5bb:VARIABLE_NAME
Run Code Online (Sandbox Code Playgroud)
看来其他人在这个问题上遇到了相同的问题,但是我不确定在那里是否得到了答案(他们展示了如何打印所有环境变量,但是没有展示如何在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'
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
包含环境变量的步骤运行为:
dotnet sonarscanner begin /k:project-key /d:sonar.branch.name=org.jenkinsci.plugins.workflow.cps.EnvActionImpl@54ee3a8:BRANCH_NAME
Run Code Online (Sandbox Code Playgroud)
powershell 'dotnet sonarscanner begin /k:project-key /d:sonar.branch.name=$env:BRANCH_NAME'
Run Code Online (Sandbox Code Playgroud)
它甚至根本不评估环境变量,而只是运行为:
dotnet sonarscanner begin /k:project-key /d:sonar.branch.name=$env:BRANCH_NAME
Run Code Online (Sandbox Code Playgroud)