From db76d2ab50ed1a67da4f7034721d334cac4af653 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 | 82 ++++++++++++++++++++++++++++------- .scripts/release_notes.js | 1 + 2 files changed, 67 insertions(+), 16 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 04a81975..de4caba1 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: @@ -21,25 +24,35 @@ jobs: with: python-version: "3.x" - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install click - pip install --upgrade platformio - - - name: Login to PlatformIO - run: | - pio account login -u "${{ secrets.PIO_USERNAME }}" -p "${{ secrets.PIO_PASSWORD }}" - - 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.rest.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 +62,25 @@ 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: Install dependencies + if: steps.check_tag.outputs.tag_exists == 'false' + run: | + python -m pip install --upgrade pip + pip install click + pip install --upgrade platformio + + - name: Login to PlatformIO + if: steps.check_tag.outputs.tag_exists == 'false' + run: | + pio account login -u "${{ secrets.PIO_USERNAME }}" -p "${{ secrets.PIO_PASSWORD }}" + - 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 +113,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 }} @@ -96,4 +122,28 @@ jobs: release_name: LcdMenu v${{ env.UPDATED_VERSION }} draft: false prerelease: false - body: ${{ steps.generate_release_notes.outputs.result }} + body: ${{ steps.generate_release_notes.outputs.release_notes }} + + - 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.rest.repos.listReleases({ + owner: context.repo.owner, + repo: context.repo.repo + }); + const release = releases.find(r => r.tag_name === '${{ env.UPDATED_VERSION }}'); + if (release) { + console.log('Updating release notes for release', release.id); + await github.rest.repos.updateRelease({ + owner: context.repo.owner, + repo: context.repo.repo, + release_id: release.id, + body: `${{ steps.generate_release_notes.outputs.release_notes }}` + }); + } else { + console.log('Release not found'); + } diff --git a/.scripts/release_notes.js b/.scripts/release_notes.js index 2ad6b3f9..64370a67 100644 --- a/.scripts/release_notes.js +++ b/.scripts/release_notes.js @@ -89,6 +89,7 @@ async function generateReleaseNotes(github, context) { pr.labels.forEach((label) => { if (label.name === "breaking-change") { hasBreakingChanges = true; + return; } if (categories[label.name]) { categories[label.name].push(prEntry);