Skip to content

Commit 19d384f

Browse files
committed
Improve release detection with multiple fallback methods
- Add three methods to detect existing releases (gh view, gh list, API) - Improve error messages to be more helpful and actionable - Handle edge case where release exists but initial check missed it - Add better diagnostics for troubleshooting release creation issues - Make workflow more resilient to GitHub API inconsistencies
1 parent ae935ae commit 19d384f

1 file changed

Lines changed: 59 additions & 7 deletions

File tree

.github/workflows/npm-release-reusable.yml

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,32 @@ jobs:
159159
id: check_release
160160
if: '!inputs.dry-run'
161161
run: |
162+
# Try multiple methods to detect if release exists
163+
RELEASE_EXISTS=false
164+
165+
# Method 1: Try gh release view
162166
if gh release view "v${{ steps.get_version.outputs.version }}" --repo ${{ inputs.repository }} >/dev/null 2>&1; then
167+
RELEASE_EXISTS=true
168+
echo "Found release using 'gh release view'"
169+
fi
170+
171+
# Method 2: Check via gh release list if view failed
172+
if [ "$RELEASE_EXISTS" = "false" ]; then
173+
if gh release list --repo ${{ inputs.repository }} | grep -q "^v${{ steps.get_version.outputs.version }}\s"; then
174+
RELEASE_EXISTS=true
175+
echo "Found release using 'gh release list'"
176+
fi
177+
fi
178+
179+
# Method 3: Check via GitHub API directly
180+
if [ "$RELEASE_EXISTS" = "false" ]; then
181+
if gh api "repos/${{ inputs.repository }}/releases/tags/v${{ steps.get_version.outputs.version }}" >/dev/null 2>&1; then
182+
RELEASE_EXISTS=true
183+
echo "Found release using GitHub API"
184+
fi
185+
fi
186+
187+
if [ "$RELEASE_EXISTS" = "true" ]; then
163188
echo "exists=true" >> $GITHUB_OUTPUT
164189
echo "::warning::Release v${{ steps.get_version.outputs.version }} already exists - skipping release creation"
165190
else
@@ -193,15 +218,42 @@ jobs:
193218
- name: Handle release creation failure
194219
if: ${{ !inputs.dry-run && steps.check_release.outputs.exists == 'false' && steps.create_release.outcome == 'failure' }}
195220
run: |
196-
echo "::warning::Failed to create release for existing tag v${{ steps.get_version.outputs.version }}"
197-
echo "This might happen if:"
198-
echo " - The release was partially created in a previous run"
199-
echo " - There's a permission issue with the GitHub token"
200-
echo "Attempting to verify if release exists now..."
221+
echo "::warning::Failed to create release for tag v${{ steps.get_version.outputs.version }}"
222+
echo "This typically happens when a release already exists from a previous run."
223+
echo "Attempting to verify if release exists now using multiple methods..."
224+
225+
RELEASE_FOUND=false
226+
227+
# Method 1: Try gh release view
201228
if gh release view "v${{ steps.get_version.outputs.version }}" --repo ${{ inputs.repository }} >/dev/null 2>&1; then
202-
echo "::notice::Release exists for v${{ steps.get_version.outputs.version }} (may have been created in a previous run)"
229+
RELEASE_FOUND=true
230+
echo "✓ Found release using 'gh release view'"
231+
fi
232+
233+
# Method 2: Check via gh release list
234+
if [ "$RELEASE_FOUND" = "false" ]; then
235+
if gh release list --repo ${{ inputs.repository }} | grep -q "^v${{ steps.get_version.outputs.version }}\s"; then
236+
RELEASE_FOUND=true
237+
echo "✓ Found release using 'gh release list'"
238+
fi
239+
fi
240+
241+
# Method 3: Check via GitHub API directly
242+
if [ "$RELEASE_FOUND" = "false" ]; then
243+
if gh api "repos/${{ inputs.repository }}/releases/tags/v${{ steps.get_version.outputs.version }}" >/dev/null 2>&1; then
244+
RELEASE_FOUND=true
245+
echo "✓ Found release using GitHub API"
246+
fi
247+
fi
248+
249+
if [ "$RELEASE_FOUND" = "true" ]; then
250+
echo "::notice::Release already exists for v${{ steps.get_version.outputs.version }}"
251+
echo "The workflow can continue normally. The release was likely created in a previous run."
203252
else
204-
echo "::error::Unable to create release for v${{ steps.get_version.outputs.version }}"
253+
echo "::error::Unable to create or find release for v${{ steps.get_version.outputs.version }}"
254+
echo "This is unexpected. Please check:"
255+
echo " - GitHub token permissions (needs repo scope)"
256+
echo " - Repository name is correct: ${{ inputs.repository }}"
205257
exit 1
206258
fi
207259
env:

0 commit comments

Comments
 (0)