1- name : Frontend CI/CD
2-
3- on :
4- push :
5- branches :
6- - dev
7- - main
8-
9- concurrency :
10- group : ${{ github.workflow }}-${{ github.ref }}
11- cancel-in-progress : false
12-
13- jobs :
14- test-and-build :
15- runs-on : aiadgen
16- steps :
17- - name : Checkout Repository
18- uses : actions/checkout@v4
19-
20- - name : Setup Node.js
21- uses : actions/setup-node@v4
22- with :
23- node-version : " 20.x"
24-
25- - name : Debug Branch Information
26- run : |
27- echo "Current branch: ${{ github.ref }}"
28- echo "Is main? ${{ github.ref == 'refs/heads/main' }}"
29- echo "Is dev? ${{ github.ref == 'refs/heads/dev' }}"
30-
31- - name : Setup pnpm
32- uses : pnpm/action-setup@v3
33- with :
34- version : latest
35-
36- - name : Create Environment-Specific .env File
37- run : |
38- if [ "${{ github.ref }}" == "refs/heads/main" ]; then
39- echo "NEXT_PUBLIC_API_URL=${{ vars.PROD_API_URL }}" >> .env
40- echo "NEXT_PUBLIC_ENVIRONMENT=production" >> .env
41- elif [ "${{ github.ref }}" == "refs/heads/dev" ]; then
42- echo "NEXT_PUBLIC_API_URL=${{ vars.DEV_API_URL }}" >> .env
43- echo "NEXT_PUBLIC_ENVIRONMENT=test" >> .env
44- fi
45-
46- - name : Debug Environment File
47- run : |
48- echo "Contents of .env file:"
49- cat .env
50-
51- - name : Install Dependencies
52- run : pnpm install --frozen-lockfile
53-
54- - name : Build Frontend
55- run : pnpm build
56-
57- - name : Upload Build Artifacts
58- uses : actions/upload-artifact@v4
59- with :
60- name : next-build-${{ github.ref == 'refs/heads/main' && 'prod' || 'dev' }}
61- path : |
62- .next/**
63- !.next/cache/**
64- public/
65- package.json
66- pnpm-lock.yaml
67- .env
68- if-no-files-found : error
69- include-hidden-files : true
70- compression-level : 6
71-
72- - name : Debug Available Files
73- run : |
74- pwd
75- ls -la
76- find . -name ".next" -type d || echo ".next directory not found"
77-
78- deploy :
79- runs-on : aiadgen
80- needs : [test-and-build]
81- steps :
82- - name : Checkout Repository
83- uses : actions/checkout@v4
84-
85- - name : Download Build Artifacts
86- uses : actions/download-artifact@v4
87- with :
88- name : next-build-${{ github.ref == 'refs/heads/main' && 'prod' || 'dev' }}
89- path : .
90-
91- - name : Determine Deployment Environment
92- id : set-env
93- run : |
94- if [ " ${{ github.ref }}" == "refs/heads/main" ]; then
95- echo "DEPLOY_PORT=5000 " >> $GITHUB_ENV
96- echo "DEPLOY_NAME=ai-adgen-frontend-prod " >> $GITHUB_ENV
97- echo "DEPLOY_DIR=AI-Ad-Gen-Frontend-Prod" >> $GITHUB_ENV
98- elif [ " ${{ github.ref }}" == "refs/heads/dev" ]; then
99- echo "DEPLOY_PORT=5005 " >> $GITHUB_ENV
100- echo "DEPLOY_NAME=ai-adgen-frontend-dev " >> $GITHUB_ENV
101- echo "DEPLOY_DIR=AI-Ad-Gen-Frontend-Dev" >> $GITHUB_ENV
102- fi
103-
104- - name : Create Deployment Directory if Not Exists
105- run : |
106- sshpass -p "${{ secrets.SERVER_PASSWORD }}" ssh -o StrictHostKeyChecking=no \
107- "${{ secrets.SERVER_USER }}@${{ secrets.SERVER_IP }}" << EOF
108- if [ ! -d "/home/${{ secrets.SERVER_USER }}/${{ env.DEPLOY_DIR }}" ]; then
109- echo "Creating directory /home/${{ secrets.SERVER_USER }}/${{ env.DEPLOY_DIR }}"
110- mkdir -p /home/${{ secrets.SERVER_USER }}/${{ env.DEPLOY_DIR }}
111- else
112- echo "Directory /home/${{ secrets.SERVER_USER }}/${{ env.DEPLOY_DIR }} already exists"
113- fi
114- EOF
115-
116- - name : Deploy to Server
117- run : |
118- echo "Deploying .next build to server..."
119- sshpass -p "${{ secrets.SERVER_PASSWORD }}" scp -o StrictHostKeyChecking=no -r .next public/ package.json pnpm-lock.yaml .env \
120- "${{ secrets.SERVER_USER }}@${{ secrets.SERVER_IP }}:/home/${{ secrets.SERVER_USER }}/${{ env.DEPLOY_DIR }}"
121-
122- - name : Start Application on Server
123- run : |
124- sshpass -p "${{ secrets.SERVER_PASSWORD }}" ssh -o StrictHostKeyChecking=no \
125- "${{ secrets.SERVER_USER }}@${{ secrets.SERVER_IP }}" << 'EOF'
126- set -e # Stop script on error
127- export DEPLOY_NAME="${{ env.DEPLOY_NAME }}"
128- export DEPLOY_PORT="${{ env.DEPLOY_PORT }}"
129- export DEPLOY_DIR="${{ env.DEPLOY_DIR }}"
130- cd /home/${{ secrets.SERVER_USER }}/$DEPLOY_DIR
131- pnpm install --frozen-lockfile
132- pm2 delete $DEPLOY_NAME || true
133- PORT=$DEPLOY_PORT pm2 start pnpm --name $DEPLOY_NAME -- start
1+ name : Frontend CI/CD
2+
3+ on :
4+ push :
5+ branches :
6+ - dev
7+ - main
8+
9+ concurrency :
10+ group : ${{ github.workflow }}-${{ github.ref }}
11+ cancel-in-progress : false
12+
13+ jobs :
14+ test-and-build :
15+ runs-on : aiadgen
16+ steps :
17+ - name : Checkout Repository
18+ uses : actions/checkout@v4
19+
20+ - name : Setup Node.js
21+ uses : actions/setup-node@v4
22+ with :
23+ node-version : " 20.x"
24+
25+ - name : Debug Branch Information
26+ run : |
27+ echo "Current branch: ${{ github.ref }}"
28+ echo "Is main? ${{ github.ref == 'refs/heads/main' }}"
29+ echo "Is dev? ${{ github.ref == 'refs/heads/dev' }}"
30+
31+ - name : Setup pnpm
32+ uses : pnpm/action-setup@v3
33+ with :
34+ version : latest
35+
36+ - name : Create Environment-Specific .env File
37+ run : |
38+ if [ "${{ github.ref }}" == "refs/heads/main" ]; then
39+ echo "NEXT_PUBLIC_API_URL=${{ vars.PROD_API_URL }}" >> .env
40+ echo "NEXT_PUBLIC_ENVIRONMENT=production" >> .env
41+ elif [ "${{ github.ref }}" == "refs/heads/dev" ]; then
42+ echo "NEXT_PUBLIC_API_URL=${{ vars.DEV_API_URL }}" >> .env
43+ echo "NEXT_PUBLIC_ENVIRONMENT=test" >> .env
44+ fi
45+
46+ - name : Debug Environment File
47+ run : |
48+ echo "Contents of .env file:"
49+ cat .env
50+
51+ - name : Install Dependencies
52+ run : pnpm install --frozen-lockfile
53+
54+ - name : Build Frontend
55+ run : pnpm build
56+
57+ - name : Upload Build Artifacts
58+ uses : actions/upload-artifact@v4
59+ with :
60+ name : next-build-${{ github.ref == 'refs/heads/main' && 'prod' || 'dev' }}
61+ path : |
62+ .next/**
63+ !.next/cache/**
64+ public/
65+ package.json
66+ pnpm-lock.yaml
67+ .env
68+ if-no-files-found : error
69+ include-hidden-files : true
70+ compression-level : 6
71+
72+ - name : Debug Available Files
73+ run : |
74+ pwd
75+ ls -la
76+ find . -name ".next" -type d || echo ".next directory not found"
77+
78+ deploy :
79+ runs-on : aiadgen
80+ needs : [test-and-build]
81+ steps :
82+ - name : Checkout Repository
83+ uses : actions/checkout@v4
84+
85+ - name : Download Build Artifacts
86+ uses : actions/download-artifact@v4
87+ with :
88+ name : next-build-${{ github.ref == 'refs/heads/main' && 'prod' || 'dev' }}
89+ path : .
90+
91+ - name : Set Deployment Variables from GitHub Actions
92+ run : |
93+ if [ "${{ github.ref }}" == "refs/heads/main" ]; then
94+ echo "DEPLOY_PORT= ${{ vars.PROD_DEPLOY_PORT }}" >> $GITHUB_ENV
95+ echo "DEPLOY_NAME=${{ vars.PROD_DEPLOY_NAME }} " >> $GITHUB_ENV
96+ echo "DEPLOY_DIR=${{ vars.PROD_DEPLOY_DIR }} " >> $GITHUB_ENV
97+ elif [ "${{ github.ref }}" == "refs/heads/dev" ]; then
98+ echo "DEPLOY_PORT= ${{ vars.STAGING_DEPLOY_PORT }}" >> $GITHUB_ENV
99+ echo "DEPLOY_NAME=${{ vars.STAGING_DEPLOY_NAME }} " >> $GITHUB_ENV
100+ echo "DEPLOY_DIR=${{ vars.STAGING_DEPLOY_DIR }} " >> $GITHUB_ENV
101+ fi
102+
103+
104+ - name : Create Deployment Directory if Not Exists
105+ run : |
106+ sshpass -p "${{ secrets.SERVER_PASSWORD }}" ssh -o StrictHostKeyChecking=no \
107+ "${{ secrets.SERVER_USER }}@${{ secrets.SERVER_IP }}" << EOF
108+ if [ ! -d "/home/${{ secrets.SERVER_USER }}/${{ env.DEPLOY_DIR }}" ]; then
109+ echo "Creating directory /home/${{ secrets.SERVER_USER }}/${{ env.DEPLOY_DIR }}"
110+ mkdir -p /home/${{ secrets.SERVER_USER }}/${{ env.DEPLOY_DIR }}
111+ else
112+ echo "Directory /home/${{ secrets.SERVER_USER }}/${{ env.DEPLOY_DIR }} already exists"
113+ fi
114+ EOF
115+
116+ - name : Deploy to Server
117+ run : |
118+ echo "Deploying .next build to server..."
119+ sshpass -p "${{ secrets.SERVER_PASSWORD }}" scp -o StrictHostKeyChecking=no -r .next public/ package.json pnpm-lock.yaml .env \
120+ "${{ secrets.SERVER_USER }}@${{ secrets.SERVER_IP }}:/home/${{ secrets.SERVER_USER }}/${{ env.DEPLOY_DIR }}"
121+
122+ - name : Start Application on Server
123+ run : |
124+ sshpass -p "${{ secrets.SERVER_PASSWORD }}" ssh -o StrictHostKeyChecking=no \
125+ "${{ secrets.SERVER_USER }}@${{ secrets.SERVER_IP }}" << 'EOF'
126+ set -e # Stop script on error
127+ export DEPLOY_NAME="${{ env.DEPLOY_NAME }}"
128+ export DEPLOY_PORT="${{ env.DEPLOY_PORT }}"
129+ export DEPLOY_DIR="${{ env.DEPLOY_DIR }}"
130+ cd /home/${{ secrets.SERVER_USER }}/$DEPLOY_DIR
131+ pnpm install --frozen-lockfile
132+ pm2 delete $DEPLOY_NAME || true
133+ PORT=$DEPLOY_PORT pm2 start pnpm --name $DEPLOY_NAME -- start
134134 EOF
0 commit comments