|
| 1 | +# upload-resources-to-github.ps1 |
| 2 | + |
| 3 | +param( |
| 4 | + [switch]$BinariesOnly |
| 5 | +) |
| 6 | + |
| 7 | +$ErrorActionPreference = "Stop" |
| 8 | + |
| 9 | +function HandleErrorsAndCleanup { |
| 10 | + param ( |
| 11 | + [int]$ExitCode |
| 12 | + ) |
| 13 | + if ($ExitCode -eq 0) { |
| 14 | + exit 0 |
| 15 | + } |
| 16 | + if ($global:AssetIdsUploaded.Count -ne 0) { |
| 17 | + Write-Host "`nCleaning up assets uploaded in the current execution of the script" |
| 18 | + foreach ($assetId in $global:AssetIdsUploaded) { |
| 19 | + Write-Host "Deleting asset $assetId" |
| 20 | + Invoke-RestMethod -Method Delete -Uri "https://api.github.com/repos/aws/aws-node-termination-handler/releases/assets/$assetId" -Headers @{Authorization = "token $env:GITHUB_TOKEN"} |
| 21 | + } |
| 22 | + exit $ExitCode |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function UploadAsset { |
| 27 | + param ( |
| 28 | + [string]$AssetPath |
| 29 | + ) |
| 30 | + $resp = Invoke-RestMethod -Method Post -Uri "https://uploads.github.com/repos/aws/aws-node-termination-handler/releases/$ReleaseId/assets?name=$(Split-Path -Leaf $AssetPath)" -Headers @{ |
| 31 | + Authorization = "token $env:GITHUB_TOKEN" |
| 32 | + 'Content-Type' = (Get-Content -Path $AssetPath -Raw | Measure-Object -Line).Count -gt 1 ? 'application/zip' : 'application/octet-stream' |
| 33 | + } -InFile $AssetPath -ErrorAction Stop |
| 34 | + |
| 35 | + if ($resp.id -ne $null) { |
| 36 | + $global:AssetIdsUploaded += $resp.id |
| 37 | + Write-Host "Created asset ID $($resp.id) successfully" |
| 38 | + } else { |
| 39 | + Write-Host "❌ Upload failed with response message: $($resp | ConvertTo-Json) ❌" |
| 40 | + exit 1 |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +$global:AssetIdsUploaded = @() |
| 45 | +trap { HandleErrorsAndCleanup -ExitCode $global:LASTEXITCODE } |
| 46 | + |
| 47 | +$ScriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path |
| 48 | +$Version = & "$ScriptPath\..\Makefile" version -s |
| 49 | +$BuildDir = "$ScriptPath\..\build\k8s-resources\$Version" |
| 50 | +$BinaryDir = "$ScriptPath\..\build\bin" |
| 51 | +$ReleaseId = (Invoke-RestMethod -Uri "https://api.github.com/repos/aws/aws-node-termination-handler/releases" -Headers @{Authorization = "token $env:GITHUB_TOKEN"} | ConvertFrom-Json | Where-Object { $_.tag_name -eq $Version }).id |
| 52 | + |
| 53 | +$Assets = @() |
| 54 | +if (-not $BinariesOnly) { |
| 55 | + $Assets += "$BuildDir\individual-resources.tar", "$BuildDir\all-resources.yaml", "$BuildDir\individual-resources-queue-processor.tar", "$BuildDir\all-resources-queue-processor.yaml" |
| 56 | +} |
| 57 | +if ($BinariesOnly) { |
| 58 | + $Assets += Get-ChildItem -Path $BinaryDir |
| 59 | +} |
| 60 | + |
| 61 | +Write-Host "`nUploading release assets for release id '$ReleaseId' to Github" |
| 62 | +for ($i = 0; $i -lt $Assets.Count; $i++) { |
| 63 | + Write-Host "`n $($i + 1). $($Assets[$i] | Split-Path -Leaf)" |
| 64 | + UploadAsset -AssetPath $Assets[$i] |
| 65 | +} |
0 commit comments