From 028f6c53998860ded1a4004e91d96c4fe1f49a35 Mon Sep 17 00:00:00 2001 From: Karan Popat Date: Sun, 12 Jan 2025 01:33:17 +0530 Subject: [PATCH] Fix workflow for preview builds (#255) --- .github/workflows/trigger-turbot-com.yml | 49 ++++++++++++++++-------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/.github/workflows/trigger-turbot-com.yml b/.github/workflows/trigger-turbot-com.yml index 339ef647..0dc83d32 100644 --- a/.github/workflows/trigger-turbot-com.yml +++ b/.github/workflows/trigger-turbot-com.yml @@ -8,11 +8,7 @@ on: - "main" pull_request: - types: [opened] - -env: - PREVIEW_LINK: turbot-com-git-docs-guardrails - PREVIEW_DOMAIN: turbot.vercel.app + types: [opened, synchronize] jobs: update: @@ -24,28 +20,49 @@ jobs: with: token: ${{ secrets.GH_PAT }} repository: turbot/turbot.com - event-type: build-guardrails-docs-preview + event-type: build-guardrails-docs-preview-test client-payload: '{"ref": "${{ github.ref }}", "sha": "${{ github.sha }}", "ref_name": "${{ github.ref_name }}"}' + comment-link: runs-on: ubuntu-latest timeout-minutes: 5 - if: ${{ github.event_name == 'pull_request' && github.event.action == 'opened' }} + if: ${{ github.event_name == 'pull_request'}} steps: + - name: Get the deployment details + id: get_deployment_details + if: ${{ github.event_name == 'pull_request'}} + uses: actions/github-script@v6 + with: + script: | + const branchName = 'docs/guardrails/${{ github.event.pull_request.head.ref }}'; - - name: Convert head branch special characters to hyphens - id: sanitize_branch - run: | - sanitized_branch=$(echo "${{ github.event.pull_request.head.ref }}" | sed 's/[^a-zA-Z0-9]/-/g') - echo "Sanitized head branch: $sanitized_branch" - echo "::set-output name=sanitized_branch::$sanitized_branch" + const response = await fetch('https://turbot.com/api/service/deploy-details', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ${{ secrets.TAILPIPE_API_KEY }}', + }, + body: JSON.stringify({ branch: branchName }), + }); + if (!response.ok) { + core.setOutput('foundDeployment', false); + } else { + const data = await response.json(); + core.setOutput('foundDeployment', true); + core.setOutput('apiResponse', data); + } - - uses: actions/github-script@v7 + - name: Add comment with preview link + uses: actions/github-script@v7 + if: ${{ steps.get_deployment_details.outputs.foundDeployment == 'true' }} with: script: | + const apiResponse = ${{ steps.get_deployment_details.outputs.apiResponse }}; + github.rest.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, - body: 'Preview link: [https://${{ env.PREVIEW_LINK }}${{ steps.sanitize_branch.outputs.sanitized_branch }}-${{ env.PREVIEW_DOMAIN }}](https://${{ env.PREVIEW_LINK }}${{ steps.sanitize_branch.outputs.sanitized_branch }}-${{ env.PREVIEW_DOMAIN }}/)' - }) \ No newline at end of file + body: `### Preview Available 🚀\n\n**Commit Author:** ${apiResponse.meta.githubCommitAuthorName}\n**Commit Message:** ${apiResponse.meta.githubCommitMessage}\n\n**Preview Link:** [${apiResponse.meta.branchAlias}](https://${apiResponse.meta.branchAlias})` + }); \ No newline at end of file