Sherrylene Gauci
Sherrylene Gauci

Reputation: 83

Passing a variable value to different local scopes within a Jenkins file using Powershell

I am developing a Jenkins file in order to create a test pipeline.

I've been struggling trying to find a solution for the following:

In the Jenkins file,I've added a stage where I want to publish the Nunit report after the tests finish, however for every run a folder labelled with the date and time is created, so it is important that I always pick the last folder from the list. My issue is that I am using a powershell command to retrieve the name of the last folder created and executing this command in a specific directory path as follows:

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'
                    }
                }

As you can notice I am attempting to create a new variable called 'testFile' which holds the value of the last folder name, but when I go to the next part of the script, which requires the changing of the directory again, the testfile variable is not created and when attempting to retrieve its value an exception is thrown.

All I would like to do is get the name of the last folder created and pass it on to this part of the script, in order to change to a new directory path.

dir("C:\\Jenkins\\workspace\\QA-Test-Pipeline\\iGCAutomation.Runner\\Reports\\" + testFile + "\\")

I've tried a lot of solutions online but nothing seems to work. Powershell within the Groovy sandbox doesn't always work as I expect.

Upvotes: 3

Views: 598

Answers (1)

Chui Tey
Chui Tey

Reputation: 5554

Instead of running powershell multiple times, join all the scripts together and execute powershell just once. Each time powershell finishes, all the variables are deleted.

Solution:

  1. Create a file called 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. Modify your Jenkins step to look like this

    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'
      }
    }
    

Upvotes: 1

Related Questions