-
Notifications
You must be signed in to change notification settings - Fork 3
284 lines (232 loc) · 12.3 KB
/
publish-krewplugin.yaml
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
name: Publish Plugin to Krew
on:
push:
tags:
- 'v*'
release:
types:
- published
workflow_dispatch:
jobs:
publish:
runs-on: ubuntu-latest # Run everything in Linux
steps:
# Checkout the repository
- name: Checkout code
uses: actions/checkout@v3
with:
ref: ${{ github.ref }} # Check out the branch that triggered the workflow
# Update the KubeTidy.psm1 file for Krew Plugin using PowerShell
- name: Update KubeTidy.psm1 for Krew Plugin
shell: pwsh # Use PowerShell
run: |
# Create a copy of KubeTidy.psm1 directly in the root
$sourceFile = "KubeTidy.psm1"
$destinationFile = "kubectl-KubeTidy"
Copy-Item -Path $sourceFile -Destination $destinationFile -Force
# Read the content of the destination file
$content = Get-Content $destinationFile -Raw
# Create a regex pattern to capture the entire parameter block
$pattern = '# START PARAM BLOCK[\s\S]*?# END PARAM BLOCK' # Capture the entire block, including newlines
# Check if the parameter block exists
if ($content -match $pattern) {
# Capture the parameter block for verification
$paramBlock = $matches[0]
# Remove the captured parameter block from the original content
$content = $content -replace $pattern, '' # Remove the entire block
# Replace the MARKER: NEW PARAM BLOCK line with the new parameter block
$content = $content -replace '# MARKER: NEW PARAM BLOCK', $paramBlock
# Prepare the new function call to be inserted
$functionCall = @"
# Call the function, passing parameters manually for cross-platform compatibility
Invoke-KubeTidy \$PSBoundParameters
"@
# Replace the MARKER: FUNCTION CALL line with the new function call
$content = $content -replace '# MARKER: FUNCTION CALL', $functionCall
# Save the modified content back to the original script file
Set-Content -Path $destinationFile -Value $content
Write-Host "$destinationFile has been modified successfully."
} else {
Write-Host "No parameter block found to remove."
}
# Validate PowerShell Code
- name: Run PSScriptAnalyzer and format output in Markdown
run: |
# Run ScriptAnalyzer and capture the output
$results = Invoke-ScriptAnalyzer -Path ./ -Recurse -Severity 'Error', 'Warning' -ExcludeRule PSAvoidUsingWriteHost
# Separate the warnings and errors
$warnings = $results | Where-Object { $_.Severity -eq 'Warning' }
$errors = $results | Where-Object { $_.Severity -eq 'Error' }
# Debug: Output the count of warnings and errors
Write-Host "Found $($warnings.Count) warnings"
Write-Host "Found $($errors.Count) errors"
# Create Markdown formatted tables
function ConvertTo-MarkdownTable {
param ($items)
$header = "| RuleName | Severity | ScriptName | Line | Message |"
$separator = "| --- | --- | --- | --- | --- |"
$rows = foreach ($item in $items) {
"| $($item.RuleName) | $($item.Severity) | $($item.ScriptName) | $($item.Line) | $($item.Message) |"
}
# Join rows into a single string
return "$header`n$separator`n$($rows -join "`n")"
}
# Append warnings to the GitHub Actions summary (if any)
if ($warnings.Count -gt 0) {
$warningTable = ConvertTo-MarkdownTable -items $warnings
Add-Content -Path $env:GITHUB_STEP_SUMMARY -Value "### PSScriptAnalyzer Warnings`n"
Add-Content -Path $env:GITHUB_STEP_SUMMARY -Value "$warningTable`n"
} else {
Add-Content -Path $env:GITHUB_STEP_SUMMARY -Value "### No Warnings Found`n"
}
# Append errors to the GitHub Actions summary (if any)
if ($errors.Count -gt 0) {
$errorTable = ConvertTo-MarkdownTable -items $errors
Add-Content -Path $env:GITHUB_STEP_SUMMARY -Value "### PSScriptAnalyzer Errors`n"
Add-Content -Path $env:GITHUB_STEP_SUMMARY -Value "$errorTable`n"
} else {
Add-Content -Path $env:GITHUB_STEP_SUMMARY -Value "### No Errors Found`n"
}
# Fail the job if there are any errors
if ($errors.Count -gt 0) {
Write-Error "PSScriptAnalyzer found errors."
}
shell: pwsh
# Convert line endings from Windows (CRLF) to Unix (LF)
- name: Convert Line Endings
shell: pwsh # Use PowerShell
run: |
(Get-Content "kubectl-KubeTidy" -Raw) -replace "`r`n", "`n" | Set-Content "kubectl-KubeTidy"
# Create tar.gz files for Linux, Darwin (AMD64, ARM64)
- name: Create Tar Files
run: |
# Get the version from the tag
version="${{ github.ref_name }}"
# Create output directory for the tar files
outputDir="krewplugin"
mkdir -p "$outputDir"
# Create platform-specific directories
linuxAmd64Dir="$outputDir/linux_amd64"
linuxArm64Dir="$outputDir/linux_arm64"
darwinAmd64Dir="$outputDir/darwin_amd64"
darwinArm64Dir="$outputDir/darwin_arm64"
mkdir -p "$linuxAmd64Dir" "$linuxArm64Dir" "$darwinAmd64Dir" "$darwinArm64Dir"
# Copy the kubectl-KubeTidy script to each platform directory
cp "kubectl-KubeTidy" "$linuxAmd64Dir/kubectl-KubeTidy"
cp "kubectl-KubeTidy" "$linuxArm64Dir/kubectl-KubeTidy"
cp "kubectl-KubeTidy" "$darwinAmd64Dir/kubectl-KubeTidy"
cp "kubectl-KubeTidy" "$darwinArm64Dir/kubectl-KubeTidy"
# Copy the Private folder to each platform directory
cp -r "Private" "$linuxAmd64Dir/Private"
cp -r "Private" "$linuxArm64Dir/Private"
cp -r "Private" "$darwinAmd64Dir/Private"
cp -r "Private" "$darwinArm64Dir/Private"
# Set execute permission for the kubectl-KubeTidy script
chmod +x "$linuxAmd64Dir/kubectl-KubeTidy"
chmod +x "$linuxArm64Dir/kubectl-KubeTidy"
chmod +x "$darwinAmd64Dir/kubectl-KubeTidy"
chmod +x "$darwinArm64Dir/kubectl-KubeTidy"
# Create tar.gz files for all platforms using the tar command
tar -czf "$outputDir/KubeTidy-linux-amd64-$version.tar.gz" -C "$linuxAmd64Dir" .
tar -czf "$outputDir/KubeTidy-linux-arm64-$version.tar.gz" -C "$linuxArm64Dir" .
tar -czf "$outputDir/KubeTidy-darwin-amd64-$version.tar.gz" -C "$darwinAmd64Dir" .
tar -czf "$outputDir/KubeTidy-darwin-arm64-$version.tar.gz" -C "$darwinArm64Dir" .
# Generate SHA256 Checksums
- name: Generate SHA256 Checksums
run: |
version="${{ github.ref_name }}"
outputDir="krewplugin"
# Generate checksums for all tar.gz files
for file in "$outputDir"/*.tar.gz; do
sha256sum "$file" | awk '{print $1 " " $2}' > "${file}.sha256"
done
# Update KubeTidy.yaml with the new version, URLs, and checksums
- name: Update KubeTidy.yaml
run: |
version="${{ github.ref_name }}"
outputDir="krewplugin"
# Extract SHA256 checksums for each platform
sha_linux_amd64=$(cat "$outputDir/KubeTidy-linux-amd64-$version.tar.gz.sha256" | awk '{print $1}')
sha_linux_arm64=$(cat "$outputDir/KubeTidy-linux-arm64-$version.tar.gz.sha256" | awk '{print $1}')
sha_darwin_amd64=$(cat "$outputDir/KubeTidy-darwin-amd64-$version.tar.gz.sha256" | awk '{print $1}')
sha_darwin_arm64=$(cat "$outputDir/KubeTidy-darwin-arm64-$version.tar.gz.sha256" | awk '{print $1}')
# Read the KubeTidy.yaml template and update it with the new version and checksums
# Update version
sed -i "s/version:.*/version: \"$version\"/" KubeTidy.yaml
# Update URLs and corresponding SHA256 checksums for each platform
# Linux AMD64
sed -i "s#uri: .*linux-amd64.*#uri: https://github.com/KubeDeckio/KubeTidy/releases/download/$version/KubeTidy-linux-amd64-$version.tar.gz#" KubeTidy.yaml
sed -i "/uri: .*linux-amd64.*/!b;n;s/sha256: \".*\"/sha256: \"$sha_linux_amd64\"/" KubeTidy.yaml
# Linux ARM64
sed -i "s#uri: .*linux-arm64.*#uri: https://github.com/KubeDeckio/KubeTidy/releases/download/$version/KubeTidy-linux-arm64-$version.tar.gz#" KubeTidy.yaml
sed -i "/uri: .*linux-arm64.*/!b;n;s/sha256: \".*\"/sha256: \"$sha_linux_arm64\"/" KubeTidy.yaml
# Darwin AMD64
sed -i "s#uri: .*darwin-amd64.*#uri: https://github.com/KubeDeckio/KubeTidy/releases/download/$version/KubeTidy-darwin-amd64-$version.tar.gz#" KubeTidy.yaml
sed -i "/uri: .*darwin-amd64.*/!b;n;s/sha256: \".*\"/sha256: \"$sha_darwin_amd64\"/" KubeTidy.yaml
# Darwin ARM64
sed -i "s#uri: .*darwin-arm64.*#uri: https://github.com/KubeDeckio/KubeTidy/releases/download/$version/KubeTidy-darwin-arm64-$version.tar.gz#" KubeTidy.yaml
sed -i "/uri: .*darwin-arm64.*/!b;n;s/sha256: \".*\"/sha256: \"$sha_darwin_arm64\"/" KubeTidy.yaml
# Create a GitHub release and upload the tar.gz files
- name: Create GitHub Release
id: create_release
uses: actions/create-release@v1
with:
tag_name: ${{ github.ref_name }} # Use the version from the output
release_name: KubeTidy Krew Release ${{ github.ref_name }}
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Upload Release Assets (Including KubeTidy.yaml)
- name: Upload Release Assets
run: |
version="${{ github.ref_name }}"
outputDir="krewplugin"
# Upload each tar.gz file and its corresponding sha256 file
for file in "$outputDir"/*.tar.gz; do
asset_name=$(basename "$file")
checksum_name="${asset_name}.sha256"
gh release upload "$version" "$file" --clobber
gh release upload "$version" "${file}.sha256" --clobber
done
# Upload KubeTidy.yaml to the release
gh release upload "$version" "KubeTidy.yaml" --clobber
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Update release notes with CHANGELOG.md content
- name: Update Release Notes
shell: pwsh # Use PowerShell
run: |
$gitVersion = "${{ github.ref_name }}"
$version = $gitVersion -replace 'v',''
$changelogPath = "CHANGELOG.md"
# Read the entire CHANGELOG.md content
$changelogContent = Get-Content -Path $changelogPath -Raw
# Extract the section for the specific version
if ($changelogContent -match "(?s)## \[$version\].*?(?=## \[|$)") {
$changelog = $matches[0]
} else {
Write-Host "No changelog found for version $version."
exit 1
}
# Save the changelog to a file
$changelog | Out-File -FilePath "release_notes.md" -Encoding utf8
Write-Host "Release notes saved to release_notes.md."
# Check if the file was created successfully
if (-Not (Test-Path "release_notes.md")) {
Write-Host "Failed to create release_notes.md."
exit 1
}
# Use the GitHub CLI to update the release with the notes
gh release edit $gitVersion -F release_notes.md # Use -F to read release notes from the file
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# # New job to publish the plugin to Krew
# publish_krew:
# runs-on: ubuntu-latest
# needs: publish # This ensures this job runs only after the publish job completes successfully
# if: startsWith(github.ref, 'refs/tags/')
# steps:
# # Publish the plugin to Krew
# - name: Update new version in krew-index
# uses: rajatjindal/krew-release-bot@v0.0.46