Skip to content

Commit

Permalink
Fixed deployment process
Browse files Browse the repository at this point in the history
  • Loading branch information
lipkau committed Sep 7, 2018
1 parent 390b903 commit 50f317c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 39 deletions.
59 changes: 36 additions & 23 deletions AtlassianPS.Configuration.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,34 @@ Set-StrictMode -Version Latest
Import-Module "$PSScriptRoot/Tools/BuildTools.psm1" -Force -ErrorAction Stop
Import-Module "$PSScriptRoot/Tools/AppVeyor.psm1" -Force -ErrorAction Stop

if ($env:APPVEYOR_JOB_ID) {
$project = Get-AppVeyorProject
}

if ($BuildTask -notin @("SetUp", "InstallDependencies")) {
Import-Module BuildHelpers -Force -ErrorAction Stop
Invoke-Init
}
if ('AppVeyor' -eq $env:BHBuildSystem) {
$project = Get-AppVeyorProject
}

$shouldDeploy = (
($env:ShouldDeploy -eq $true) -and
# only deploy master branch
('master' -eq $env:BHBranchName) -and
# it cannot be a PR
( -not $env:APPVEYOR_PULL_REQUEST_NUMBER) -and
# only deploy from AppVeyor
('AppVeyor' -eq $env:BHBuildSystem) -and
# must be last job of AppVeyor
(Test-IsLastJob) -and
# Travis-CI must be finished (if used)
# TODO: ( -not Test-TravisProgress) -and
# it cannot have a commit message that contains "skip-deploy"
($env:BHCommitMessage -notlike '*skip-deploy*')
)

#region SetUp
# Synopsis: Proxy task
task Init { Invoke-Init }

# Synopsis: Create an initial environment for developing on the module
task SetUp InstallDependencies, Build

Expand All @@ -56,13 +75,6 @@ task InstallDependencies {
Import-Module BuildHelpers -Force
}

# Synopsis: Ensure the build environment is all ready to go
task Init {
Set-BuildEnvironment -BuildOutput '$ProjectPath/Release' -ErrorAction SilentlyContinue

Add-ToModulePath -Path $env:BHBuildOutput
}, GetNextVersion

# Synopsis: Get the next version for the build
task GetNextVersion {
$currentVersion = [Version](Get-Metadata -Path $env:BHPSModuleManifest)
Expand Down Expand Up @@ -98,7 +110,7 @@ switch ($true) {
#endregion HarmonizeVariables

#region DebugInformation
task ShowInfo Init, {
task ShowInfo Init, GetNextVersion, {
Write-Build Gray
Write-Build Gray ('Running in: {0}' -f $env:BHBuildSystem)
Write-Build Gray '-------------------------------------------------------'
Expand All @@ -113,7 +125,7 @@ task ShowInfo Init, {
Write-Build Gray ('Commit: {0}' -f $env:BHCommitMessage)
Write-Build Gray ('Build #: {0}' -f $env:BHBuildNumber)
Write-Build Gray ('Next Version: {0}' -f $env:NextBuildVersion)
Write-Build Gray ('Will deploy new version? {0}' -f (Test-ShouldDeploy))
Write-Build Gray ('Will deploy new version? {0}' -f $shouldDeploy)
Write-Build Gray '-------------------------------------------------------'
Write-Build Gray
Write-Build Gray ('PowerShell version: {0}' -f $PSVersionTable.PSVersion.ToString())
Expand All @@ -125,10 +137,10 @@ task ShowInfo Init, {

#region BuildRelease
# Synopsis: Build a shippable release
task Build GenerateRelease, UpdateManifest, CompileModule, UploadArtifacts
task Build Init, GenerateRelease, UpdateManifest, CompileModule, UploadArtifacts

# Synopsis: Generate ./Release structure
task GenerateRelease Init, GenerateExternalHelp, {
task GenerateRelease GenerateExternalHelp, {
# Setup
if (-not (Test-Path "$env:BHBuildOutput/$env:BHProjectName")) {
$null = New-Item -Path "$env:BHBuildOutput/$env:BHProjectName" -ItemType Directory
Expand Down Expand Up @@ -187,7 +199,7 @@ task CompileModule {
}

# Synopsis: Use PlatyPS to generate External-Help
task GenerateExternalHelp Init, {
task GenerateExternalHelp {
Import-Module platyPS -Force
foreach ($locale in (Get-ChildItem "$env:BHProjectPath/docs" -Attribute Directory)) {
New-ExternalHelp -Path "$($locale.FullName)" -OutputPath "$env:BHModulePath/$($locale.Basename)" -Force
Expand Down Expand Up @@ -221,12 +233,12 @@ task Package GenerateRelease, {
}

# Synopsis: Upload build files as artifacts
task UploadArtifacts -If ($env:APPVEYOR_JOB_ID) {
task UploadArtifacts -If ('AppVeyor' -eq $env:BHBuildSystem) {
Get-ChildItem $env:BHBuildOutput/$env:BHProjectName -File | % { Push-AppveyorArtifact $_.FullName }
}

# Synopsis: Download build module from artifacts
task DownloadArtifacts -If ($env:APPVEYOR_JOB_ID) Init, GenerateExternalHelp, {
task DownloadArtifacts -If ('AppVeyor' -eq $env:BHBuildSystem) GenerateExternalHelp, {
# Setup
if (-not (Test-Path "$env:BHBuildOutput/$env:BHProjectName")) {
$null = New-Item -Path "$env:BHBuildOutput/$env:BHProjectName" -ItemType Directory
Expand Down Expand Up @@ -275,7 +287,7 @@ task Test Init, {
}
$testResults = Invoke-Pester @parameter

if ($env:APPVEYOR_JOB_ID) {
if ('AppVeyor' -eq $env:BHBuildSystem) {
BuildHelpers\Add-TestResultToAppveyor -TestFile $parameter["OutputFile"]
}

Expand All @@ -284,18 +296,19 @@ task Test Init, {
catch {
throw $_
}
}, RemoveTestResults
}, RemoveTestResults, { Init }
#endregion

#region Publish
# Synopsis: Publish a new release on github and the PSGallery
task Deploy -If { Test-ShouldDeploy } Init, PublishToGallery, TagReplository, UpdateHomepage
task Deploy -If ($shouldDeploy) Init, PublishToGallery, TagReplository, UpdateHomepage

# Synpsis: Publish the $release to the PSGallery
task PublishToGallery {
Assert-True (-not [String]::IsNullOrEmpty($env:PSGalleryAPIKey)) "No key for the PSGallery"
Assert-True {Get-Module $env:BHProjectName -ListAvailable} "Module $env:BHProjectName is not available"

Remove-Module $env:BHProjectName -ErrorAction SilentlyContinue
Remove-Module $env:BHProjectName -ErrorAction Ignore

Publish-Module -Name $env:BHProjectName -NuGetApiKey $env:PSGalleryAPIKey
}
Expand Down Expand Up @@ -370,7 +383,7 @@ task RemoveTestResults {
}
#endregion

if (($env:APPVEYOR_JOB_ID) -and ($env:APPVEYOR_JOB_ID -ne $project.build.jobs[0].JobId)) {
if (('AppVeyor' -eq $env:BHBuildSystem) -and ($env:APPVEYOR_JOB_ID -ne $project.build.jobs[0].JobId)) {
task . ShowInfo, Clean, DownloadArtifacts, Test, Deploy
}
else {
Expand Down
45 changes: 29 additions & 16 deletions Tools/BuildTools.psm1
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
[CmdletBinding()]
param()

function Invoke-Init {
[Alias("Init")]
[CmdletBinding()]
param()
begin {
Set-BuildEnvironment -BuildOutput '$ProjectPath/Release' -ErrorAction SilentlyContinue
Add-ToModulePath -Path $env:BHBuildOutput
}
}

function Assert-True {
[CmdletBinding( DefaultParameterSetName = 'ByBool' )]
param(
Expand Down Expand Up @@ -53,8 +63,8 @@ function Get-AppVeyorBuild {
Assert-True { $env:APPVEYOR_ACCOUNT_NAME } "not an appveyor build."

$invokeRestMethodSplat = @{
Uri = "https://ci.appveyor.com/api/projects/$env:APPVEYOR_ACCOUNT_NAME/$env:APPVEYOR_PROJECT_SLUG"
Method = 'GET'
Uri = "https://ci.appveyor.com/api/projects/$env:APPVEYOR_ACCOUNT_NAME/$env:APPVEYOR_PROJECT_SLUG"
Method = 'GET'
Headers = @{
"Authorization" = "Bearer $env:APPVEYOR_API_TOKEN"
"Content-type" = "application/json"
Expand All @@ -70,8 +80,8 @@ function Get-TravisBuild {
Assert-True { $env:APPVEYOR_ACCOUNT_NAME } "not an appveyor build."

$invokeRestMethodSplat = @{
Uri = "https://api.travis-ci.org/builds?limit=10"
Method = 'Get'
Uri = "https://api.travis-ci.org/builds?limit=10"
Method = 'Get'
Headers = @{
"Authorization" = "token $env:TRAVIS_API_TOKEN"
"Travis-API-Version" = "3"
Expand Down Expand Up @@ -112,7 +122,7 @@ function Test-ShouldDeploy {
return $false
}
# only deploy from AppVeyor
if (-not ('AppVeyor' -eq $env:BHBuildSystem)) {
if (-not ($env:APPVEYOR_JOB_ID)) {
return $false
}
# must be last job of AppVeyor
Expand Down Expand Up @@ -195,17 +205,17 @@ function Set-AppVeyorBuildNumber {
$separator = "-"
$headers = @{
"Authorization" = "Bearer $env:APPVEYOR_API_TOKEN"
"Content-type" = "application/json"
"Content-type" = "application/json"
}
$apiURL = "https://ci.appveyor.com/api/projects/$env:APPVEYOR_ACCOUNT_NAME/$env:APPVEYOR_PROJECT_SLUG"
$history = Invoke-RestMethod -Uri "$apiURL/history?recordsNumber=2" -Headers $headers -Method Get
if ($history.builds.Count -eq 2) {
$s = Invoke-RestMethod -Uri "$apiURL/settings" -Headers $headers -Method Get
$s.settings.nextBuildNumber = ($s.settings.nextBuildNumber - 1)
Invoke-RestMethod -Uri 'https://ci.appveyor.com/api/projects' -Headers $headers -Body ($s.settings | ConvertTo-Json -Depth 10) -Method Put
$previousVersion = $history.builds[1].version
if ($previousVersion.IndexOf("$separator") -ne "-1") {$previousVersion = $previousVersion.SubString(0, $previousVersion.IndexOf("$separator"))}
Update-AppveyorBuild -Version $previousVersion$separator$((New-Guid).ToString().SubString(0,8))
$s = Invoke-RestMethod -Uri "$apiURL/settings" -Headers $headers -Method Get
$s.settings.nextBuildNumber = ($s.settings.nextBuildNumber - 1)
Invoke-RestMethod -Uri 'https://ci.appveyor.com/api/projects' -Headers $headers -Body ($s.settings | ConvertTo-Json -Depth 10) -Method Put
$previousVersion = $history.builds[1].version
if ($previousVersion.IndexOf("$separator") -ne "-1") {$previousVersion = $previousVersion.SubString(0, $previousVersion.IndexOf("$separator"))}
Update-AppveyorBuild -Version $previousVersion$separator$((New-Guid).ToString().SubString(0,8))
}
}

Expand Down Expand Up @@ -320,7 +330,7 @@ function Get-FileEncoding {
[System.Collections.Generic.List[Byte]]$signatureBytes = foreach ($byte in $value.Split('-')) {
[Convert]::ToByte($byte, 16)
}
,$signatureBytes
, $signatureBytes
}
$signatures[$name] = $values
}
Expand Down Expand Up @@ -369,7 +379,8 @@ function Get-FileEncoding {
Encoding = $encoding
Path = $Path
} | Add-Member -TypeName 'EncodingInfo' -PassThru
} catch {
}
catch {
$pscmdlet.WriteError($_)
}
}
Expand Down Expand Up @@ -420,10 +431,12 @@ function Remove-Utf8Bom {
[System.IO.File]::ReadAllLines($Path),
$encoding
)
} else {
}
else {
Write-Verbose ('A UTF8 BOM was not detected on the file {0}' -f $Path)
}
} catch {
}
catch {
Write-Error -ErrorRecord $_
}
}
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ environment:
TRAVIS_API_TOKEN:
secure: xwsl4f9maG+AEQDAWzFNA/sFXFZfGuFdHf+V6Eikabs=
TIMEOUT_MINS: 30
ShouldDeploy: true
# APPVEYOR_BUILD_WORKER_CLOUD: gce
matrix:
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
Expand Down

0 comments on commit 50f317c

Please sign in to comment.