@@ -59,22 +59,32 @@ jobs:
59
59
60
60
echo "All required secrets and variables are set"
61
61
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
+
62
85
- name : Setup Deployment Configuration
63
86
id : setup
64
87
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
-
78
88
# Function to check if app exists
79
89
check_app_exists() {
80
90
local app_name=$1
@@ -93,40 +103,31 @@ jobs:
93
103
DEFAULT_BRANCH="refs/heads/$(gh repo view --json defaultBranchRef --jq '.defaultBranchRef.name')"
94
104
echo "DEFAULT_BRANCH=$DEFAULT_BRANCH" >> $GITHUB_OUTPUT
95
105
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
+
96
112
# Check if we should deploy
97
113
if [[ "${{ github.ref }}" == "$DEFAULT_BRANCH" ]]; then
98
114
echo "SHOULD_DEPLOY=true" >> $GITHUB_OUTPUT
99
- echo "IS_STAGING=true" >> $GITHUB_ENV
100
- echo "APP_NAME=${{ env.STAGING_APP_NAME }}" >> $GITHUB_ENV
101
115
elif [[ "${{ github.event_name }}" == "issue_comment" &&
102
116
"${{ github.event.issue.pull_request }}" == "true" &&
103
117
"${{ github.event.comment.body }}" == "/deploy-review-app" ]]; then
104
118
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
120
122
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 }}"
123
124
fi
124
125
else
125
126
no_deployment "No deployment needed for this event"
126
127
fi
127
128
128
129
# 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
130
131
131
132
- name : Set Workflow URL
132
133
id : workflow-url
@@ -161,7 +162,6 @@ jobs:
161
162
}
162
163
163
164
- name : Create Initial Comment
164
- if : env.IS_STAGING != 'true' && env.PR_NUMBER != ''
165
165
id : create-comment
166
166
uses : actions/github-script@v7
167
167
with :
0 commit comments