From 70ddd32ba73f35abc3de4eb10db27f37f034853a Mon Sep 17 00:00:00 2001 From: forntoh Date: Mon, 7 Oct 2024 20:22:42 +0200 Subject: [PATCH] Add optional tag version input and implement tag existence check - 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. --- .github/workflows/publish.yml | 55 +++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 04a81975..c7a614f3 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -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 update if not incrementing" + required: false jobs: publish: @@ -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: tags } = await github.repos.listTags({ + owner: context.repo.owner, + repo: context.repo.repo + }); + const tagExists = tags.some(t => t.name === tag); + core.setOutput('tag_exists', tagExists); - 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" @@ -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 @@ -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 }} @@ -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 }}' + }); + } \ No newline at end of file