powershell - 使用 Powershell 将变量值传递到 Jenkins 文件中的不同本地范围

标签 powershell jenkins-pipeline jenkins-groovy

我正在开发 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 完成时,所有变量都会被删除。

解决方案:

  1. 创建一个名为 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
    
  2. 将您的 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'
      }
    }
    

关于powershell - 使用 Powershell 将变量值传递到 Jenkins 文件中的不同本地范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52019758/

相关文章:

powershell - CMD:如何在 cmd 中转义此 powershell 命令?

docker - 运行 Jenkins Pipeline Slave 时如何升级 docker 服务器版本

jenkins - 如何限制并行运行的某些管道的数量?

jenkins - 如何在 Jenkins 管道中并行运行 for 循环的每次迭代?

jenkins - 事件选择参数中的通用 Groovy 脚本

powershell - char[] 和 char 有什么区别?

azure - 使用 powershell 在 Azure VM 上启动和停止 Windows 服务

docker - 如何将 jenkins 管道作业链接到 docker 从属容器上的标签

jenkins - 从变量 Groovy 获取 YAML 值

powershell - system() 在 vi​​m 中使用 powershell