chore: improve release workflow by adding checks for existing tags and releases, and conditionally bumping version; streamline tag creation process

This commit is contained in:
Luke Hagar
2025-08-14 22:35:07 -05:00
parent 1929932ace
commit c8b881e8c0
2 changed files with 124 additions and 14 deletions

View File

@@ -117,13 +117,67 @@ jobs:
git config user.name "github-actions[bot]" git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com" git config user.email "github-actions[bot]@users.noreply.github.com"
git add package.json dist/ git add package.json dist/
# Check if there are changes to commit
if git diff --staged --quiet; then
echo "No changes to commit, skipping commit"
else
git commit -m "chore: bump version to ${{ steps.bump.outputs.new_version }} and build action" git commit -m "chore: bump version to ${{ steps.bump.outputs.new_version }} and build action"
git push git push
fi
- name: Check if release exists and handle tag/release
id: release_check
uses: actions/github-script@v7
with:
script: |
const { execSync } = require('child_process');
const tagName = 'v${{ steps.bump.outputs.new_version }}';
const tagExists = execSync(`git tag -l "${tagName}"`, { encoding: 'utf8' }).trim() === tagName;
console.log(`Tag ${tagName} exists: ${tagExists}`);
if (tagExists) {
// Check if release exists for this tag
try {
const release = await github.rest.repos.getReleaseByTag({
owner: context.repo.owner,
repo: context.repo.repo,
tag: tagName
});
console.log(`Release for ${tagName} exists: true`);
return { tagExists: true, releaseExists: true };
} catch (error) {
console.log(`Release for ${tagName} exists: false`);
return { tagExists: true, releaseExists: false };
}
} else {
console.log(`Tag ${tagName} does not exist`);
return { tagExists: false, releaseExists: false };
}
- name: Bump version again if release exists
if: steps.release_check.outputs.tagExists == 'true' && steps.release_check.outputs.releaseExists == 'true'
id: rebump
run: |
echo "Release already exists for v${{ steps.bump.outputs.new_version }}, bumping version again"
NEW_VERSION=$(npm version patch --no-git-tag-version)
NEW_VERSION=${NEW_VERSION#v}
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "Bumped to: $NEW_VERSION"
- name: Create and push tag - name: Create and push tag
run: | run: |
git tag v${{ steps.bump.outputs.new_version }} TAG_VERSION="${{ steps.release_check.outputs.releaseExists == 'true' && steps.rebump.outputs.new_version || steps.bump.outputs.new_version }}"
git push origin v${{ steps.bump.outputs.new_version }} echo "Creating tag: v$TAG_VERSION"
# Check if tag already exists
if git tag -l "v$TAG_VERSION" | grep -q "v$TAG_VERSION"; then
echo "Tag v$TAG_VERSION already exists, skipping tag creation"
else
git tag v$TAG_VERSION
git push origin v$TAG_VERSION
fi
- name: Create Release - name: Create Release
id: create_release id: create_release
@@ -131,10 +185,10 @@ jobs:
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with: with:
tag_name: v${{ steps.bump.outputs.new_version }} tag_name: v${{ steps.release_check.outputs.releaseExists == 'true' && steps.rebump.outputs.new_version || steps.bump.outputs.new_version }}
release_name: Release v${{ steps.bump.outputs.new_version }} release_name: Release v${{ steps.release_check.outputs.releaseExists == 'true' && steps.rebump.outputs.new_version || steps.bump.outputs.new_version }}
body: | body: |
## 🎉 Release v${{ steps.bump.outputs.new_version }} ## 🎉 Release v${{ steps.release_check.outputs.releaseExists == 'true' && steps.rebump.outputs.new_version || steps.bump.outputs.new_version }}
### Changes: ### Changes:
${{ steps.commits.outputs.commits }} ${{ steps.commits.outputs.commits }}
@@ -142,7 +196,8 @@ jobs:
### Usage: ### Usage:
Update your workflows to use: Update your workflows to use:
```yaml ```yaml
uses: LukeHagar/usage-statistics@v${{ steps.bump.outputs.new_version }} uses: LukeHagar/usage-statistics@v${{ steps.release_check.outputs.releaseExists == 'true' && steps.rebump.outputs.new_version || steps.bump.outputs.new_version }}
``` ```
draft: false draft: false
prerelease: false prerelease: false
continue-on-error: true

View File

@@ -100,13 +100,67 @@ jobs:
git config user.name "github-actions[bot]" git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com" git config user.email "github-actions[bot]@users.noreply.github.com"
git add package.json dist/ git add package.json dist/
# Check if there are changes to commit
if git diff --staged --quiet; then
echo "No changes to commit, skipping commit"
else
git commit -m "chore: bump version to ${{ steps.bump.outputs.new_version }} and build action" git commit -m "chore: bump version to ${{ steps.bump.outputs.new_version }} and build action"
git push git push
fi
- name: Check if release exists and handle tag/release
id: release_check
uses: actions/github-script@v7
with:
script: |
const { execSync } = require('child_process');
const tagName = 'v${{ steps.bump.outputs.new_version }}';
const tagExists = execSync(`git tag -l "${tagName}"`, { encoding: 'utf8' }).trim() === tagName;
console.log(`Tag ${tagName} exists: ${tagExists}`);
if (tagExists) {
// Check if release exists for this tag
try {
const release = await github.rest.repos.getReleaseByTag({
owner: context.repo.owner,
repo: context.repo.repo,
tag: tagName
});
console.log(`Release for ${tagName} exists: true`);
return { tagExists: true, releaseExists: true };
} catch (error) {
console.log(`Release for ${tagName} exists: false`);
return { tagExists: true, releaseExists: false };
}
} else {
console.log(`Tag ${tagName} does not exist`);
return { tagExists: false, releaseExists: false };
}
- name: Bump version again if release exists
if: steps.release_check.outputs.tagExists == 'true' && steps.release_check.outputs.releaseExists == 'true'
id: rebump
run: |
echo "Release already exists for v${{ steps.bump.outputs.new_version }}, bumping version again"
NEW_VERSION=$(npm version patch --no-git-tag-version)
NEW_VERSION=${NEW_VERSION#v}
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "Bumped to: $NEW_VERSION"
- name: Create and push tag - name: Create and push tag
run: | run: |
git tag v${{ steps.bump.outputs.new_version }} TAG_VERSION="${{ steps.release_check.outputs.releaseExists == 'true' && steps.rebump.outputs.new_version || steps.bump.outputs.new_version }}"
git push origin v${{ steps.bump.outputs.new_version }} echo "Creating tag: v$TAG_VERSION"
# Check if tag already exists
if git tag -l "v$TAG_VERSION" | grep -q "v$TAG_VERSION"; then
echo "Tag v$TAG_VERSION already exists, skipping tag creation"
else
git tag v$TAG_VERSION
git push origin v$TAG_VERSION
fi
- name: Create Release - name: Create Release
id: create_release id: create_release
@@ -114,10 +168,11 @@ jobs:
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with: with:
tag_name: v${{ steps.bump.outputs.new_version }} tag_name: v${{ steps.release_check.outputs.releaseExists == 'true' && steps.rebump.outputs.new_version || steps.bump.outputs.new_version }}
release_name: Release v${{ steps.bump.outputs.new_version }} release_name: Release v${{ steps.release_check.outputs.releaseExists == 'true' && steps.rebump.outputs.new_version || steps.bump.outputs.new_version }}
draft: false draft: false
prerelease: false prerelease: false
continue-on-error: true
- name: Update action.yml version reference - name: Update action.yml version reference
run: | run: |