From 6131ce63ec1be830e75d7e9598dda5e344e00843 Mon Sep 17 00:00:00 2001 From: Justin Gordon Date: Tue, 28 Jan 2025 22:59:14 -1000 Subject: [PATCH] fix app name and messages --- .../deploy-to-control-plane-review-app.yml | 107 +++++++++--------- 1 file changed, 56 insertions(+), 51 deletions(-) diff --git a/.github/workflows/deploy-to-control-plane-review-app.yml b/.github/workflows/deploy-to-control-plane-review-app.yml index 1895ce80..f16fae6f 100644 --- a/.github/workflows/deploy-to-control-plane-review-app.yml +++ b/.github/workflows/deploy-to-control-plane-review-app.yml @@ -5,11 +5,6 @@ run-name: Deploy PR Review App - PR #${{ github.event.pull_request.number || git on: pull_request: types: [opened, synchronize, reopened] - push: - branches: - - '**' # Any branch - - '!main' # Except main - - '!master' # Except master issue_comment: types: [created] workflow_dispatch: @@ -139,9 +134,9 @@ jobs: fi fi - # Extract and set PR data + # Set PR_NUMBER and override APP_NAME with validated PR number echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV - echo "APP_NAME=${{ vars.REVIEW_APP_PREFIX }}-$PR_NUMBER" >> $GITHUB_ENV + echo "APP_NAME=${{ vars.REVIEW_APP_PREFIX }}-pr-$PR_NUMBER" >> $GITHUB_ENV echo "PR_REF=$(echo $PR_DATA | jq -r .headRefName)" >> $GITHUB_OUTPUT echo "PR_SHA=$(echo $PR_DATA | jq -r .headRefOid)" >> $GITHUB_ENV @@ -178,40 +173,14 @@ jobs: exit 0 fi + # Validate supported event types if ! [[ "${{ github.event_name }}" == "workflow_dispatch" || \ "${{ github.event_name }}" == "issue_comment" || \ - "${{ github.event_name }}" == "pull_request" || \ - "${{ github.event_name }}" == "push" ]]; then + "${{ github.event_name }}" == "pull_request" ]]; then echo "Error: Unsupported event type ${{ github.event_name }}" exit 1 fi - # Set DO_DEPLOY based on event type and conditions - if [[ "${{ github.event_name }}" == "pull_request" && \ - ("${{ github.event.action }}" == "opened" || \ - "${{ github.event.action }}" == "synchronize" || \ - "${{ github.event.action }}" == "reopened") ]]; then - echo "DO_DEPLOY=true" >> $GITHUB_ENV - elif [[ "${{ github.event_name }}" == "push" ]]; then - echo "DO_DEPLOY=true" >> $GITHUB_ENV - elif [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then - echo "DO_DEPLOY=true" >> $GITHUB_ENV - elif [[ "${{ github.event_name }}" == "issue_comment" ]]; then - if [[ "${{ github.event.issue.pull_request }}" ]]; then - # Trim spaces and check for exact command - COMMENT_BODY=$(echo "${{ github.event.comment.body }}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') - if [[ "$COMMENT_BODY" == "/deploy-review-app" ]]; then - echo "DO_DEPLOY=true" >> $GITHUB_ENV - else - echo "DO_DEPLOY=false" >> $GITHUB_ENV - echo "Skipping deployment - comment '$COMMENT_BODY' does not match '/deploy-review-app'" - fi - else - echo "DO_DEPLOY=false" >> $GITHUB_ENV - echo "Skipping deployment for non-PR comment" - fi - fi - - name: Setup Control Plane App if Not Existing if: env.DO_DEPLOY != 'false' && env.APP_EXISTS == 'false' env: @@ -233,7 +202,11 @@ jobs: body: [ `🏗️ Building Docker image for PR [#${process.env.PR_NUMBER}](${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/pull/${process.env.PR_NUMBER}), commit [${context.sha.substring(0, 7)}](${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/commit/${context.sha})`, '', - `📝 [View Build Logs](${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}/job/${context.job})`, + '🚀 Deploying to Control Plane...', + '', + '⏳ Waiting for deployment to be ready...', + '', + `📝 [View Build and Deploy Logs](${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}/job/${context.job})`, '', process.env.CONSOLE_LINK ].join('\n') @@ -266,7 +239,7 @@ jobs: }; const workflowUrl = await getWorkflowUrl(context.runId); - core.exportVariable('WORKFLOW_URL', workflowUrl); + core.exportVariable('BUILD_LOGS_URL', workflowUrl); core.exportVariable('CONSOLE_LINK', '🎮 [Control Plane Console](' + 'https://console.cpln.io/console/org/' + process.env.CPLN_ORG + '/gvc/' + process.env.APP_NAME + '/-info)' @@ -350,21 +323,43 @@ jobs: uses: actions/github-script@v7 with: script: | - const deployingMessage = [ - '🚀 Deploying to Control Plane...', - '', - '⏳ Waiting for deployment to be ready...', - '', - '📝 [View Deploy Logs](${{ env.WORKFLOW_URL }})', - '', - process.env.CONSOLE_LINK - ].join('\n'); + // Create deployment status for deploying state + await github.rest.repos.createDeploymentStatus({ + owner: context.repo.owner, + repo: context.repo.repo, + deployment_id: ${{ needs.process-deployment.outputs.deployment_id }}, + state: 'in_progress', + description: 'Deployment in progress', + log_url: process.env.BUILD_LOGS_URL + }); + + // Get the current job URL and ID + const { data: jobs } = await github.rest.actions.listJobsForWorkflowRun({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: context.runId + }); + const currentJob = jobs.jobs.find(job => job.name === context.job); + const currentJobUrl = `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}/job/${currentJob.id}?pr=${process.env.PR_NUMBER}`; + + // Update the PR comment with correct job URLs await github.rest.issues.updateComment({ owner: context.repo.owner, repo: context.repo.repo, comment_id: ${{ needs.build.outputs.comment_id }}, - body: deployingMessage + body: [ + `🏗️ Built Docker image for PR [#${process.env.PR_NUMBER}](${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/pull/${process.env.PR_NUMBER}), commit [${process.env.PR_SHA.substring(0, 7)}](${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/commit/${process.env.PR_SHA})`, + '', + '🚀 Deploying to Control Plane...', + '', + '⏳ Waiting for deployment to be ready...', + '', + process.env.CONSOLE_LINK, + '', + `📝 [View Build Logs](${process.env.BUILD_LOGS_URL}?pr=${process.env.PR_NUMBER})`, + `📝 [View Deploy Logs](${currentJobUrl})` + ].join('\n') }); - name: Deploy to Control Plane @@ -390,6 +385,16 @@ jobs: const consoleLink = process.env.CONSOLE_LINK; + // Get current job ID for accurate logs URL + const { data: jobs } = await github.rest.actions.listJobsForWorkflowRun({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: context.runId + }); + + const currentJob = jobs.jobs.find(job => job.name === context.job); + const logsUrl = `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}/job/${currentJob.id}?pr=${prNumber}`; + // Create GitHub deployment status const deploymentStatus = { owner: context.repo.owner, @@ -397,7 +402,7 @@ jobs: deployment_id: ${{ steps.init-deployment.outputs.result }}, state: isSuccess ? 'success' : 'failure', environment_url: isSuccess ? appUrl : undefined, - log_url: workflowUrl, + log_url: logsUrl, environment: 'review' }; @@ -410,7 +415,7 @@ jobs: '🚀 [Review App for PR #' + prNumber + '](' + appUrl + ')', consoleLink, '', - '📋 [View Completed Action Build and Deploy Logs](' + workflowUrl + ')' + `📝 [View Build and Deploy Logs](${logsUrl})`, ].join('\n'); const failureMessage = [ @@ -418,13 +423,13 @@ jobs: '', consoleLink, '', - '📋 [View Deployment Logs with Errors](' + workflowUrl + ')' + `📝 [View Build and Deploy Logs with Errors](${logsUrl})`, ].join('\n'); // Update the existing comment await github.rest.issues.updateComment({ owner: context.repo.owner, repo: context.repo.repo, - comment_id: ${{ steps.create-comment.outputs.comment-id }}, + comment_id: ${{ needs.build.outputs.comment_id }}, body: isSuccess ? successMessage : failureMessage });