templates/t_build.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
$BuildFileContent = @"
# Include: Settings.
. './<module>.settings.ps1'
 
# Include: build_utils.
. './build_utils.ps1'
 
#Synopsis: Run/Publish Tests and Fail Build on Error.
task Test Clean, RunTests, ConfirmTestsPassed
 
#Synopsis: Run full Pipeline.
task . Clean, Analyze, Test, Publish
 
#Synopsis: Install dependencies.
task InstallDependencies {}
 
#Synopsis: Clean Artifact directory.
task Clean {
     
    if (Test-Path -Path `$Artifacts) {
        Remove-Item "`$Artifacts/*" -Recurse -Force
    }
 
    New-Item -ItemType Directory -Path `$Artifacts -Force
}
 
#Synopsis: Analyze code.
task Analyze {
    `$scriptAnalyzerParams = @{
        Path = `$ModulePath
        ExcludeRule = @('PSPossibleIncorrectComparisonWithNull', 'PSUseToExportFieldsInManifest')
        Severity = @('Error', 'Warning')
        Recurse = `$true
        Verbose = `$false
    }
 
    `$saResults = Invoke-ScriptAnalyzer @scriptAnalyzerParams
    # Save the results.
    `$saResults | ConvertTo-Json | Set-Content (Join-Path `$Artifacts "ScriptAnalysisResults.json")
}
 
#Synopsis: Run tests.
task RunTests {
    `$invokePesterParams = @{
        OutputFile = (Join-Path `$Artifacts "TestResults.xml")
        OutputFormat = "NUnitXml"
        Strict = `$true
        PassThru = `$true
        Verbose = `$false
        EnableExit = `$false
        CodeCoverage = (Get-ChildItem -Path "`$ModulePath\*.ps1" -Exclude "*.Tests.*" -Recurse).FullName
    }
 
    `$testResults = Invoke-Pester @invokePesterParams
 
    `$testResults | ConvertTo-Json -Depth 5 | Set-Content (Join-Path `$Artifacts "PesterResults.json")
}
 
#Synopsis: Confirm that tests passed.
task ConfirmTestsPassed {
    # Fail Build after reports are created, this allows CI to publish test results before failing
    [xml]`$xml = Get-Content (Join-Path `$Artifacts "TestResults.xml")
    `$numberFails = `$xml."test-results".failures
    assert(`$numberFails -eq 0) ('Failed "{0}" unit tests.' -f `$numberFails)
 
    # Fail Build if Coverage is under requirement
    `$json = Get-Content (Join-Path `$Artifacts "PesterResults.json") | ConvertFrom-Json
    `$overallCoverage = [Math]::Floor((`$json.CodeCoverage.NumberOfCommandsExecuted / `$json.CodeCoverage.NumberOfCommandsAnalyzed) * 100)
    assert(`$OverallCoverage -gt `$PercentCompliance) ('A Code Coverage of "{0}" does not meet the build requirement of "{1}"' -f `$overallCoverage, `$PercentCompliance)
}
 
#Synopsis: Publish to SMB File Share.
task Publish {
     
    `$moduleInfo = @{
        RepositoryName = `$Settings.SMBRepositoryName
        RepositoryPath = `$Settings.SMBRepositoryPath
        ModuleName = `$ModuleName
        ModulePath = (Join-Path `$ModulePath "`$ModuleName.psd1")
        BuildNumber = `$BuildNumber
    }
 
    Publish-PSModule @moduleInfo -Verbose
}
 
task PublishLocal {
 
    `$moduleInfo = @{
        Path = (Join-Path `$ModulePath "`$ModuleName.psd1")
        Repository = '<LocalRepository>'
    }
 
    Publish-Module @moduleInfo
}
"@