Skip to content

Commit

Permalink
Define InvokeBuild-based CI pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuBuisson committed Sep 12, 2017
1 parent c7162d6 commit 7fb2803
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 34 deletions.
33 changes: 33 additions & 0 deletions PSGithubSearch.BuildSettings.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This file stores variables which are used by the build script

# Storing all values in a single $Settings variable to make it obvious that the values are coming from this BuildSettings file when accessing them.
$Settings = @{

Dependency = @('Coveralls','Pester','PsScriptAnalyzer','PSCodeHealth')
CoverallsKey = $env:Coveralls_Key
Branch = $env:APPVEYOR_REPO_BRANCH
TestUploadUrl = "https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)"
Version = $env:APPVEYOR_BUILD_VERSION
ManifestPath = '{0}\{1}\{1}.psd1' -f $PSScriptRoot, $env:APPVEYOR_PROJECT_NAME
VersionRegex = "ModuleVersion\s=\s'(?<ModuleVersion>\S+)'" -as [regex]

UnitTestParams = @{
Script = '.\Tests\Unit'
CodeCoverage = (Get-ChildItem -Path '.\PSGithubSearch\PSGithubSearch.psm1').FullName
OutputFile = "$PSScriptRoot\BuildOutput\UnitTestsResult.xml"
PassThru = $True
}

AnalyzeParams = @{
Path = "$PSScriptRoot\$($env:APPVEYOR_PROJECT_NAME)"
Severity = 'Error'
Recurse = $True
}

GitHubKey = $env:GitHub_Key
Email = 'MathieuBuisson@users.noreply.github.com'
Name = 'Mathieu Buisson'
BuildOutput = "$PSScriptRoot\BuildOutput"
SourceFolder = "$PSScriptRoot\$($env:APPVEYOR_PROJECT_NAME)"
OutputModulePath = "$PSScriptRoot\BuildOutput\$($env:APPVEYOR_PROJECT_NAME)"
}
145 changes: 145 additions & 0 deletions PSGithubSearch.build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#Requires -Modules 'InvokeBuild'

# Importing all build settings into the current scope
. '.\PSGithubSearch.BuildSettings.ps1'

Function Write-TaskBanner ( [string]$TaskName ) {
"`n" + ('-' * 79) + "`n" + "`t`t`t $($TaskName.ToUpper()) `n" + ('-' * 79) + "`n"
}

task Clean {
Write-TaskBanner -TaskName $Task.Name

If (Test-Path -Path $Settings.BuildOutput) {
"Removing existing files and folders in $($Settings.BuildOutput)\"
Get-ChildItem $Settings.BuildOutput | Remove-Item -Force -Recurse
}
Else {
"$($Settings.BuildOutput) is not present, nothing to clean up."
$Null = New-Item -ItemType Directory -Path $Settings.BuildOutput
}
}

task Install_Dependencies {
Write-TaskBanner -TaskName $Task.Name

Foreach ( $Depend in $Settings.Dependency ) {
"Installing build dependency : $Depend"
If ( $Depend -eq 'Selenium.WebDriver' ) {
Install-Package $Depend -Source nuget.org -Force
}
Else {
Install-Module $Depend -Scope CurrentUser -Force -SkipPublisherCheck
Import-Module $Depend -Force
}
}
}

task Unit_Tests {
Write-TaskBanner -TaskName $Task.Name

$UnitTestSettings = $Settings.UnitTestParams
$Script:UnitTestsResult = Invoke-Pester @UnitTestSettings
}

task Fail_If_Failed_Unit_Test {
Write-TaskBanner -TaskName $Task.Name

$FailureMessage = '{0} Unit test(s) failed. Aborting build' -f $UnitTestsResult.FailedCount
assert ($UnitTestsResult.FailedCount -eq 0) $FailureMessage
}

task Publish_Unit_Tests_Coverage {
Write-TaskBanner -TaskName $Task.Name

$Coverage = Format-Coverage -PesterResults $UnitTestsResult -CoverallsApiToken $Settings.CoverallsKey -BranchName $Settings.Branch
Publish-Coverage -Coverage $Coverage
}

task Upload_Test_Results_To_AppVeyor {
Write-TaskBanner -TaskName $Task.Name

$TestResultFiles = (Get-ChildItem -Path $Settings.BuildOutput -Filter '*TestsResult.xml').FullName
Foreach ( $TestResultFile in $TestResultFiles ) {
"Uploading test result file : $TestResultFile"
(New-Object 'System.Net.WebClient').UploadFile($Settings.TestUploadUrl, $TestResultFile)
}
}

task Test Unit_Tests,
Fail_If_Failed_Unit_Test,
Publish_Unit_Tests_Coverage,
Upload_Test_Results_To_AppVeyor

task Analyze {
Write-TaskBanner -TaskName $Task.Name

Add-AppveyorTest -Name 'Code Analysis' -Outcome Running
$AnalyzeSettings = $Settings.AnalyzeParams
$Script:AnalyzeFindings = Invoke-ScriptAnalyzer @AnalyzeSettings

If ( $AnalyzeFindings ) {
$FindingsString = $AnalyzeFindings | Out-String
Write-Warning $FindingsString
Update-AppveyorTest -Name 'Code Analysis' -Outcome Failed -ErrorMessage $FindingsString
}
Else {
Update-AppveyorTest -Name 'Code Analysis' -Outcome Passed
}
}

task Fail_If_Analyze_Findings {
Write-TaskBanner -TaskName $Task.Name

$FailureMessage = 'PSScriptAnalyzer found {0} issues. Aborting build' -f $AnalyzeFindings.Count
assert ( -not($AnalyzeFindings) ) $FailureMessage
}

task Set_Module_Version {
Write-TaskBanner -TaskName $Task.Name

$ManifestContent = Get-Content -Path $Settings.ManifestPath
$CurrentVersion = $Settings.VersionRegex.Match($ManifestContent).Groups['ModuleVersion'].Value
"Current module version in the manifest : $CurrentVersion"

$ManifestContent -replace $CurrentVersion,$Settings.Version | Set-Content -Path $Settings.ManifestPath -Force
$NewManifestContent = Get-Content -Path $Settings.ManifestPath
$NewVersion = $Settings.VersionRegex.Match($NewManifestContent).Groups['ModuleVersion'].Value
"Updated module version in the manifest : $NewVersion"

If ( $NewVersion -ne $Settings.Version ) {
Throw "Module version was not updated correctly to $($Settings.Version) in the manifest."
}
}

task Push_Build_Changes_To_Repo {
Write-TaskBanner -TaskName $Task.Name

cmd /c "git config --global credential.helper store 2>&1"
Add-Content "$env:USERPROFILE\.git-credentials" "https://$($Settings.GitHubKey):x-oauth-basic@github.com`n"
cmd /c "git config --global user.email ""$($Settings.Email)"" 2>&1"
cmd /c "git config --global user.name ""$($Settings.Name)"" 2>&1"
cmd /c "git config --global core.autocrlf true 2>&1"
cmd /c "git checkout $($Settings.Branch) 2>&1"
cmd /c "git add -A 2>&1"
cmd /c "git commit -m ""Commit build changes [ci skip]"" 2>&1"
cmd /c "git status 2>&1"
cmd /c "git push origin $($Settings.Branch) 2>&1"
}

task Copy_Source_To_Build_Output {
Write-TaskBanner -TaskName $Task.Name

"Copying the source folder [$($Settings.SourceFolder)] into the build output folder : [$($Settings.BuildOutput)]"
Copy-Item -Path $Settings.SourceFolder -Destination $Settings.BuildOutput -Recurse
}

# Default task :
task . Clean,
Install_Dependencies,
Test,
Analyze,
Fail_If_Analyze_Findings,
Set_Module_Version,
Push_Build_Changes_To_Repo,
Copy_Source_To_Build_Output
48 changes: 14 additions & 34 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,20 @@ version: 1.0.{build}

image: Visual Studio 2017

install:
environment:
Coveralls_Key:
secure: 8k+ZP0tz89b1tE7yKah3uwxdbfkvgwWHlRwxQwPTQxLgkuJ8NWy4dd6BXsQ6/0aW
GitHub_Key:
secure: wtrwAJK+i7Ar5L8TXeXOUtsxmVD+2wXu9u9bOf6GRfPP0Xn2V4yqTatLwaT7VWA6
PSGallery_Key:
secure: 1OaraGK9SJwOoGVdOHdM1DH5rvfL2AcDfvUFvrMxKYIkqJ0LIw3rwe4nUhxMgBfE

before_build:
- ps: Write-Host "Build version :` $env:APPVEYOR_BUILD_VERSION"
- ps: Write-Host "Branch :` $env:APPVEYOR_REPO_BRANCH"
- ps: Install-Module Pester -Scope CurrentUser -SkipPublisherCheck -Force
- ps: Install-Module PsScriptAnalyzer -Scope CurrentUser -SkipPublisherCheck -Force
- ps: Install-Module PSCodeHealth -Scope CurrentUser -SkipPublisherCheck -Force

build: false

test_script:
- ps: |
$UnitTestsOutput = Invoke-Pester -Script ".\Tests" -OutputFile TestsResults.xml -OutputFormat NUnitXml -PassThru
(New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path .\TestsResults.xml))
$FailedCount = $UnitTestsOutput.FailedCount
If ($FailedCount -gt 0) {
Add-AppveyorMessage -Message "$($FailedCount) unit test(s) failed."
# Failing the build
Throw "Build failed because $($FailedCount) unit test(s) failed"
}
- ps: Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
- ps: Install-Module InvokeBuild -Scope CurrentUser -AllowClobber -Force
- ps: Import-Module InvokeBuild

- ps: |
Add-AppveyorTest -Name "PsScriptAnalyzer" -Outcome Running
$ScriptAnalyzerResults = Invoke-ScriptAnalyzer -Path $pwd -Recurse -Severity Error -ErrorAction SilentlyContinue
If ($ScriptAnalyzerResults) {
$ScriptAnalyzerResultString = $ScriptAnalyzerResults | Out-String
Write-Warning $ScriptAnalyzerResultString
Add-AppveyorMessage -Message "PSScriptAnalyzer output contained one or more result(s) with 'Error' severity.`
Check the 'Tests' tab of this build for more details." -Category Error
Update-AppveyorTest -Name "PsScriptAnalyzer" -Outcome Failed -ErrorMessage $ScriptAnalyzerResultString
# Failing the build
Throw "Build failed"
}
Else {
Update-AppveyorTest -Name "PsScriptAnalyzer" -Outcome Passed
}
build_script:
- ps: Invoke-Build

0 comments on commit 7fb2803

Please sign in to comment.