forked from pspete/psPAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy-github.ps1
116 lines (76 loc) · 3.23 KB
/
deploy-github.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<#---------------------------------
Update version number on GitHub to match build version
---------------------------------#>
if (-not ($ENV:APPVEYOR_PULL_REQUEST_NUMBER)) {
If ($ENV:APPVEYOR_REPO_BRANCH -eq 'master') {
<# Master Branch #>
Write-Host "Deploy Process: GitHub Repository" -ForegroundColor Yellow
<#---------------------------------#>
<# If Not a PR #>
<# Push psd1 file to ORIGIN Branch #>
<#---------------------------------#>
Try {
Write-Host "Push Updated $($env:APPVEYOR_PROJECT_NAME).psd1 to GitHub..." -ForegroundColor Yellow
git config --global core.safecrlf false
git config --global credential.helper store
Add-Content "$HOME\.git-credentials" "https://$($env:access_token):[email protected]`n"
git config --global user.email "$($env:github_email)"
git config --global user.name "Pete Maan"
git checkout -q $($ENV:APPVEYOR_REPO_BRANCH)
git add $(Join-Path -Path (Join-Path -Path "$env:APPVEYOR_BUILD_FOLDER" -ChildPath "$env:APPVEYOR_PROJECT_NAME") -ChildPath "$env:APPVEYOR_PROJECT_NAME.psd1")
git status
git commit -s -m "Update Version: $($env:APPVEYOR_BUILD_VERSION)"
git push --porcelain origin $($ENV:APPVEYOR_REPO_BRANCH)
Write-Host "$($env:APPVEYOR_PROJECT_NAME) version $($env:APPVEYOR_BUILD_VERSION) pushed to GitHub." -ForegroundColor Cyan
}
Catch {
Write-Host "Push to GitHub failed." -ForegroundColor Red
throw $_
}
Write-Host "Deploy Process: GitHub Release" -ForegroundColor Yellow
If ($env:APPVEYOR_BUILD_VERSION -ge "1.0.0") {
<# Create New Release #>
$token = $env:access_token
$uploadFilePath = Resolve-Path "..\$($env:APPVEYOR_PROJECT_NAME)-v$($env:APPVEYOR_BUILD_VERSION).zip"
$releaseName = "v$($env:APPVEYOR_BUILD_VERSION)"
$repo = "pspete/$env:APPVEYOR_PROJECT_NAME"
$headers = @{
"Authorization" = "token $token"
"Content-type" = "application/json"
}
$body = @{
tag_name = $releaseName
name = $releaseName
body = "$($env:APPVEYOR_PROJECT_NAME) v$($env:APPVEYOR_BUILD_VERSION)"
draft = $false
prerelease = $false
}
Write-Host "Creating release $releaseName..." -NoNewline
try {
$json = (ConvertTo-Json $body)
$release = Invoke-RestMethod -Uri "https://api.github.com/repos/$repo/releases" -Headers $headers -Method POST -Body $json
$uploadUrl = $release.upload_url.Replace("{?name,label}", "") + "?name=" + [IO.Path]::GetFileName($uploadFilePath)
Write-Host "OK" -ForegroundColor Green
Write-Host "Uploading asset $($env:APPVEYOR_PROJECT_NAME)-v$($env:APPVEYOR_BUILD_VERSION).zip..." -NoNewline
$data = [System.IO.File]::ReadAllBytes($uploadFilePath)
$wc = New-Object Net.WebClient
$wc.Headers['Content-type'] = 'application/octet-stream'
$wc.Headers['Authorization'] = "token $token"
$null = $wc.UploadData($uploadUrl, "POST", $data)
Write-Host "OK" -ForegroundColor Green
}
catch {
Write-Host "GitHub Release Failed." -ForegroundColor Red
throw $_
}
}
}
Else {
<# Not Master Branch #>
Write-Host "$ENV:APPVEYOR_REPO_BRANCH Branch; No Release" -ForegroundColor Cyan
exit;
}
}
Else {
Write-Host "Skipping Deploy Process: GitHub Repository" -ForegroundColor Yellow
}