Skip to content

Commit edcbaac

Browse files
Refactor PowerShell script for release workflow
Signed-off-by: Igor Mundstein <3512138+IgorMundstein@users.noreply.github.com>
1 parent 5965bdc commit edcbaac

1 file changed

Lines changed: 43 additions & 56 deletions

File tree

.github/workflows/release.yml

Lines changed: 43 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ jobs:
4343
git fetch --tags
4444
$currentTag = git tag --sort=-creatordate | Where-Object { $_ -match '^\d+\.\d+\.\d+$' } | Select-Object -First 1
4545
if (-not $currentTag) {
46-
$major = 1
47-
$minor = 0
48-
$patch = 0
46+
$major = 1; $minor = 0; $patch = 0
4947
} else {
5048
$parts = $currentTag.Split('.')
5149
$major = [int]$parts[0]
@@ -87,8 +85,10 @@ jobs:
8785

8886
- name: Capture window screenshot from SIGNED exe
8987
if: steps.signpath.conclusion == 'success'
88+
id: capture_screenshot
9089
shell: pwsh
9190
run: |
91+
# PowerShell script remains the same
9292
$ErrorActionPreference = "Stop"
9393
$appPath = ".\WinMemoryCleaner.exe"
9494
if (-not (Test-Path $appPath)) {
@@ -97,12 +97,9 @@ jobs:
9797
}
9898
$windowScreenshotPath = "$pwd\main-window-raw.png"
9999
$proc = Start-Process -FilePath $appPath -PassThru
100-
$maxWaitMs = 15000
101-
$elapsed = 0
100+
$maxWaitMs = 15000; $elapsed = 0
102101
while ($proc.MainWindowHandle -eq 0 -and $elapsed -lt $maxWaitMs) {
103-
Start-Sleep -Milliseconds 200
104-
$proc.Refresh()
105-
$elapsed += 200
102+
Start-Sleep -Milliseconds 200; $proc.Refresh(); $elapsed += 200
106103
}
107104
if ($proc.MainWindowHandle -eq 0) {
108105
Write-Host "::error::Main window not found for signed EXE."
@@ -116,26 +113,22 @@ jobs:
116113
$rect = New-Object Win32+RECT
117114
if (-not [Win32]::GetWindowRect([IntPtr]$proc.MainWindowHandle, [ref]$rect)) {
118115
Write-Host "::error::GetWindowRect failed."
119-
Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue
120-
exit 1
116+
Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue; exit 1
121117
}
122-
$width = $rect.Right - $rect.Left
123-
$height = $rect.Bottom - $rect.Top
118+
$width = $rect.Right - $rect.Left; $height = $rect.Bottom - $rect.Top
124119
if ($width -le 0 -or $height -le 0) {
125120
Write-Host "::error::Invalid window size: $($width)x$($height)"
126-
Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue
127-
exit 1
121+
Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue; exit 1
128122
}
129123
$wbmp = New-Object System.Drawing.Bitmap $width, $height
130124
$wgfx = [System.Drawing.Graphics]::FromImage($wbmp)
131125
$wgfx.CopyFromScreen($rect.Left, $rect.Top, 0, 0, (New-Object System.Drawing.Size($width, $height)))
132126
$wbmp.Save($windowScreenshotPath, [System.Drawing.Imaging.ImageFormat]::Png)
133-
$wgfx.Dispose()
134-
$wbmp.Dispose()
127+
$wgfx.Dispose(); $wbmp.Dispose()
135128
Stop-Process -Id $proc.Id -Force
136129
137130
- name: Upload screenshot artifact
138-
if: steps.signpath.conclusion == 'success'
131+
if: steps.capture_screenshot.outcome == 'success'
139132
uses: actions/upload-artifact@v4
140133
with:
141134
name: release-window-screenshot
@@ -161,87 +154,73 @@ jobs:
161154
name: Create GitHub Release
162155
needs: build
163156
runs-on: windows-latest
164-
165157
steps:
166158
- name: Checkout
167159
uses: actions/checkout@v4
168-
169160
- name: Download release artifacts
170161
uses: actions/download-artifact@v4
171162
with:
172163
name: winmemorycleaner-release-${{ needs.build.outputs.new_tag }}
173164
path: release_artifacts
174-
175165
- name: Create Checksums
176166
shell: pwsh
177167
working-directory: release_artifacts
178168
run: |
179169
Get-FileHash -Algorithm SHA256 -Path "WinMemoryCleaner.exe", "WinMemoryCleaner.zip" | ForEach-Object {
180170
$_.Hash.ToLower() + " " + (Split-Path -Leaf $_.Path)
181171
} | Out-File -FilePath "checksums.txt" -Encoding utf8
182-
Write-Host "Generated checksums.txt:"
183-
cat checksums.txt
184-
172+
Write-Host "Generated checksums.txt:"; cat checksums.txt
185173
- name: Create or reuse version tag
186174
shell: pwsh
187175
run: |
188176
$tag = "${{ needs.build.outputs.new_tag }}"
189177
git fetch --tags
190-
$tagExists = git tag -l $tag
191-
if (-not $tagExists) {
192-
git config user.name "github-actions"
193-
git config user.email "github-actions@github.com"
178+
if (-not (git tag -l $tag)) {
179+
git config user.name "github-actions"; git config user.email "github-actions@github.com"
194180
git tag $tag
195181
git push origin $tag
196182
} else {
197183
Write-Host "Tag '$tag' already exists. Reusing..."
198184
}
199-
200185
- name: Check if release already exists
201186
id: check_release
202187
shell: pwsh
188+
env:
189+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
203190
run: |
204191
$tag = "${{ needs.build.outputs.new_tag }}"
205192
$headers = @{ Authorization = "Bearer $env:GITHUB_TOKEN" }
206193
$uri = "https://api.github.com/repos/${{ github.repository }}/releases/tags/$tag"
207194
try {
208-
$response = Invoke-RestMethod -Uri $uri -Headers $headers -Method GET
195+
Invoke-RestMethod -Uri $uri -Headers $headers -Method GET
209196
Write-Host "Release already exists for tag $tag."
210197
"skip=true" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append
211198
} catch {
212199
if ($_.Exception.Response.StatusCode.value__ -eq 404) {
213200
Write-Host "No release exists for tag $tag. Proceeding."
214201
"skip=false" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append
215202
} else {
216-
Write-Error "Unexpected error: $($_.Exception.Message)"
217-
exit 1
203+
Write-Error "Unexpected error: $($_.Exception.Message)"; exit 1
218204
}
219205
}
220-
env:
221-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
222-
223206
- name: Get commit date
224207
id: commit_date
225208
shell: pwsh
226209
run: |
227210
$date = (git show -s --format=%cd --date=short ${{ github.event.head_commit.id }}).Trim()
228-
echo "date=$date" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append
229-
211+
"date=$date" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append
230212
- name: Format commit message for release notes
231213
id: formatted_message
232214
shell: pwsh
233215
env:
234216
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
235217
run: |
236218
$msg = @()
237-
foreach ($line in $env:COMMIT_MESSAGE -split "`n") {
238-
$msg += "- $line"
239-
}
219+
foreach ($line in $env:COMMIT_MESSAGE -split "`n") { $msg += "- $line" }
240220
$body = $msg -join "`n"
241-
echo "body<<EOF" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append
242-
echo "$body" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append
243-
echo "EOF" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append
244-
221+
"body<<EOF" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append
222+
"$body" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append
223+
"EOF" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append
245224
- name: Upload GitHub release assets
246225
if: steps.check_release.outputs.skip == 'false'
247226
uses: softprops/action-gh-release@v2
@@ -284,43 +263,51 @@ jobs:
284263
git pull --rebase
285264
286265
- name: Download window-only screenshot
266+
id: download_screenshot
287267
uses: actions/download-artifact@v4
288268
with:
289269
name: release-window-screenshot
290270
path: ./ui-window
271+
continue-on-error: true
291272

273+
- name: Check if screenshot exists
274+
id: check_screenshot
275+
if: steps.download_screenshot.outcome == 'success'
276+
shell: bash
277+
run: |
278+
if [ -f "./ui-window/main-window-raw.png" ]; then
279+
echo "Screenshot artifact found. Proceeding with image update."
280+
echo "screenshot_exists=true" >> $GITHUB_OUTPUT
281+
else
282+
echo "::warning::Screenshot artifact downloaded, but main-window-raw.png is missing. Skipping image update."
283+
echo "screenshot_exists=false" >> $GITHUB_OUTPUT
284+
fi
285+
292286
- name: Install ImageMagick
287+
if: steps.check_screenshot.outputs.screenshot_exists == 'true'
293288
run: |
294289
sudo apt-get update
295290
sudo apt-get install -y imagemagick
296291
297292
- name: Add transparent rounded corners and write image
298-
id: image_dimensions
293+
if: steps.check_screenshot.outputs.screenshot_exists == 'true'
299294
shell: bash
300295
run: |
301296
set -euo pipefail
302297
mkdir -p docs/assets/images
303298
FILE="./ui-window/main-window-raw.png"
304-
if [ ! -f "$FILE" ]; then
305-
echo "::error::main-window-raw.png not found in artifact"
306-
exit 1
307-
fi
308299
read W H < <(identify -format "%w %h" "$FILE")
309-
if [ -z "${W:-}" ] || [ -z "${H:-}" ]; then
310-
echo "::error::Failed to read image size"
311-
exit 1
312-
fi
300+
if [ -z "${W:-}" ] || [ -z "${H:-}" ]; then echo "::error::Failed to read image size"; exit 1; fi
313301
if [ "$W" -lt "$H" ]; then MIN=$W; else MIN=$H; fi
314-
R=$(( MIN / 50 ))
315-
if [ "$R" -lt 10 ]; then R=10; fi
316-
if [ "$R" -gt 24 ]; then R=24; fi
302+
R=$(( MIN / 50 )); if [ "$R" -lt 10 ]; then R=10; fi; if [ "$R" -gt 24 ]; then R=24; fi
317303
magick "$FILE" \
318304
\( -size "${W}x${H}" xc:none -fill white -draw "roundrectangle 0,0,$((W-1)),$((H-1)),$R,$R" \) \
319305
-compose DstIn -composite \
320306
docs/assets/images/main-window.png
321307
322308
- name: Create Pull Request for Updated Screenshot
323309
id: cpr
310+
if: steps.check_screenshot.outputs.screenshot_exists == 'true'
324311
uses: peter-evans/create-pull-request@v6
325312
with:
326313
token: ${{ secrets.PAT_TOKEN }}
@@ -336,7 +323,7 @@ jobs:
336323
docs/assets/images/main-window.png
337324
338325
- name: Enable Auto-Merge for the PR
339-
if: steps.cpr.outputs.pull-request-number != ''
326+
if: steps.check_screenshot.outputs.screenshot_exists == 'true' && steps.cpr.outputs.pull-request-number != ''
340327
env:
341328
GH_TOKEN: ${{ secrets.PAT_TOKEN }}
342329
PR_NUMBER: ${{ steps.cpr.outputs.pull-request-number }}

0 commit comments

Comments
 (0)