Skip to content

Commit 83cacaf

Browse files
committed
rework-main-job-deploy
1 parent c994d42 commit 83cacaf

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

.github/workflows/deploy-to-control-plane.yml

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,32 @@ jobs:
5959
6060
echo "All required secrets and variables are set"
6161
62+
- name: Get PR Number for Push Event
63+
if: github.event_name == 'push'
64+
id: get-pr
65+
env:
66+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
67+
run: |
68+
# Get PR number from branch
69+
PR_NUMBER=$(gh pr list --head ${{ github.ref_name }} --json number --jq '.[0].number')
70+
if [ -n "$PR_NUMBER" ]; then
71+
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
72+
echo "APP_NAME=${{ env.REVIEW_APP_PREFIX }}-pr-$PR_NUMBER" >> $GITHUB_ENV
73+
echo "has_pr=true" >> $GITHUB_OUTPUT
74+
else
75+
echo "No PR found for this branch"
76+
exit 0
77+
fi
78+
79+
- name: Set PR Number for Comment Event
80+
if: github.event_name == 'issue_comment'
81+
run: |
82+
echo "PR_NUMBER=${{ github.event.issue.number }}" >> $GITHUB_ENV
83+
echo "APP_NAME=${{ env.REVIEW_APP_PREFIX }}-pr-${{ github.event.issue.number }}" >> $GITHUB_ENV
84+
6285
- name: Setup Deployment Configuration
6386
id: setup
6487
run: |
65-
# Function to set PR deployment environment
66-
setup_pr_deployment() {
67-
local pr_number=$1
68-
echo "PR_NUMBER=$pr_number" >> $GITHUB_ENV
69-
echo "APP_NAME=${{ env.REVIEW_APP_PREFIX }}-pr-$pr_number" >> $GITHUB_ENV
70-
echo "IS_STAGING=false" >> $GITHUB_ENV
71-
72-
# Get PR ref and SHA
73-
local pr_data=$(gh pr view $pr_number --repo ${{ github.repository }} --json headRefName,headRefOid)
74-
echo "PR_REF=$(echo "$pr_data" | jq -r '.headRefName')" >> $GITHUB_OUTPUT
75-
echo "PR_SHA=$(echo "$pr_data" | jq -r '.headRefOid')" >> $GITHUB_OUTPUT
76-
}
77-
7888
# Function to check if app exists
7989
check_app_exists() {
8090
local app_name=$1
@@ -93,40 +103,31 @@ jobs:
93103
DEFAULT_BRANCH="refs/heads/$(gh repo view --json defaultBranchRef --jq '.defaultBranchRef.name')"
94104
echo "DEFAULT_BRANCH=$DEFAULT_BRANCH" >> $GITHUB_OUTPUT
95105
106+
# Set staging app name if this is the default branch
107+
if [[ "${{ github.ref }}" == "$DEFAULT_BRANCH" ]]; then
108+
echo "APP_NAME=${{ env.STAGING_APP_NAME }}" >> $GITHUB_ENV
109+
echo "IS_STAGING=true" >> $GITHUB_ENV
110+
fi
111+
96112
# Check if we should deploy
97113
if [[ "${{ github.ref }}" == "$DEFAULT_BRANCH" ]]; then
98114
echo "SHOULD_DEPLOY=true" >> $GITHUB_OUTPUT
99-
echo "IS_STAGING=true" >> $GITHUB_ENV
100-
echo "APP_NAME=${{ env.STAGING_APP_NAME }}" >> $GITHUB_ENV
101115
elif [[ "${{ github.event_name }}" == "issue_comment" &&
102116
"${{ github.event.issue.pull_request }}" == "true" &&
103117
"${{ github.event.comment.body }}" == "/deploy-review-app" ]]; then
104118
echo "SHOULD_DEPLOY=true" >> $GITHUB_OUTPUT
105-
setup_pr_deployment "${{ github.event.issue.number }}"
106-
elif [[ "${{ github.event_name }}" == "push" ]]; then
107-
# Get PR number for the current branch, handling refs/heads/ prefix
108-
BRANCH_NAME="${{ github.ref_name }}"
109-
PR_NUMBER=$(gh pr list --head "$BRANCH_NAME" --json number,state --jq '.[] | select(.state=="OPEN") | .number')
110-
111-
if [ -n "$PR_NUMBER" ]; then
112-
echo "Found PR #$PR_NUMBER for branch $BRANCH_NAME"
113-
APP_NAME="${{ env.REVIEW_APP_PREFIX }}-pr-$PR_NUMBER"
114-
if check_app_exists "$APP_NAME"; then
115-
echo "SHOULD_DEPLOY=true" >> $GITHUB_OUTPUT
116-
setup_pr_deployment "$PR_NUMBER"
117-
else
118-
no_deployment "No existing review app found for PR $PR_NUMBER"
119-
fi
119+
elif [[ "${{ github.event_name }}" == "push" && "${{ steps.get-pr.outputs.has_pr }}" == "true" ]]; then
120+
if check_app_exists "${{ env.APP_NAME }}"; then
121+
echo "SHOULD_DEPLOY=true" >> $GITHUB_OUTPUT
120122
else
121-
echo "No open PR found for branch $BRANCH_NAME"
122-
no_deployment "No PR found for this branch"
123+
no_deployment "No existing review app found for PR ${{ env.PR_NUMBER }}"
123124
fi
124125
else
125126
no_deployment "No deployment needed for this event"
126127
fi
127128
128129
# Set console link after APP_NAME is set
129-
echo "CONSOLE_LINK=[View in Control Plane Console](https://console.cpln.io/org/${{ env.CPLN_ORG }}/workload/$APP_NAME)" >> $GITHUB_ENV
130+
echo "CONSOLE_LINK=[View in Control Plane Console](https://console.cpln.io/org/${{ env.CPLN_ORG }}/workload/${{ env.APP_NAME }})" >> $GITHUB_ENV
130131
131132
- name: Set Workflow URL
132133
id: workflow-url
@@ -161,7 +162,6 @@ jobs:
161162
}
162163
163164
- name: Create Initial Comment
164-
if: env.IS_STAGING != 'true' && env.PR_NUMBER != ''
165165
id: create-comment
166166
uses: actions/github-script@v7
167167
with:

0 commit comments

Comments
 (0)