Search code examples
powershellgitlabyamlpipeline

Make the pipeline fail if a specific sentence appears in the log – Find a substring, but ignore a specific substring


In my first question, the goal was to intentionally make the CI/CD pipeline fail if a transformation is not possible in the Web.Live.Config. I had to save the build in a variable. If a transformation is not possible, the string "No element in the source document matches" appears in the log. But: Although there is no key with "connectionString" in the config files, strangely the string "No element in the source document matches '/configuration/connectionStrings'" appears. I wanted to ignore it.

Can someone help me search the log for a string but ignore the "No element in the source document matches '/configuration/connectionStrings'"? I tried -ne, -notmatch and -notlike, but it didn't help.

Even though the string " ... warning : No element in the source document matches '/configuration/appSettings/add[@key='anotherKEY']'" appears, the pipeline does not fail.

I'm new to PowerShell, and I would appreciate your assistance.

-  echo "Verifying build…"
- |
 $msbuildOutput = & $MSBUILD_PATH "$Our_ASMX_PROJECT_PATH" /Target:Rebuild /property:Configuration=Live /T:Package 
 $pattern = "No element in the source document matches"
 $ignorePattern = "No element in the source document matches '/configuration/connectionStrings'" # ignore the specific message
 $relevantMatches = $pattern | Where-Object { $_ -notmatch $ignorePattern }
 if ($relevantMatches)
 {
    Write-Host "Build failed due to missing configuration key or transformation."
    exit 1
 }

Another attempt:

-  echo "Verifying build…"
- |
 $msbuildOutput = & $MSBUILD_PATH "$Our_ASMX_PROJECT_PATH" /Target:Rebuild /property:Configuration=Live /T:Package 
 $pattern = "No element in the source document matches"
 $ignorePattern = "No element in the source document matches '/configuration/connectionStrings'" # ignore the specific message
 $matches = $msbuildOutput | Select-String -Pattern $pattern | ForEach-Object { $_.Line }
 $relevantMatches = $matches | Where-Object { $_.Line -notlike "*$ignorePattern*" }
 if ($relevantMatches)
 {
    Write-Host "Build failed due to missing configuration key or transformation."
    exit 1
 }

Attempt 7/22/2024

- |
 Write-Host "Verifying build…"
 $msbuildOutput = & $MSBUILD_PATH "$Our_ASMX_PROJECT_PATH" /Target:Rebuild /property:Configuration=Demo /T:Package 
 $pattern = "No element in the source document matches"
 $IgnorePattern = "No element in the source document matches '/configuration/connectionStrings'"
 # Split the long string into an array of lines
 $msbuildOutputLines = $msbuildOutput -split "(\r\n)"
 $errors = $msbuildOutputLines | Where-Object { $_ -like "*$pattern*" -and $_ -notlike "*$IgnorePattern*" }
 if ($errors)
 {
   Write-Host "Build failed due to missing configuration key or transformation."
   exit 1
 }

07/26/2024 Output (anonymised, excerpt)

''
''
'"D:\gitlab-runner-ps\builds\BuildNumber\0\aNamespace\anotherNamespace\Project\OurDirectory\ProjectName.vbproj" (Rebuild;Package Ziel) (1) ->'
'"D:\gitlab-runner-ps\builds\BuildNumber\0\aNamespace\anotherNamespace\Project\AnotherDirectory\AnotherProjectName.csproj" (Standardziel) (3:3) ->'
'"D:\gitlab-runner-ps\builds\BuildNumber\0\aNamespace\anotherNamespace\Project\EvenAnotherDir\EvenAnotherProjectName" (Standardziel) (13:5) ->'
'  D:\gitlab-runner-ps\builds\BuildNumber\0\aNamespace\anotherNamespace\Project\AnotherDir\FileName.cs(72,30): warning CS0168: Die Variable "e" ist deklariert, wird aber nie verwendet. [D:\gitlab-runner-ps\builds\BuildNumber\0\aNamespace\anotherNamespace\Project\AnotherDir\ProjectName.csproj]'

Edit 07/29/2024 showing errors

Showing errors:
C:\Windows\TEMP\build_scriptSomeNumber\script.ps1 :
D:\gitlab-runner-ps\builds\AnotherNumber\0\aNamespace\aNamespace\Project\OurProjectPath\Web.Live.config(204,10): warning : No element in the source document matches '/configuration/appSettings/add[@key='THATKEY']'
[D:\gitlab-runner-ps\builds\AnotherNumber\0\aNamespace\aNamespace\Project\Namespace\OurProjectPath.vbproj]
D:\gitlab-runner-ps\builds\AnotherNumber\0\aNamespace\aNamespace\Project\OurCompany.ANamespace.Project.AsmxService\Web.Live.config(204,10): warning : No element in the source document matches '/configuration/appSettings/add[@key='THATKEY']' 
[D:\gitlab-runnerps\builds\AnotherNumber\0\aNamespace\aNamespace\Project\OurCompany.ANamespace.Project.AsmxService\OurProjectPath.vbproj]
In Zeile:1 Zeichen:1
+ C:\Windows\TEMP\build_scriptSomeNumber\script.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,script.ps1


Solution

  • Assuming each warning will be in a separate line you can use this:

    $MsBuildOutput = & $MSBUILD_PATH "$Our_ASMX_PROJECT_PATH" /Target:Rebuild /property:Configuration=Live /T:Package 
    $pattern = "No element in the source document matches"
    $IgnorePattern = "No element in the source document matches '/configuration/connectionStrings'"
    $errors = $MsBuildOutput -split "`n" | where { $_ -notlike "*$IgnorePattern*" -and $_ -like "*$pattern*" }
    if ($errors) { Write-Error -ErrorAction Stop -Message "$errors" }
    

    This code assumes that each warning is on a separate line.