mirror of
https://github.com/LukeHagar/usage-statistics.git
synced 2025-12-06 04:21:55 +00:00
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:
71
.github/workflows/auto-release.yml
vendored
71
.github/workflows/auto-release.yml
vendored
@@ -117,13 +117,67 @@ jobs:
|
||||
git config user.name "github-actions[bot]"
|
||||
git config user.email "github-actions[bot]@users.noreply.github.com"
|
||||
git add package.json dist/
|
||||
git commit -m "chore: bump version to ${{ steps.bump.outputs.new_version }} and build action"
|
||||
git push
|
||||
# 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 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
|
||||
run: |
|
||||
git tag v${{ steps.bump.outputs.new_version }}
|
||||
git push origin v${{ steps.bump.outputs.new_version }}
|
||||
TAG_VERSION="${{ steps.release_check.outputs.releaseExists == 'true' && steps.rebump.outputs.new_version || 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
|
||||
id: create_release
|
||||
@@ -131,10 +185,10 @@ jobs:
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
tag_name: v${{ steps.bump.outputs.new_version }}
|
||||
release_name: Release 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.release_check.outputs.releaseExists == 'true' && steps.rebump.outputs.new_version || steps.bump.outputs.new_version }}
|
||||
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:
|
||||
${{ steps.commits.outputs.commits }}
|
||||
@@ -142,7 +196,8 @@ jobs:
|
||||
### Usage:
|
||||
Update your workflows to use:
|
||||
```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
|
||||
prerelease: false
|
||||
continue-on-error: true
|
||||
|
||||
67
.github/workflows/release.yml
vendored
67
.github/workflows/release.yml
vendored
@@ -100,13 +100,67 @@ jobs:
|
||||
git config user.name "github-actions[bot]"
|
||||
git config user.email "github-actions[bot]@users.noreply.github.com"
|
||||
git add package.json dist/
|
||||
git commit -m "chore: bump version to ${{ steps.bump.outputs.new_version }} and build action"
|
||||
git push
|
||||
# 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 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
|
||||
run: |
|
||||
git tag v${{ steps.bump.outputs.new_version }}
|
||||
git push origin v${{ steps.bump.outputs.new_version }}
|
||||
TAG_VERSION="${{ steps.release_check.outputs.releaseExists == 'true' && steps.rebump.outputs.new_version || 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
|
||||
id: create_release
|
||||
@@ -114,10 +168,11 @@ jobs:
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
tag_name: v${{ steps.bump.outputs.new_version }}
|
||||
release_name: Release 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.release_check.outputs.releaseExists == 'true' && steps.rebump.outputs.new_version || steps.bump.outputs.new_version }}
|
||||
draft: false
|
||||
prerelease: false
|
||||
continue-on-error: true
|
||||
|
||||
- name: Update action.yml version reference
|
||||
run: |
|
||||
|
||||
Reference in New Issue
Block a user