Skip to content

Commit

Permalink
Add optional tag version input and implement tag existence check
Browse files Browse the repository at this point in the history
- Added an optional input for setting a specific version if not incrementing
- Implemented a check to verify the existence of a tag before committing and tagging changes.
  • Loading branch information
forntoh committed Oct 7, 2024
1 parent 3569f5b commit 58854e4
Showing 1 changed file with 50 additions and 5 deletions.
55 changes: 50 additions & 5 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
part_to_increment:
description: "Part to increment (1 for major, 2 for minor, 3 for patch)"
required: true
tag_version:
description: "Version to set if not incrementing"
required: false

jobs:
publish:
Expand Down Expand Up @@ -34,12 +37,32 @@ jobs:
- name: Update Version
env:
PART_TO_INCREMENT: ${{ github.event.inputs.part_to_increment }}
TAG_VERSION: ${{ github.event.inputs.tag_version }}
run: |
chmod +x version
./version "$PART_TO_INCREMENT"
echo "UPDATED_VERSION=$(grep "version=" library.properties | cut -d'=' -f2)" >> $GITHUB_ENV
if [ -n "$TAG_VERSION" ]; then
UPDATED_VERSION=$TAG_VERSION
else
chmod +x version
./version "$PART_TO_INCREMENT"
UPDATED_VERSION=$(grep "version=" library.properties | cut -d'=' -f2)
fi
echo "UPDATED_VERSION=$UPDATED_VERSION" >> $GITHUB_ENV
- name: Check if tag exists
id: check_tag
uses: actions/github-script@v6
with:
script: |
const tag = '${{ env.UPDATED_VERSION }}';
const { data: refs } = await github.git.listMatchingRefs({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `tags/${tag}`
});
core.setOutput('tag_exists', refs.length > 0);
- name: Commit changes
if: steps.check_tag.outputs.tag_exists == 'false'
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
Expand All @@ -49,12 +72,13 @@ jobs:
git push origin master --force
- name: Create Tag
if: ${{ env.UPDATED_VERSION != '' }}
if: steps.check_tag.outputs.tag_exists == 'false'
run: |
git tag -a "${{ env.UPDATED_VERSION }}" -m "Tagging version ${{ env.UPDATED_VERSION }}"
git push origin "${{ env.UPDATED_VERSION }}"
- name: Publish to PlatformIO
if: steps.check_tag.outputs.tag_exists == 'false'
id: publish
run: yes y | pio pkg publish
continue-on-error: true
Expand Down Expand Up @@ -87,7 +111,7 @@ jobs:
core.setOutput('release_notes', releaseNotes);
- name: Create GitHub Release
if: success()
if: steps.check_tag.outputs.tag_exists == 'false'
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -97,3 +121,24 @@ jobs:
draft: false
prerelease: false
body: ${{ steps.generate_release_notes.outputs.result }}

- name: Update GitHub Release
if: steps.check_tag.outputs.tag_exists == 'true'
uses: actions/github-script@v6
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
script: |
const { data: releases } = await github.repos.listReleases({
owner: context.repo.owner,
repo: context.repo.repo
});
const release = releases.find(r => r.tag_name === '${{ env.UPDATED_VERSION }}');
if (release) {
await github.repos.updateRelease({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: release.id,
body: '${{ steps.generate_release_notes.outputs.result }}'
});
}

0 comments on commit 58854e4

Please sign in to comment.