From 8e3aa52241f829ddfdc43e64e6d6e2b03e90cc60 Mon Sep 17 00:00:00 2001 From: "Joel Sallow (/u/ta11ow)" <32407840+vexx32@users.noreply.github.com> Date: Sat, 29 Feb 2020 18:46:54 -0500 Subject: [PATCH] :rocket: Run tests against built module (#363) --- Build/Build-Module.ps1 | 9 +- Build/Register-FileSystemRepository.ps1 | 25 +++ Deploy/Publish.ps1 | 8 - azure-pipelines.yml | 229 ++++++++++++++---------- templates/install-built-module.yml | 36 ++++ templates/register-local-repo.yml | 19 ++ 6 files changed, 216 insertions(+), 110 deletions(-) create mode 100644 Build/Register-FileSystemRepository.ps1 create mode 100644 templates/install-built-module.yml create mode 100644 templates/register-local-repo.yml diff --git a/Build/Build-Module.ps1 b/Build/Build-Module.ps1 index d5cf96de7..3adbbbdb3 100644 --- a/Build/Build-Module.ps1 +++ b/Build/Build-Module.ps1 @@ -1,6 +1,8 @@ # Grab nuget bits, set build variables, start build. Get-PackageProvider -Name NuGet -ForceBootstrap > $null +Import-Module "$env:PROJECTROOT/PSKoans" + Set-BuildEnvironment $Lines = '-' * 70 @@ -31,8 +33,5 @@ catch { # Build external help files from Platyps MD files New-ExternalHelp -Path "$env:PROJECTROOT/docs/" -OutputPath "$env:PROJECTROOT/PSKoans/en-us" -$BuiltModuleFolder = "$env:BUILD_ARTIFACTSTAGINGDIRECTORY/Module/" -Write-Host "##vso[task.setvariable variable=BuiltModuleFolder]$BuiltModuleFolder" - -New-Item -Path $BuiltModuleFolder -ItemType Directory -Copy-Item -Path "$env:PROJECTROOT/PSKoans" -Destination $BuiltModuleFolder -Recurse -PassThru +Copy-Item -Path "$env:PROJECTROOT/PSKoans" -Destination $env:BUILTMODULEPATH -Recurse -PassThru | + Where-Object { -not $_.PSIsContainer } diff --git a/Build/Register-FileSystemRepository.ps1 b/Build/Register-FileSystemRepository.ps1 new file mode 100644 index 000000000..2a79a77f0 --- /dev/null +++ b/Build/Register-FileSystemRepository.ps1 @@ -0,0 +1,25 @@ +[CmdletBinding()] +param( + [Parameter(Mandatory)] + [string] + $Path, + + [Parameter(Mandatory)] + [string] + $Name +) + +$RepositoryFolder = if (-not (Test-Path $Path)) { + New-Item -ItemType Directory -Path $Path -Force +} +else { + Get-Item -Path $Path +} + +$Params = @{ + Name = $Name + SourceLocation = $RepositoryFolder.FullName + ScriptSourceLocation = $RepositoryFolder.FullName + InstallationPolicy = 'Trusted' +} +Register-PSRepository @Params diff --git a/Deploy/Publish.ps1 b/Deploy/Publish.ps1 index 37370e527..3e94942f6 100644 --- a/Deploy/Publish.ps1 +++ b/Deploy/Publish.ps1 @@ -13,14 +13,6 @@ param( $env:NugetApiKey = $Key if ($OutputDirectory) { - $Params = @{ - Name = 'FileSystem' - SourceLocation = "$PSScriptRoot/FileSystem" - ScriptSourceLocation = "$PSScriptRoot/FileSystem" - InstallationPolicy = 'Trusted' - } - Register-PSRepository @Params - Import-Module "$PSScriptRoot/PSKoans" $Module = Get-Module -Name PSKoans $Dependencies = @( diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 46123cb16..a8ae5d629 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -14,62 +14,50 @@ trigger: pr: - master +variables: + NupkgArtifactName: 'PSKoans.nupkg' + stages: -- stage: LinuxTests - displayName: 'Linux' +- stage: UploadChangelog + displayName: 'Upload Changelog' dependsOn: [] + condition: startsWith(variables['Build.SourceBranch'], 'refs/heads/master') jobs: - - job: Linux - displayName: 'Pester Tests' + - job: GenerateChangelog + displayName: "Generate Changelog" pool: vmImage: ubuntu-latest - steps: - - template: templates/environment-setup.yml - - template: templates/test-steps.yml - -- stage: WindowsTests - displayName: 'Windows' - dependsOn: [] - - jobs: - - job: Windows - displayName: 'Pester Tests' - - pool: - vmImage: windows-latest + variables: + FilePath: '$(System.DefaultWorkingDirectory)/Changelog.md' steps: - - template: templates/environment-setup.yml - - template: templates/test-steps.yml - -- stage: MacOSTests - displayName: 'MacOS' - dependsOn: [] - - jobs: - - job: MacOS - displayName: 'Pester Tests' + - task: PowerShell@2 + displayName: 'Create Changelog' + inputs: + targetType: 'filepath' + filePath: ./Build/New-Changelog.ps1 + arguments: -Path '$(FilePath)' -ApiKey $(GithubApiKey) -Verbose - pool: - vmImage: macOS-latest + - task: PublishPipelineArtifact@1 + displayName: 'Publish Changelog' + inputs: + path: '$(FilePath)' + artifact: Changelog - steps: - - template: templates/environment-setup.yml - - template: templates/test-steps.yml +- stage: Build + displayName: 'Build PSKoans' + dependsOn: [] -- stage: PublishModule - displayName: 'Create Module' - dependsOn: - - LinuxTests - - WindowsTests - - MacOSTests + variables: + FileSystemDeploymentPath: '$(System.DefaultWorkingDirectory)/Deploy/FileSystem' + BuiltModulePath: '$(System.DefaultWorkingDirectory)/Deploy/PSKoans' jobs: - - job: BuildModule - displayName: "Build PSKoans" + - job: BuildNupkg + displayName: 'Module Nupkg' pool: vmImage: ubuntu-latest @@ -78,7 +66,7 @@ stages: - template: templates/environment-setup.yml - task: PowerShell@2 - displayName: 'Initialize Environment' + displayName: 'Stage PSKoans Module' inputs: targetType: 'filePath' @@ -91,34 +79,19 @@ stages: - task: PublishPipelineArtifact@1 displayName: 'Publish Built Module Artifact' inputs: - path: '$(BuiltModuleFolder)/PSKoans' + path: '$(BuiltModulePath)' artifact: PSKoans - - job: PublishFiles - dependsOn: BuildModule - displayName: "Publish Artifacts" - - pool: - vmImage: ubuntu-latest - variables: - builtModulePath: '$(System.DefaultWorkingDirectory)/Deploy/PSKoans' - fileSystemDeploymentPath: '$(System.DefaultWorkingDirectory)/Deploy/FileSystem' - - steps: - - template: templates/environment-setup.yml - - - task: DownloadPipelineArtifact@2 - displayName: 'Download Built Module Artifact' - inputs: - artifact: PSKoans - path: $(builtModulePath) + - template: ./templates/register-local-repo.yml + parameters: + repositoryPath: '$(FileSystemDeploymentPath)' - task: PowerShell@2 displayName: 'Create Module Nupkg' inputs: targetType: 'filePath' filePath: ./Deploy/Publish.ps1 - arguments: -Key 'filesystem' -Path '$(fileSystemDeploymentPath)' -OutputDirectory '$(fileSystemDeploymentPath)' + arguments: -Key 'filesystem' -Path '$(FileSystemDeploymentPath)' -OutputDirectory '$(FileSystemDeploymentPath)' errorActionPreference: 'stop' failOnStderr: true @@ -128,18 +101,107 @@ stages: displayName: 'Publish Nupkg Artifact' inputs: path: '$(NupkgPath)' - artifact: PSKoans.nupkg + artifact: '$(NupkgArtifactName)' +- stage: LinuxTests + displayName: 'Linux' + dependsOn: + - Build + + variables: + PackageDownloadPath: '$(System.DefaultWorkingDirectory)/Module' + PSRepositoryName: 'FileSystem' + + jobs: + - job: Linux + displayName: 'Pester Tests' + + pool: + vmImage: ubuntu-latest + + steps: + - template: templates/environment-setup.yml + + - template: ./templates/install-built-module.yml + parameters: + repositoryPath: '$(PackageDownloadPath)' + repositoryName: '$(PSRepositoryName)' + artifactName: '$(NupkgArtifactName)' + + - template: templates/test-steps.yml + +- stage: WindowsTests + displayName: 'Windows' + dependsOn: + - Build + + variables: + PackageDownloadPath: '$(System.DefaultWorkingDirectory)/Module' + PSRepositoryName: 'FileSystem' + + jobs: + - job: Windows + displayName: 'Pester Tests' + + pool: + vmImage: windows-latest + + steps: + - template: templates/environment-setup.yml + + - template: ./templates/install-built-module.yml + parameters: + repositoryPath: '$(PackageDownloadPath)' + repositoryName: '$(PSRepositoryName)' + artifactName: '$(NupkgArtifactName)' + + - template: templates/test-steps.yml + +- stage: MacOSTests + displayName: 'MacOS' + dependsOn: + - Build + + variables: + PackageDownloadPath: '$(System.DefaultWorkingDirectory)/Module' + PSRepositoryName: 'FileSystem' + + jobs: + - job: MacOS + displayName: 'Pester Tests' + + pool: + vmImage: macOS-latest + + steps: + - template: templates/environment-setup.yml + + - template: ./templates/install-built-module.yml + parameters: + repositoryPath: '$(PackageDownloadPath)' + repositoryName: '$(PSRepositoryName)' + artifactName: '$(NupkgArtifactName)' + + - template: templates/test-steps.yml + +- stage: PublishModule + displayName: 'Publish Module' + dependsOn: + - LinuxTests + - WindowsTests + - MacOSTests + condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/')) + + jobs: - job: PublishToGallery - displayName: 'Publish PSKoans to PSGallery' - dependsOn: PublishFiles - condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/')) + displayName: 'PowerShell Gallery' pool: vmImage: ubuntu-latest + variables: - builtModulePath: '$(System.DefaultWorkingDirectory)/Deploy/PSKoans' - galleryDeploymentPath: '$(System.DefaultWorkingDirectory)/Deploy/PSGallery' + BuiltModulePath: '$(System.DefaultWorkingDirectory)/Deploy/PSKoans' + GalleryDeploymentPath: '$(System.DefaultWorkingDirectory)/Deploy/PSGallery' steps: - template: templates/environment-setup.yml @@ -148,43 +210,16 @@ stages: displayName: 'Download Built Module Artifact' inputs: artifact: PSKoans - path: $(builtModulePath) + path: $(BuiltModulePath) - task: PowerShell@2 displayName: 'Publish Module to PSGallery' inputs: targetType: 'filePath' - arguments: -Key $(PSApiKey) -Path '$(galleryDeploymentPath)' + arguments: -Key $(PSApiKey) -Path '$(GalleryDeploymentPath)' filePath: ./Deploy/Publish.ps1 errorActionPreference: 'stop' failOnStderr: true pwsh: true - -- stage: CreateChangelog - displayName: 'Upload Changelog' - dependsOn: [] - condition: startsWith(variables['Build.SourceBranch'], 'refs/heads/master') - - jobs: - - job: GenerateChangelog - displayName: "Generate Changelog" - pool: - vmImage: ubuntu-latest - variables: - filePath: '$(System.DefaultWorkingDirectory)/Changelog.md' - - steps: - - task: PowerShell@2 - displayName: 'Create Changelog' - inputs: - targetType: 'filepath' - filePath: ./Build/New-Changelog.ps1 - arguments: -Path '$(filePath)' -ApiKey $(GithubApiKey) -Verbose - - - task: PublishPipelineArtifact@1 - displayName: 'Publish Changelog' - inputs: - path: '$(filePath)' - artifact: Changelog diff --git a/templates/install-built-module.yml b/templates/install-built-module.yml new file mode 100644 index 000000000..8bfc79a33 --- /dev/null +++ b/templates/install-built-module.yml @@ -0,0 +1,36 @@ +parameters: +- name: repositoryPath + type: string + default: '$(System.DefaultWorkingDirectory)' +- name: repositoryName + type: string + default: 'FileSystem' +- name: artifactName + type: string + default: 'PSKoans.nupkg' + +steps: +- template: ./register-local-repo.yml + parameters: + repositoryPath: ${{ parameters.repositoryPath }} + repositoryName: ${{ parameters.repositoryName }} + +- task: DownloadPipelineArtifact@2 + displayName: 'Download Built Module Artifact' + inputs: + artifact: ${{ parameters.artifactName }} + path: ${{ parameters.repositoryPath }} + +- task: PowerShell@2 + displayName: 'Install PSKoans from Nupkg' + inputs: + targetType: 'inline' + script: | + Register-PackageSource -Name PSGallery -ProviderName NuGet -Location https://www.powershellgallery.com/api/v2 -Force + Save-Package -Name Pester -ProviderName NuGet -Path ${{ parameters.repositoryPath }} -Force -Source PSGallery | + Select-Object -Property Name, Version, Status, Source + Install-Module PSKoans -Repository ${{ parameters.repositoryName }} -Force -Scope CurrentUser + + errorActionPreference: 'stop' + failOnStderr: true + pwsh: true diff --git a/templates/register-local-repo.yml b/templates/register-local-repo.yml new file mode 100644 index 000000000..4887e49ee --- /dev/null +++ b/templates/register-local-repo.yml @@ -0,0 +1,19 @@ +parameters: +- name: repositoryPath + type: string + default: '$(System.DefaultWorkingDirectory)' +- name: repositoryName + type: string + default: 'FileSystem' + +steps: +- task: PowerShell@2 + displayName: 'Register FileSystem Repository' + inputs: + targetType: 'filePath' + filePath: ./Build/Register-FileSystemRepository.ps1 + arguments: -Path '${{ parameters.repositoryPath }}' -Name '${{ parameters.repositoryName }}' + + errorActionPreference: 'stop' + failOnStderr: true + pwsh: true