diff --git a/.github/workflows/publish-step-1.yml b/.github/workflows/publish-step-1.yml index 02eda05b..f8a62da0 100644 --- a/.github/workflows/publish-step-1.yml +++ b/.github/workflows/publish-step-1.yml @@ -28,38 +28,94 @@ jobs: run: | VERSION=$(cat VERSION) echo "VERSION=${VERSION}" >> $GITHUB_ENV + TAG="v${VERSION}" + echo "TAG=${TAG}" >> $GITHUB_ENV echo "PYTHON=python3" >> $GITHUB_ENV git config --global user.name "github-actions[bot]" git config --global user.email "github-actions[bot]@users.noreply.github.com" - name: Tagging + shell: bash + env: + TAG: ${{ env.TAG }} run: | - TAG="v${{ env.VERSION }}" + # Ensure latest commit is tag with '$TAG' + NEW_COMMIT=$(git rev-parse HEAD) if git rev-parse "$TAG" >/dev/null 2>&1; then - git tag -d "$TAG" - git push --delete origin "$TAG" + EXISTING_COMMIT=$(git rev-parse "$TAG") + if [ "$NEW_COMMIT" != "$EXISTING_COMMIT" ]; then + echo "Deleting existing tag $TAG" + git tag -d "$TAG" + git push --delete origin "$TAG" + else + echo "Tag $TAG already exists and points to the same commit. No changes needed." + exit 0 + fi fi + echo "Creating new tag $TAG" git tag "$TAG" git push origin "$TAG" - - name: Create Release - id: create_release - uses: actions/create-release@v1 + - name: Ensure Release exists and init release_vars + id: release_vars + uses: actions/github-script@v7 env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + TAG: ${{ env.TAG }} with: - tag_name: "v${{ env.VERSION }}" - release_name: "v${{ env.VERSION }}" - body: ${{ github.event.inputs.release_body }} - draft: true - prerelease: false + script: | + let tag_name = process.env.TAG; + console.log(`tag_name: ${tag_name}`); + let upload_url; - - name: Upload Release Assets - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: dist/* - asset_name: $(basename ${{ steps.create_release.outputs.upload_url }}) - asset_content_type: application/octet-stream + try { + // Check if the release already exists + const { data: releases } = await github.rest.repos.listReleases({ + owner: context.repo.owner, + repo: context.repo.repo, + }); + const release = releases.find(release => release.tag_name === tag_name); + + if (release) { + if (!release.draft) { + console.log(`Release for tag ${tag_name} is already published.Exiting with error.`); + core.setFailed(`Release for tag ${tag_name} is already published.`); + return; + } + console.log(`Release for tag ${tag_name} already exists.`); + upload_url = release.upload_url; + + // Update the release to point to potentially new commit (noop when no change) + release = await github.rest.repos.updateRelease({ + owner: context.repo.owner, + repo: context.repo.repo, + release_id: release.id, + tag_name: tag_name, + target_commitish: context.sha, + }); + } else { + // Release does not exists, so create it. + const response = await github.rest.repos.createRelease({ + owner: context.repo.owner, + repo: context.repo.repo, + tag_name: tag_name, + name: tag_name, + draft: true, + prerelease: false, + }); + if (response.status >= 200 && response.status < 300 && response.data.upload_url) { + console.log(`Release created with tag [${tag_name}]`); + upload_url = response.data.upload_url; + } else { + core.setFailed(`Failed to create release with tag [${tag_name}] Status: ${response.status}`); + return; + } + } + } catch (error) { + core.setFailed(`Error while listing or creating releases: ${error.message}`); + return; + } + + core.setOutput("upload_url", upload_url); + + outputs: + upload_url: ${{ steps.release_vars.outputs.upload_url }}