Deploying Review App for justin808-testing-2025-01-04 #107
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy to Control Plane | |
run-name: ${{ github.event_name == 'issue_comment' && format('Deploying Review App for {0}', github.ref_name) || github.ref == '${{ steps.default-branch.outputs.name }}' && 'Deploying to Staging' || format('Deploying Review App for {0}', github.ref_name) }} | |
on: | |
issue_comment: | |
types: [created] | |
push: | |
branches: | |
- '**' # Allow all branches | |
- '!gh-pages' # Exclude gh-pages if it exists | |
# Allow manual triggers | |
workflow_dispatch: | |
# Use concurrency to cancel in-progress runs | |
concurrency: | |
group: ${{ github.ref == '${{ steps.default-branch.outputs.name }}' && 'deploy-staging' || format('deploy-pr-{0}', github.event.issue.number) }} | |
cancel-in-progress: true | |
jobs: | |
# Job to handle staging deployments | |
Deploy-Staging: | |
if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch) | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
deployments: write | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }} | |
CPLN_ORG: ${{ vars.CPLN_ORG_STAGING }} | |
STAGING_APP_NAME: ${{ vars.STAGING_APP_NAME }} | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Verify Environment Variables | |
run: | | |
# Required actions secrets | |
: "${GITHUB_TOKEN:?Required secret GITHUB_TOKEN not set}" | |
: "${CPLN_TOKEN:?Required secret CPLN_TOKEN_STAGING not set}" | |
# Required actions variables | |
: "${CPLN_ORG:?Required variable CPLN_ORG_STAGING not set}" | |
: "${STAGING_APP_NAME:?Required variable STAGING_APP_NAME not set}" | |
- name: Setup Environment | |
uses: ./.github/actions/setup-environment | |
env: | |
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }} | |
CPLN_ORG: ${{ vars.CPLN_ORG_STAGING }} | |
- name: Deploy to Control Plane | |
id: deploy | |
uses: ./.github/actions/deploy-to-control-plane | |
with: | |
app_name: ${{ env.STAGING_APP_NAME }} | |
org: ${{ env.CPLN_ORG }} | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
# Job to handle review app deployments | |
Deploy-Review-App: | |
if: | | |
(github.event_name == 'issue_comment' && | |
github.event.issue.pull_request && | |
github.event.comment.body == '/deploy-review-app') || | |
github.event_name == 'push' | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
deployments: write | |
pull-requests: write | |
issues: write | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }} | |
CPLN_ORG: ${{ vars.CPLN_ORG_STAGING }} | |
REVIEW_APP_PREFIX: ${{ vars.REVIEW_APP_PREFIX }} | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Verify Environment Variables | |
run: | | |
# Required actions secrets | |
: "${GITHUB_TOKEN:?Required secret GITHUB_TOKEN not set}" | |
: "${CPLN_TOKEN:?Required secret CPLN_TOKEN_STAGING not set}" | |
# Required actions variables | |
: "${CPLN_ORG:?Required variable CPLN_ORG_STAGING not set}" | |
: "${REVIEW_APP_PREFIX:?Required variable REVIEW_APP_PREFIX not set}" | |
- name: Setup Environment | |
uses: ./.github/actions/setup-environment | |
env: | |
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN_STAGING }} | |
CPLN_ORG: ${{ vars.CPLN_ORG_STAGING }} | |
- name: Get PR Number for Push Event | |
if: github.event_name == 'push' | |
id: get-pr | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
# Get PR number from branch | |
PR_DATA=$(gh pr list --head ${{ github.ref_name }} --json number,headRefOid --jq '.[0]') | |
PR_NUMBER=$(echo "$PR_DATA" | jq -r '.number') | |
if [ -n "$PR_NUMBER" ]; then | |
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV | |
echo "APP_NAME=${{ env.REVIEW_APP_PREFIX }}-pr-$PR_NUMBER" >> $GITHUB_ENV | |
echo "COMMIT_SHA=$(echo "$PR_DATA" | jq -r '.headRefOid')" >> $GITHUB_ENV | |
echo "has_pr=true" >> $GITHUB_OUTPUT | |
else | |
echo "No PR found for this branch" | |
exit 0 | |
fi | |
- name: Set PR Number for Comment Event | |
if: github.event_name == 'issue_comment' | |
run: | | |
# Get PR data including the commit SHA | |
PR_DATA=$(gh pr view ${{ github.event.issue.number }} --json headRefOid --jq '.') | |
echo "PR_NUMBER=${{ github.event.issue.number }}" >> $GITHUB_ENV | |
echo "APP_NAME=${{ env.REVIEW_APP_PREFIX }}-pr-${{ github.event.issue.number }}" >> $GITHUB_ENV | |
echo "COMMIT_SHA=$(echo "$PR_DATA" | jq -r '.headRefOid')" >> $GITHUB_ENV | |
- name: Check Review App Exists | |
id: check-app | |
run: | | |
if ! cpln workload get "${{ env.APP_NAME }}" --org "${{ env.CPLN_ORG }}" > /dev/null 2>&1; then | |
echo "Review app ${{ env.APP_NAME }} does not exist" | |
exit 0 | |
fi | |
echo "exists=true" >> $GITHUB_OUTPUT | |
- name: Build Docker Image | |
if: steps.check-app.outputs.exists == 'true' || github.event_name == 'issue_comment' | |
uses: ./.github/actions/build-docker-image | |
with: | |
app_name: ${{ env.APP_NAME }} | |
org: ${{ env.CPLN_ORG }} | |
commit: ${{ env.COMMIT_SHA }} | |
- name: Deploy to Control Plane | |
if: steps.check-app.outputs.exists == 'true' || github.event_name == 'issue_comment' | |
id: deploy | |
uses: ./.github/actions/deploy-to-control-plane | |
with: | |
app_name: ${{ env.APP_NAME }} | |
org: ${{ env.CPLN_ORG }} | |
github_token: ${{ secrets.GITHUB_TOKEN }} |