使用 Powershell 将变量值传递到 Jenkins 文件中的不同本地范围
Passing a variable value to different local scopes within a Jenkins file using Powershell
我正在开发 Jenkins 文件以创建测试管道。
我一直在努力寻找解决以下问题的方法:
在 Jenkins 文件中,我添加了一个阶段,我想在测试完成后发布 Nunit 报告,但是对于每个 运行 都会创建一个标有日期和时间的文件夹,因此它重要的是我总是从列表中选择最后一个文件夹。我的问题是我正在使用 powershell 命令检索最后创建的文件夹的名称,并在特定目录路径中执行此命令,如下所示:
stage('Publish NUnit Test Report'){
dir('C:\Jenkins\workspace\QA-Test-Pipeline\iGCAutomation.Runner\Reports') {
powershell 'echo "Set directory"'
powershell 'New-Variable -Name "testFile" -Value (gci|sort LastWriteTime|select -last 1).Name -Scope global'
powershell 'Get-Variable -Name "testFile"'
}
testFile = powershell 'Get-Variable -Name "testFile"'
dir("C:\Jenkins\workspace\QA-Test-Pipeline\iGCAutomation.Runner\Reports\" + testFile + "\") {
powershell 'Get-Location'
powershell 'copy-item "TestResultNUnit3.xml" -destination "C:\Jenkins\workspace\QA-Test-Pipeline\iGCAutomation.Runner\Reports\NUnitXmlReport" -force'
}
dir('C:\Jenkins\workspace\QA-Test-Pipeline\iGCAutomation.Runner\Reports\NUnitXmlReport'){
nunit testResultsPattern: 'TestResultNUnit3.xml'
}
}
正如您所注意到的,我正在尝试创建一个名为 'testFile' 的新变量,它保存最后一个文件夹名称的值,但是当我转到脚本的下一部分时,这需要更改再次进入目录,未创建 testfile 变量,并且在尝试检索其值时抛出异常。
我想做的就是获取最后创建的文件夹的名称并将其传递给脚本的这一部分,以便更改为新的目录路径。
dir("C:\Jenkins\workspace\QA-Test-Pipeline\iGCAutomation.Runner\Reports\" + testFile + "\")
我在网上尝试了很多解决方案,但似乎没有任何效果。 Groovy 沙箱中的 Powershell 并不总是像我预期的那样工作。
而不是 运行 powershell
多次,将所有脚本连接在一起并只执行一次 powershell
。每次 powershell
完成后,所有变量都会被删除。
解决方案:
创建名为 Copy-NUnitResults.ps1
的文件:
# Filename: Copy-NUnitResults.ps1
$reportsSrcDir = 'C:\Jenkins\workspace\QA-Test-Pipeline\iGCAutomation.Runner\Reports'
$reportDestDir = 'C:\Jenkins\workspace\QA-Test-Pipeline\iGCAutomation.Runner\Reports\NUnitXmlReport'
Push-Location $reportsSrcDir
$testFile = (gci|sort LastWriteTime|select -last 1).Name
Pop-Location
Push-Location "$reportsSrcDir$testFile"
Copy-Item TestResultNUnit3.xml -Destination $reportDest -Force
Pop-Location
将您的 Jenkins 步骤修改为如下所示
stage('Publish NUnit Test Report'){
# you may need to put in the full path to Copy-NUnitResults.ps1
# e.g. powershell C:\Jenkins\Copy-NUnitResults.ps1
powershell .\Copy-NUnitResults.ps1
dir('C:\Jenkins\workspace\QA-Test-Pipeline\iGCAutomation.Runner\Reports\NUnitXmlReport'){
nunit testResultsPattern: 'TestResultNUnit3.xml'
}
}
我正在开发 Jenkins 文件以创建测试管道。
我一直在努力寻找解决以下问题的方法:
在 Jenkins 文件中,我添加了一个阶段,我想在测试完成后发布 Nunit 报告,但是对于每个 运行 都会创建一个标有日期和时间的文件夹,因此它重要的是我总是从列表中选择最后一个文件夹。我的问题是我正在使用 powershell 命令检索最后创建的文件夹的名称,并在特定目录路径中执行此命令,如下所示:
stage('Publish NUnit Test Report'){
dir('C:\Jenkins\workspace\QA-Test-Pipeline\iGCAutomation.Runner\Reports') {
powershell 'echo "Set directory"'
powershell 'New-Variable -Name "testFile" -Value (gci|sort LastWriteTime|select -last 1).Name -Scope global'
powershell 'Get-Variable -Name "testFile"'
}
testFile = powershell 'Get-Variable -Name "testFile"'
dir("C:\Jenkins\workspace\QA-Test-Pipeline\iGCAutomation.Runner\Reports\" + testFile + "\") {
powershell 'Get-Location'
powershell 'copy-item "TestResultNUnit3.xml" -destination "C:\Jenkins\workspace\QA-Test-Pipeline\iGCAutomation.Runner\Reports\NUnitXmlReport" -force'
}
dir('C:\Jenkins\workspace\QA-Test-Pipeline\iGCAutomation.Runner\Reports\NUnitXmlReport'){
nunit testResultsPattern: 'TestResultNUnit3.xml'
}
}
正如您所注意到的,我正在尝试创建一个名为 'testFile' 的新变量,它保存最后一个文件夹名称的值,但是当我转到脚本的下一部分时,这需要更改再次进入目录,未创建 testfile 变量,并且在尝试检索其值时抛出异常。
我想做的就是获取最后创建的文件夹的名称并将其传递给脚本的这一部分,以便更改为新的目录路径。
dir("C:\Jenkins\workspace\QA-Test-Pipeline\iGCAutomation.Runner\Reports\" + testFile + "\")
我在网上尝试了很多解决方案,但似乎没有任何效果。 Groovy 沙箱中的 Powershell 并不总是像我预期的那样工作。
而不是 运行 powershell
多次,将所有脚本连接在一起并只执行一次 powershell
。每次 powershell
完成后,所有变量都会被删除。
解决方案:
创建名为
Copy-NUnitResults.ps1
的文件:# Filename: Copy-NUnitResults.ps1 $reportsSrcDir = 'C:\Jenkins\workspace\QA-Test-Pipeline\iGCAutomation.Runner\Reports' $reportDestDir = 'C:\Jenkins\workspace\QA-Test-Pipeline\iGCAutomation.Runner\Reports\NUnitXmlReport' Push-Location $reportsSrcDir $testFile = (gci|sort LastWriteTime|select -last 1).Name Pop-Location Push-Location "$reportsSrcDir$testFile" Copy-Item TestResultNUnit3.xml -Destination $reportDest -Force Pop-Location
将您的 Jenkins 步骤修改为如下所示
stage('Publish NUnit Test Report'){ # you may need to put in the full path to Copy-NUnitResults.ps1 # e.g. powershell C:\Jenkins\Copy-NUnitResults.ps1 powershell .\Copy-NUnitResults.ps1 dir('C:\Jenkins\workspace\QA-Test-Pipeline\iGCAutomation.Runner\Reports\NUnitXmlReport'){ nunit testResultsPattern: 'TestResultNUnit3.xml' } }