Skip to content

DEPLOY PREVIEW CI

DEPLOY PREVIEW CI #55

name: DEPLOY PREVIEW CI
env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_TEAM_ID }}
on:
workflow_run:
workflows: [TEST CI]
types:
- completed
jobs:
deploy-backend:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
environment:
name: Preview – project-management-backend
url: ${{ steps.vercel_deploy.outputs.BACKEND_DEPLOYED_URL }}
outputs:
BACKEND_DEPLOYED_URL: ${{steps.vercel_deploy.outputs.BACKEND_DEPLOYED_URL}}
defaults:
run:
working-directory: ./backend
env:
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_BACKEND_PROJECT_ID }}
permissions:
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install Vercel CLI
run: npm install --global vercel
- name: Deploy Project To Vercel
id: vercel_deploy
run: |
BACKEND_DEPLOYED_URL=$(vercel deploy --yes --token=${{ secrets.VERCEL_API_TOKEN }})
echo "BACKEND_DEPLOYED_URL=$BACKEND_DEPLOYED_URL" >> $GITHUB_OUTPUT
deploy-frontend:
runs-on: ubuntu-latest
needs: deploy-backend
environment:
name: Preview – project-management-frontend
url: ${{ steps.vercel_deploy.outputs.FRONT_END_DEPLOYED_URL }}
defaults:
run:
working-directory: ./frontend
env:
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_FRONTEND_PROJECT_ID }}
permissions:
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js 20
uses: actions/setup-node@v4
with:
node-version: 20
- name: Setup pnpm
uses: pnpm/action-setup@v4
with:
version: 9.4.0
- name: Setup java
uses: actions/setup-java@v4
with:
distribution: "temurin"
java-version: "21"
- name: Install Vercel CLI
run: pnpm add --global vercel
- name: Set custom environment variables
run: |
BACKEND_URL="${{ needs.deploy-backend.outputs.BACKEND_DEPLOYED_URL }}"
echo "Backend url: $BACKEND_URL"
CURRENT_DEPLOYMENT_VARS_RESPONSE=$(curl -s -o response.txt -w "%{http_code}" -X GET \
"https://api.vercel.com/v9/projects/${{ secrets.VERCEL_FRONTEND_PROJECT_ID }}/env" \
-H "Authorization: Bearer ${{ secrets.VERCEL_API_TOKEN }}" \
-H "Content-Type: application/json" \
-d '{
"gitBranch": "${{ github.head_ref }}",
"target": ["preview"]
}')
if [ "$CURRENT_DEPLOYMENT_VARS_RESPONSE" -ne 200 ]; then
echo "Failed to fetch current deployment variables. HTTP status: $CURRENT_DEPLOYMENT_VARS_RESPONSE"
cat response.txt
exit 1
fi
PUBLIC_API_URL_ID=$(jq -r --arg branch "${{ github.head_ref }}" '.envs[] | select(.key == "PUBLIC_API_URL" and .gitBranch == $branch) | .id' response.txt)
if [ -z "$PUBLIC_API_URL_ID" ] || [ "$PUBLIC_API_URL_ID" = "null" ]; then
# Create a new PUBLIC_API_URL environment variable
echo "Creating env vars"
CREATE_RESPONSE=$(curl -s -o response.txt -w "%{http_code}" -X POST \
"https://api.vercel.com/v9/projects/${{ secrets.VERCEL_FRONTEND_PROJECT_ID }}/env" \
-H "Authorization: Bearer ${{ secrets.VERCEL_API_TOKEN }}" \
-H "Content-Type: application/json" \
-d '{
"gitBranch": "${{ github.head_ref }}",
"key": "PUBLIC_API_URL",
"target": ["preview"],
"type": "plain",
"value": "'"${BACKEND_URL}"'",
"comment": "backend url for ${{ github.head_ref }}"
}')
if [ "$CREATE_RESPONSE" -ne 200 ]; then
echo "Failed to create Vercel environment variable. HTTP status: $CREATE_RESPONSE"
cat response.txt
exit 1
fi
else
# Update the existing PUBLIC_API_URL environment variable
echo "Updating existing env vars"
UPDATE_RESPONSE=$(curl -s -o response.txt -w "%{http_code}" -X PATCH \
"https://api.vercel.com/v9/projects/${{ secrets.VERCEL_FRONTEND_PROJECT_ID }}/env/$PUBLIC_API_URL_ID" \
-H "Authorization: Bearer ${{ secrets.VERCEL_API_TOKEN }}" \
-H "Content-Type: application/json" \
-d '{
"gitBranch": "${{ github.head_ref }}",
"key": "PUBLIC_API_URL",
"target": ["preview"],
"type": "plain",
"value": "'"${BACKEND_URL}"'",
"comment": "backend url for ${{ github.head_ref }}"
}')
if [ "$UPDATE_RESPONSE" -ne 200 ]; then
echo "Failed to update Vercel environment variable. HTTP status: $UPDATE_RESPONSE"
cat response.txt
exit 1
fi
fi
echo "Environment variable PUBLIC_API_URL updated successfully."
- name: Deploy Project To Vercel
id: vercel_deploy
run: |
FRONT_END_DEPLOYED_URL=$(vercel deploy --yes --token=${{ secrets.VERCEL_API_TOKEN }})
echo "FRONT_END_DEPLOYED_URL=$FRONT_END_DEPLOYED_URL" >> $GITHUB_OUTPUT