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.