From ced828c4016bcb002012b81304ba83e17d1886c5 Mon Sep 17 00:00:00 2001 From: lukeIam Date: Mon, 5 Oct 2015 22:13:34 +0200 Subject: [PATCH] Appveyor: add prepareDeploy.ps1 and gitDescribe.ps1 which is needed to use GitHub API to correctly tag automatic builds, because git-describe doesn't work on shallow clones --- .appveyor/gitDescribe.ps1 | 54 ++++++++++++++++++++++++++++++ .appveyor/prepareDeploy.ps1 | 65 +++++++++++++++++++++++++++++++++++++ appveyor.yml | 33 ++++++++++++++----- 3 files changed, 144 insertions(+), 8 deletions(-) create mode 100644 .appveyor/gitDescribe.ps1 create mode 100644 .appveyor/prepareDeploy.ps1 diff --git a/.appveyor/gitDescribe.ps1 b/.appveyor/gitDescribe.ps1 new file mode 100644 index 00000000..12b444d6 --- /dev/null +++ b/.appveyor/gitDescribe.ps1 @@ -0,0 +1,54 @@ +$currentCommitSha = $Env:CurrentCommitSha +$githubRepoApiUri = $Env:githubRepoApiUri #"https://api.github.com/repos/:user/:repo/" +$Env:VersionTag = "" + +# Inline-if helper function +Function IIf($If, $Right, $Wrong) {If ($If) {$Right} Else {$Wrong}} + +try{ + $result = Invoke-RestMethod -Uri "$($githubRepoApiUri)git/refs/tags" -Method 'GET' +} +catch [System.Net.WebException] +{ + # Error getting tags + $statusCode = [int]$_.Exception.Response.StatusCode + $html = $_.Exception.Response.StatusDescription + Write-Host "Error getting tags ($html)" + Return ; +} + +# Get tag info +try{ + $tagsInfo = ($result | % { "$($githubRepoApiUri)git/tags/$($_.object.sha)" ;} | % { Invoke-RestMethod -Method 'GET' -Uri $($_)}) +} +catch [System.Net.WebException] +{ + # Error getting tags + $statusCode = [int]$_.Exception.Response.StatusCode + $html = $_.Exception.Response.StatusDescription + Write-Host "Error getting tags ($html)" + Return ; +} + +# Compare taged commits with current commit +try{ + $commitTagComp = $tagsInfo | select sha, tag, tagger, @{n='dist';e={[int](Invoke-RestMethod -Method 'GET' -Uri "$($githubRepoApiUri)compare/$($_.object.sha)...$currentCommitSha" | % { (IIf ($_.status -ne "diverged") ($_.ahead_by) (-1))})}} +} +catch [System.Net.WebException] +{ + # Error comparing tags + $statusCode = [int]$_.Exception.Response.StatusCode + $html = $_.Exception.Response.StatusDescription + Write-Host "Error comparing tags ($html)" + Return ; +} + +$latestTag = $commitTagComp | where {$_.dist -ge 0} | sort-object -property @{Expression="dist";Descending=$false}, @{Expression="tag";Descending=$false} | Select-Object -first 1 + +if(!$latestTag){ + Write-Host "Commit not describable" + Return ; +} + +$Env:VersionTag = "$($latestTag.tag)-$($latestTag.dist)-g$($currentCommitSha.Substring(0,7))" +Return $Env:VersionTag diff --git a/.appveyor/prepareDeploy.ps1 b/.appveyor/prepareDeploy.ps1 new file mode 100644 index 00000000..c6bbbd82 --- /dev/null +++ b/.appveyor/prepareDeploy.ps1 @@ -0,0 +1,65 @@ +if($Env:APPVEYOR_REPO_TAG -eq "true") { + $targetTag = $Env:APPVEYOR_REPO_TAG_NAME + Write-Host "Tag=$targetTag" +} +else{ + if($Env:APPVEYOR_REPO_BRANCH -eq "appveyorTest"){ + $targetTag = "Nightly" + } + else{ + Write-Host "No tag or special branch found" + Return + } + git config --global user.email "build@vocaluxe.de"; + git config --global user.name "Vocaluxe_Automatic_Build"; + git tag "$targetTag" -f; + git push -q -f "https://$($Env:GitHubKey):x-oauth-basic@github.com/lukeIam/Vocaluxe.git" "$targetTag"; + + Write-Host "Branch $Env:APPVEYOR_REPO_BRANCH -> Tag=$targetTag" +} + +$getRelaseInfoParams = @{ + Uri = "https://api.github.com/repos/lukeIam/Vocaluxe/releases/tags/$targetTag"; + Method = 'GET'; +} +try{ + $result = Invoke-RestMethod @getRelaseInfoParams +} +catch [System.Net.WebException] +{ + # No Release found for this tag + $statusCode = [int]$_.Exception.Response.StatusCode + $html = $_.Exception.Response.StatusDescription + Write-Host "No Release found for this tag ($html)" + Return +} + +$assetId = ($result.assets | where {$_.name -EQ "Vocaluxe_$Env:APPVEYOR_REPO_BRANCH_Windows_$Env:PLATFORM.zip" } | Select-Object -first 1 ).id + +if(!$assetId){ + # No matching asset found in this release + Write-Host "No matching asset found in this release" + Return +} + +$deleteAssetParams = @{ + Uri = "https://api.github.com/repos/lukeIam/Vocaluxe/releases/assets/$assetId"; + Method = 'DELETE'; + Headers = @{ + Authorization = 'Basic ' + [Convert]::ToBase64String( + [Text.Encoding]::ASCII.GetBytes($Env:GitHubKey + ":x-oauth-basic")); + } +} +try{ + $result = Invoke-RestMethod @deleteAssetParams + Write-Host "Successfully deleted asset" +} +catch [System.Net.WebException] +{ + # Could not delete asset + $statusCode = [int]$_.Exception.Response.StatusCode + $html = $_.Exception.Response.StatusDescription + Write-Host "Could not delete asset ($html)" + Return +} + \ No newline at end of file diff --git a/appveyor.yml b/appveyor.yml index 278c6ac4..b51c56eb 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -6,26 +6,42 @@ configuration: ReleaseWin platform: - x64 - x86 -clone_depth: 1 +clone_depth: 10 +environment: + GitHubKey: + secure: QBwVoI9PlI+PWfX8vtjH22cSPhj2nwJ78CQcwC4Olsibn1QNar2KX4HEfOGNbazA +install: +- ps: >- + $Env:CurrentCommitSha = "$(git rev-parse --verify HEAD)" + + $Env:githubRepoApiUri = "https://api.github.com/repos/Vocaluxe/Vocaluxe/" + + ./.appveyor/gitDescribe.ps1 + + $Env:GIT_VERSION_TAG = $Env:VersionTag before_build: - ps: >- nuget restore -DisableParallelProcessing - (Get-Content -Encoding UTF8 'PitchTracker\PitchTracker.vcxproj') | Foreach-Object {$_ -replace 'v110_xp', 'v120_xp'} | Set-Content -Encoding UTF8 'PitchTracker\PitchTracker.vcxproj' + #(Get-Content -Encoding UTF8 'PitchTracker\PitchTracker.vcxproj') | Foreach-Object {$_ -replace 'v110_xp', 'v120_xp'} | Set-Content -Encoding UTF8 'PitchTracker\PitchTracker.vcxproj' build: project: Vocaluxe.sln verbosity: normal after_build: -- cd Output -- 7z a Vocaluxe_%APPVEYOR_REPO_BRANCH%_Windows_%PLATFORM%.zip * -- appveyor PushArtifact Vocaluxe_%APPVEYOR_REPO_BRANCH%_Windows_%PLATFORM%.zip -FileName Vocaluxe_%APPVEYOR_REPO_BRANCH%_Windows_%PLATFORM%.zip -DeploymentName ReleaseZip -- cd .. +- cmd: >- + cd Output + + 7z a Vocaluxe_%APPVEYOR_REPO_BRANCH%_Windows_%PLATFORM%.zip * + + appveyor PushArtifact Vocaluxe_%APPVEYOR_REPO_BRANCH%_Windows_%PLATFORM%.zip -FileName Vocaluxe_%APPVEYOR_REPO_BRANCH%_Windows_%PLATFORM%.zip -DeploymentName ReleaseZip + + cd .. test: off artifacts: - path: Output\Vocaluxe_{APPVEYOR_REPO_BRANCH}_Windows_{PLATFORM}.zip name: ReleaseZip before_deploy: -- set GIT_VERSION_TAG=git describe --long +- ps: ./.appveyor/prepareDeploy.ps1 deploy: - provider: GitHub tag: $(APPVEYOR_REPO_TAG_NAME) @@ -45,4 +61,5 @@ deploy: secure: QBwVoI9PlI+PWfX8vtjH22cSPhj2nwJ78CQcwC4Olsibn1QNar2KX4HEfOGNbazA artifact: ReleaseZip on: - branch: appveyorTest \ No newline at end of file + branch: appveyorTest + APPVEYOR_PULL_REQUEST_NUMBER: '' \ No newline at end of file