Skip to content

Commit 79608b0

Browse files
committed
Auto-release setup
1 parent 13900f5 commit 79608b0

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎Jenkins/release.ps1‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
param([string] $ApiBase, [string]$tagName, [string]$authinfo, [string[]] $assetPaths)
2+
Add-Type -AssemblyName "System.Web"
3+
4+
$headers = @{
5+
Authorization = "Basic " + [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($authinfo))
6+
Accept = "application/vnd.github.v3+json"
7+
}
8+
try
9+
{
10+
Write-Output("Checking if release with tag " + $tagName + " already exists...")
11+
$release = Invoke-RestMethod -Uri ($ApiBase+"releases/tags/$tagName") -Method "GET" -Headers $headers
12+
Write-Output(" Using existing release " + $release.id + " at " + $release.html_url)
13+
} catch {
14+
Write-Output(" Doesn't exist")
15+
$rel_arg = @{
16+
tag_name=$tagName
17+
name="Generated $tagName"
18+
body=""
19+
draft=$TRUE
20+
prerelease=$tagName.Contains("alpha") -or $tagName.Contains("beta")
21+
}
22+
Write-Output("Creating new release " + $tagName + "...")
23+
$release = Invoke-RestMethod -Uri ($ApiBase+"releases") -Method "POST" -Headers $headers -Body (ConvertTo-Json($rel_arg))
24+
Write-Output(" Created new release " + $tagName + " at " + $release.html_url)
25+
}
26+
27+
$assetsApiBase = $release.assets_url
28+
Write-Output("Checking for existing assets...")
29+
$existingAssets = Invoke-RestMethod -Uri ($assetsApiBase) -Method "GET" -Headers $headers
30+
$assetLabels = ($assetPaths | ForEach-Object {[System.IO.Path]::GetFileName($_)})
31+
foreach ($asset in $existingAssets) {
32+
if ($assetLabels -contains $asset.name) {
33+
$uri = $asset.url
34+
Write-Output(" Deleting old asset " + $asset.name + " (id " + $asset.id + "); URI=" + $uri)
35+
$result = Invoke-RestMethod -Uri $uri -Method "DELETE" -Headers $headers
36+
}
37+
}
38+
Write-Output("Uploading assets...")
39+
$uploadUrl = $release.upload_url.Substring(0, $release.upload_url.LastIndexOf('{'))
40+
foreach ($asset in $assetPaths) {
41+
$assetName = [System.IO.Path]::GetFileName($asset)
42+
$assetType = [System.Web.MimeMapping]::GetMimeMapping($asset)
43+
$assetData = [System.IO.File]::ReadAllBytes($asset)
44+
$headerExtra = $headers + @{
45+
"Content-Type" = $assetType
46+
Name = $assetName
47+
}
48+
$uri = $uploadUrl + "?name=" + $assetName
49+
Write-Output(" Uploading " + $asset + " as " + $assetType + "; URI=" + $uri)
50+
$result = Invoke-RestMethod -Uri $uri -Method "POST" -Headers $headerExtra -Body $assetData
51+
Write-Output(" ID=" + $result.id + ", found at=" + $result.browser_download_url)
52+
}

‎Jenkinsfile‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ node {
6565
stage('Archive') {
6666
archiveArtifacts artifacts: "bin/x64/Release/Essentials.dll", caseSensitive: false, fingerprint: true, onlyIfSuccessful: true
6767
}
68+
69+
gitVersion = bat(returnStdout: true, script: "@git describe --tags").trim()
70+
gitSimpleVersion = bat(returnStdout: true, script: "@git describe --tags --abbrev=0").trim()
71+
if (gitVersion == gitSimpleVersion) {
72+
stage('Release') {
73+
withCredentials([usernamePassword(credentialsId: 'torch-github', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
74+
powershell "& ./Jenkins/release.ps1 \"https://api.github.com/repos/TorchAPI/Essentials/\" \"$gitSimpleVersion\" \"$USERNAME:$PASSWORD\" @(\"bin/x64/Release/Essentials.dll\")"
75+
}
76+
}
77+
}
6878
}
6979
else
7080
currentBuild.result = "FAIL"

0 commit comments

Comments
 (0)