diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 04a81975..723551d7 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 set 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: 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" @@ -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