diff --git a/.github/workflows/release-preview.yaml b/.github/workflows/release-preview.yaml new file mode 100644 index 0000000..c93c471 --- /dev/null +++ b/.github/workflows/release-preview.yaml @@ -0,0 +1,118 @@ +name: Release Preview + +on: + workflow_call: + +env: + SEMANTIC_RELEASE_VERSION: '24.2.0' + NODE_VERSION: '20.11.0' + +jobs: + preview: + name: Preview Release + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + ref: ${{ github.event.pull_request.head.ref }} + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: ${{ env.NODE_VERSION }} + + - name: Run semantic-release (dry-run) + id: semantic + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GIT_COMMITTER_NAME: "github-actions[bot]" + GIT_COMMITTER_EMAIL: "github-actions[bot]@users.noreply.github.com" + GIT_AUTHOR_NAME: "github-actions[bot]" + GIT_AUTHOR_EMAIL: "github-actions[bot]@users.noreply.github.com" + run: | + # Unset GitHub Actions environment variables that interfere with semantic-release + unset GITHUB_REF + unset GITHUB_REF_NAME + unset GITHUB_HEAD_REF + unset GITHUB_BASE_REF + + # Set them to what we want + export GITHUB_REF="refs/heads/${{ github.event.pull_request.head.ref }}" + export GITHUB_REF_NAME="${{ github.event.pull_request.head.ref }}" + + # Run semantic-release with inline configuration using CLI options + OUTPUT=$(npx --package semantic-release@${{ env.SEMANTIC_RELEASE_VERSION }} \ + --package @semantic-release/exec \ + --package conventional-changelog-conventionalcommits \ + semantic-release \ + --dry-run \ + --no-ci \ + --debug \ + --branches ${{ github.event.pull_request.head.ref }} 2>&1 || true) + echo "$OUTPUT" + + # Extract version information + NEW_VERSION=$(echo "$OUTPUT" | grep -Eo "The next release version is [0-9]+\.[0-9]+\.[0-9]+" | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+" || echo "") + RELEASE_TYPE=$(echo "$OUTPUT" | grep -Eo "Analysis of [0-9]+ commits complete: [a-z]+ release" | grep -Eo "(major|minor|patch) release" | sed 's/ release//' || echo "") + + # Extract release notes (everything after "Release note for version") + RELEASE_NOTES=$(echo "$OUTPUT" | sed -n '/Release note for version/,$p' | tail -n +2 || echo "") + + # Save to outputs + echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT + echo "release_type=$RELEASE_TYPE" >> $GITHUB_OUTPUT + + # Save release notes for comment + echo "release_notes<> $GITHUB_OUTPUT + echo "$RELEASE_NOTES" >> $GITHUB_OUTPUT + echo "EOF" >> $GITHUB_OUTPUT + + - name: Display Preview + run: | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" + echo " RELEASE PREVIEW" + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" + echo "" + if [ -n "${{ steps.semantic.outputs.new_version }}" ]; then + echo "Version: v${{ steps.semantic.outputs.new_version }}" + echo "Release Type: ${{ steps.semantic.outputs.release_type }}" + echo "Status: Release will be published" + else + echo "Status: No release will be published" + echo "Reason: No relevant changes detected" + fi + echo "" + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" + + - name: Comment on PR + if: github.event_name == 'pull_request' + uses: mshick/add-pr-comment@v2 + with: + message-id: release-preview + message: | + ## Release Preview + + ${{ steps.semantic.outputs.new_version && format('**Version:** `v{0}` + **Release Type:** `{1}` + **Status:** Release will be published when merged to main + + --- + + ### Release Notes + + {2} + + --- + + *This preview is generated by semantic-release dry-run mode*', steps.semantic.outputs.new_version, steps.semantic.outputs.release_type, steps.semantic.outputs.release_notes) || '**Status:** ❌ No release will be published + **Reason:** No relevant changes detected + + --- + + *This preview is generated by semantic-release dry-run mode*' }} diff --git a/.github/workflows/terraform.yaml b/.github/workflows/terraform.yaml index 3c55029..6b05110 100644 --- a/.github/workflows/terraform.yaml +++ b/.github/workflows/terraform.yaml @@ -15,6 +15,14 @@ on: - main - master jobs: + releasePreview: + name: Release Preview + if: ${{ github.event_name == 'pull_request' || github.event_name == 'pull_request_target' }} + uses: ./.github/workflows/release-preview.yaml + permissions: + contents: write + pull-requests: write + preCommitCheck: name: Terraform Checks uses: ./.github/workflows/terraform-checks.yaml