Skip to content

Commit f45eda9

Browse files
Added new PowerShell script for windows support (#1013)
* Added new powershell script for windows support * Reverted removal of upload windows binaries --------- Co-authored-by: ec2-bot 🤖 <[email protected]>
1 parent 406ae36 commit f45eda9

File tree

2 files changed

+127
-2
lines changed

2 files changed

+127
-2
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ upload-resources-to-github:
130130
${MAKEFILE_PATH}/scripts/upload-resources-to-github -k -s ${K8S_1_25_ASSET_SUFFIX}
131131

132132
upload-resources-to-github-windows:
133-
${MAKEFILE_PATH}/scripts/upload-resources-to-github -b
133+
powershell -File ${MAKEFILE_PATH}/scripts/upload-resources-to-github-windows.ps1 -BinariesOnly
134134

135135
generate-k8s-yaml:
136136
${MAKEFILE_PATH}/scripts/generate-k8s-yaml
@@ -165,7 +165,7 @@ eks-cluster-test:
165165

166166
release: build-binaries build-docker-images push-docker-images generate-k8s-yaml upload-resources-to-github
167167

168-
release-windows: build-binaries-windows build-docker-images-windows push-docker-images-windows
168+
release-windows: build-binaries-windows build-docker-images-windows push-docker-images-windows upload-resources-to-github-windows
169169

170170
test: spellcheck shellcheck unit-test e2e-test compatibility-test license-test go-linter helm-version-sync-test helm-lint
171171

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# upload-resources-to-github-windows.ps1
2+
3+
# PowerShell script to upload windows release assets to Github.
4+
# This script cleans up after itself in cases of parital failures. i.e. either all assets are uploaded or none
5+
6+
$USAGE = @'
7+
Usage: upload-resources-to-github-windows.ps1 [-BinariesOnly]
8+
Upload windows release assets to GitHub. Release assets include binaries for supported platforms and K8s resources for supported versions.
9+
10+
Options:
11+
-BinariesOnly Upload binaries only
12+
-K8sAssetsOnly Upload only the K8s Assets
13+
-Suffix <suffix> String appended to resource file names
14+
'@
15+
16+
function usage {
17+
Write-Output $USAGE
18+
}
19+
20+
param(
21+
[switch]$BinariesOnly,
22+
[switch]$K8sAssetsOnly,
23+
[switch]$Suffix = ""
24+
)
25+
26+
# Check if no options are provided or invalid options are used
27+
if (-not $BinariesOnly -and -not $K8sAssetsOnly -and $Suffix -eq "") {
28+
usage
29+
exit 1
30+
}
31+
32+
$ErrorActionPreference = "Stop"
33+
34+
# Function to handle errors and cleanup any partially uploaded assets
35+
function HandleErrorsAndCleanup {
36+
param (
37+
[int]$ExitCode
38+
)
39+
if ($ExitCode -eq 0) {
40+
exit 0
41+
}
42+
if ($global:AssetIdsUploaded.Count -ne 0) {
43+
Write-Output "`nCleaning up assets uploaded in the current execution of the script"
44+
foreach ($assetId in $global:AssetIdsUploaded) {
45+
Write-Output "Deleting asset $assetId"
46+
Invoke-RestMethod -Method Delete -Uri "https://api.github.com/repos/aws/aws-node-termination-handler/releases/assets/$assetId" -Headers @{Authorization = "token $env:GITHUB_TOKEN"}
47+
}
48+
exit $ExitCode
49+
}
50+
}
51+
52+
# Function to upload an asset to GitHub
53+
function UploadAsset {
54+
param (
55+
[string]$AssetPath
56+
)
57+
$ContentType = [System.Web.MimeMapping]::GetMimeMapping($AssetPath)
58+
$Headers = @{
59+
Authorization = "token $env:GITHUB_TOKEN"
60+
'Content-Type' = $ContentType
61+
}
62+
$Uri = "https://uploads.github.com/repos/aws/aws-node-termination-handler/releases/$ReleaseId/assets?name=$(Split-Path -Leaf $AssetPath)"
63+
64+
try {
65+
$Response = Invoke-RestMethod -Method Post -Uri $Uri -Headers $Headers -InFile $AssetPath -ErrorAction Stop
66+
if ($Response -and $Response.id) {
67+
$global:AssetIdsUploaded += $Response.id
68+
Write-Output "Created asset ID $($Response.id) successfully"
69+
} else {
70+
Write-Output "❌ Upload failed with response message: $($Response | ConvertTo-Json)"
71+
exit 1
72+
}
73+
} catch {
74+
Write-Output "❌ Upload failed for $AssetPath with error: $_"
75+
exit 1
76+
}
77+
}
78+
79+
# Initialize global variables
80+
$global:AssetIdsUploaded = @()
81+
trap { HandleErrorsAndCleanup -ExitCode $global:LASTEXITCODE }
82+
83+
$ScriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
84+
$Version = & make -s -f "$ScriptPath/../Makefile" version
85+
$BuildDir = "$ScriptPath/../build/k8s-resources/$Version"
86+
$BinaryDir = "$ScriptPath/../build/bin"
87+
88+
# Set the TLS version, powershell is supported in GitHub actions using Tls
89+
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
90+
91+
try {
92+
$Response = (Invoke-RestMethod -Uri "https://api.github.com/repos/aws/aws-node-termination-handler/releases" -Headers @{Authorization = "token $env:GITHUB_TOKEN"})
93+
} catch {
94+
Write-Output "Failed to retrieve releases from GitHub: $_"
95+
exit 1
96+
}
97+
98+
$release = $Response | Where-Object { $_.tag_name -eq $Version }
99+
100+
Write-Output "Latest Release"
101+
Write-Output $release
102+
103+
$ReleaseId = $release.id
104+
Write-Output "Release ID: $ReleaseId "
105+
106+
if (-not $ReleaseId) {
107+
Write-Output "❌ Failed to find release ID for version $Version"
108+
exit 1
109+
}
110+
111+
# Gather assets to upload
112+
$Assets = @()
113+
if ($BinariesOnly) {
114+
$Assets += Get-ChildItem -Path $BinaryDir | ForEach-Object { $_.FullName }
115+
}
116+
if (-not $BinariesOnly) {
117+
$Assets += "$BuildDir\individual-resources.tar", "$BuildDir\all-resources.yaml", "$BuildDir\individual-resources-queue-processor.tar", "$BuildDir\all-resources-queue-processor.yaml"
118+
}
119+
120+
# Upload each asset
121+
Write-Output "`nUploading release assets for release id '$ReleaseId' to Github"
122+
foreach ($Asset in $Assets) {
123+
Write-Output "`n Uploading $($Asset | Split-Path -Leaf)"
124+
UploadAsset -AssetPath $Asset
125+
}

0 commit comments

Comments
 (0)