Skip to content

Commit a8d8920

Browse files
committed
improved release.ps1 script robustness.
1 parent 5f00163 commit a8d8920

File tree

1 file changed

+109
-27
lines changed

1 file changed

+109
-27
lines changed

release.ps1

Lines changed: 109 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,18 @@ if ($GhAvailable -and $RepoName) {
118118
$TagExistsLocal = $false
119119
$TagExistsRemote = $false
120120

121-
try {
122-
$null = git rev-parse "$Version" 2>&1
121+
# Check local tag
122+
$null = git rev-parse "$Version" 2>&1 | Out-Null
123+
if ($LASTEXITCODE -eq 0) {
123124
$TagExistsLocal = $true
124-
} catch {
125-
$TagExistsLocal = $false
126125
}
127126

128-
try {
129-
$RemoteTags = git ls-remote --tags origin 2>&1
130-
if ($RemoteTags -match "refs/tags/$Version") {
127+
# Check remote tag
128+
$RemoteTagsCheck = git ls-remote --tags origin 2>&1
129+
if ($LASTEXITCODE -eq 0) {
130+
if ($RemoteTagsCheck -match "refs/tags/$Version`$") {
131131
$TagExistsRemote = $true
132132
}
133-
} catch {
134-
$TagExistsRemote = $false
135133
}
136134

137135
# If release or tag exists, delete them
@@ -161,19 +159,25 @@ if ($ReleaseExists -or $TagExistsLocal -or $TagExistsRemote) {
161159
# Delete remote tag if it exists
162160
if ($TagExistsRemote) {
163161
Write-Info "Deleting remote tag..."
164-
try {
165-
git push origin ":refs/tags/$Version" 2>&1 | Out-Null
162+
$DeleteRemoteOutput = git push origin ":refs/tags/$Version" 2>&1
163+
if ($LASTEXITCODE -eq 0) {
166164
Write-Success "Remote tag deleted"
167-
} catch {
165+
} else {
168166
Write-Warn "Failed to delete remote tag (may not exist or already deleted)"
167+
Write-Host "Error: $DeleteRemoteOutput"
169168
}
170169
}
171170

172171
# Delete local tag if it exists
173172
if ($TagExistsLocal) {
174173
Write-Info "Deleting local tag..."
175-
git tag -d "$Version" 2>&1 | Out-Null
176-
Write-Success "Local tag deleted"
174+
$DeleteLocalOutput = git tag -d "$Version" 2>&1
175+
if ($LASTEXITCODE -eq 0) {
176+
Write-Success "Local tag deleted"
177+
} else {
178+
Write-Warn "Failed to delete local tag"
179+
Write-Host "Error: $DeleteLocalOutput"
180+
}
177181
}
178182
}
179183

@@ -210,23 +214,101 @@ if ($Response -notmatch '^[Yy]$') {
210214
exit 0
211215
}
212216

213-
# Create the tag
214-
Write-Info "Creating tag $Version..."
215-
git tag -a "$Version" -m "Release $Version" 2>&1 | Out-Null
216-
Write-Success "Tag created locally"
217+
# Check if tag already exists locally (shouldn't happen due to earlier deletion, but double-check)
218+
$TagExistsNow = $false
219+
$null = git rev-parse "$Version" 2>&1 | Out-Null
220+
if ($LASTEXITCODE -eq 0) {
221+
$TagExistsNow = $true
222+
}
223+
224+
# Create the tag if it doesn't exist
225+
if (-not $TagExistsNow) {
226+
Write-Info "Creating tag $Version..."
227+
$TagOutput = git tag -a "$Version" -m "Release $Version" 2>&1
228+
if ($LASTEXITCODE -ne 0) {
229+
Write-Err "Failed to create tag"
230+
Write-Host "Error: $TagOutput"
231+
exit 1
232+
}
233+
Write-Success "Tag created locally"
234+
} else {
235+
Write-Info "Tag $Version already exists locally, skipping creation"
236+
}
217237

218-
# Push the tag
219-
Write-Info "Pushing tag to remote..."
220-
try {
221-
git push origin "$Version" 2>&1 | Out-Null
222-
Write-Success "Tag pushed successfully"
223-
} catch {
224-
Write-Err "Failed to push tag"
225-
Write-Warn "Tag was created locally but not pushed"
226-
Write-Host "You can push it manually with: git push origin $Version"
238+
# Check if tag already exists remotely before pushing
239+
Write-Info "Checking if tag exists on remote..."
240+
$RemoteTagsOutput = git ls-remote --tags origin 2>&1
241+
if ($LASTEXITCODE -ne 0) {
242+
Write-Err "Failed to check remote tags"
243+
Write-Host "Error: $RemoteTagsOutput"
227244
exit 1
228245
}
229246

247+
# Check if tag exists (match the exact tag name, not just part of it)
248+
$TagExistsRemote = $false
249+
if ($RemoteTagsOutput -match "refs/tags/$Version`$") {
250+
$TagExistsRemote = $true
251+
}
252+
253+
# Push the tag
254+
if ($TagExistsRemote) {
255+
Write-Info "Tag already exists on remote, verifying..."
256+
# Verify it's the same tag
257+
$LocalTagCommit = git rev-parse "$Version^{}" 2>&1
258+
if ($LASTEXITCODE -ne 0) {
259+
Write-Err "Failed to get local tag commit"
260+
Write-Host "Error: $LocalTagCommit"
261+
exit 1
262+
}
263+
264+
$RemoteTagCommitOutput = git ls-remote origin "refs/tags/$Version" 2>&1
265+
if ($LASTEXITCODE -ne 0) {
266+
Write-Err "Failed to get remote tag commit"
267+
Write-Host "Error: $RemoteTagCommitOutput"
268+
exit 1
269+
}
270+
271+
if ($RemoteTagCommitOutput -match "(\S+)\s+refs/tags/$Version") {
272+
$RemoteCommit = $matches[1]
273+
if ($LocalTagCommit -eq $RemoteCommit) {
274+
Write-Success "Tag already exists on remote and matches local tag"
275+
} else {
276+
Write-Warn "Tag exists on remote but points to different commit"
277+
Write-Warn "Local: $LocalTagCommit"
278+
Write-Warn "Remote: $RemoteCommit"
279+
Write-Err "Cannot push - tag conflict. Please resolve manually."
280+
exit 1
281+
}
282+
} else {
283+
Write-Warn "Could not parse remote tag information"
284+
Write-Warn "Assuming tag exists and matches (manual verification recommended)"
285+
}
286+
} else {
287+
Write-Info "Pushing tag to remote..."
288+
$PushOutput = git push origin "$Version" 2>&1
289+
$PushExitCode = $LASTEXITCODE
290+
291+
if ($PushExitCode -ne 0) {
292+
Write-Err "Failed to push tag (exit code: $PushExitCode)"
293+
Write-Host "Error output:"
294+
Write-Host $PushOutput
295+
Write-Warn "Tag was created locally but not pushed"
296+
Write-Host "You can push it manually with: git push origin $Version"
297+
exit 1
298+
}
299+
300+
# Verify the push succeeded by checking remote
301+
Start-Sleep -Milliseconds 500 # Brief delay for remote to update
302+
$VerifyTagsOutput = git ls-remote --tags origin 2>&1
303+
if ($LASTEXITCODE -eq 0 -and $VerifyTagsOutput -match "refs/tags/$Version`$") {
304+
Write-Success "Tag pushed successfully and verified on remote"
305+
} else {
306+
Write-Warn "Tag push reported success but verification failed"
307+
Write-Warn "Tag may still be syncing. Please verify manually:"
308+
Write-Host " git ls-remote --tags origin | grep $Version"
309+
}
310+
}
311+
230312
# Print success message
231313
Write-Host ""
232314
Write-Success "=========================================="

0 commit comments

Comments
 (0)