我想在Jenkins管道中从文件运行Powershell脚本。
我已经阅读了这篇文章,其中介绍了如何运行输入到管道中的Powershell脚本,但我无法从文件运行脚本。
我尝试过:
powershell returnStatus: true, script: '-File build.ps1'
我尝试过各种组合,但是不知道如何做到这一点。
我想在Jenkins管道中从文件运行Powershell脚本。
我已经阅读了这篇文章,其中介绍了如何运行输入到管道中的Powershell脚本,但我无法从文件运行脚本。
我尝试过:
powershell returnStatus: true, script: '-File build.ps1'
我尝试过各种组合,但是不知道如何做到这一点。
很快就想通了,这行应该是这样的:
powershell returnStatus: true, script: '.\\build.ps1'
@bic的答案非常好,对我很有帮助。如果您需要传递变量,而不想在脚本内使用环境变量,我发现最好的方法是两行代码(声明式语法):
writeFile file: 'tempFile.ps1', text: "${libraryResource 'psScript1.ps1'}"
powershell './tempFile.ps1 -someArg $env:someArg'
所以你正在将脚本内容写入本地目录的临时文件。然后你直接使用powershell
调用脚本,并在调用中注入变量。
我同意在脚本中使用环境变量更方便。但需要注意以下几点模式:
environment
block can't be changed during the run. (At least that is my experience.)script
block in a step. I usually set them all up in my ('init')
stage. You should then be able to use and modify these vars anywhere in the pipe - and any sub-scripts that are spawned off of it. If you have a lot of env vars, i'd recommend shunting them off to an external groovy library.
pipeline {
...
stage('init') {
steps {
script {
env.myVariable = 'some value'
}
}
}
...
when
condition evaluations. Again, just my experience, i might be wrong.
stage('A') {
environment {
stageEnvVar = 'another value'
}
steps {
...
}
}
这是我发现的关于Jenkins环境变量的最佳参考资料:https://e.printstacktrace.blog/jenkins-pipeline-environment-variables-the-definitive-guide/
BUILD_NUMBER
等,以及在此之前设置的任何内容。请记住我在下面的答案中提到的环境变量作用域规则。 - Max Cascone