Skip to content

Merge pull request #480 from hngprojects/auth-fixes #407

Merge pull request #480 from hngprojects/auth-fixes

Merge pull request #480 from hngprojects/auth-fixes #407

Workflow file for this run

name: Frontend CI/CD
on:
push:
branches:
- dev
- main
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false
jobs:
test-and-build:
runs-on: aiadgen
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20.x"
- name: Debug Branch Information
run: |
echo "Current branch: ${{ github.ref }}"
echo "Is main? ${{ github.ref == 'refs/heads/main' }}"
echo "Is dev? ${{ github.ref == 'refs/heads/dev' }}"
- name: Setup pnpm
uses: pnpm/action-setup@v3
with:
version: latest
- name: Create Environment-Specific .env File
run: |
if [ "${{ github.ref }}" == "refs/heads/main" ]; then
echo "NEXT_PUBLIC_API_URL=${{ vars.PROD_API_URL }}" >> .env
echo "NEXT_PUBLIC_ENVIRONMENT=production" >> .env
elif [ "${{ github.ref }}" == "refs/heads/dev" ]; then
echo "NEXT_PUBLIC_API_URL=${{ vars.DEV_API_URL }}" >> .env
echo "NEXT_PUBLIC_ENVIRONMENT=test" >> .env
fi
- name: Debug Environment File
run: |
echo "Contents of .env file:"
cat .env
- name: Install Dependencies
run: pnpm install --frozen-lockfile
- name: Build Frontend
run: pnpm build
- name: Upload Build Artifacts
uses: actions/upload-artifact@v4
with:
name: next-build-${{ github.ref == 'refs/heads/main' && 'prod' || 'dev' }}
path: |
.next/**
!.next/cache/**
public/
package.json
pnpm-lock.yaml
.env
if-no-files-found: error
include-hidden-files: true
compression-level: 6
- name: Debug Available Files
run: |
pwd
ls -la
find . -name ".next" -type d || echo ".next directory not found"
deploy:
runs-on: aiadgen
needs: [test-and-build]
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Download Build Artifacts
uses: actions/download-artifact@v4
with:
name: next-build-${{ github.ref == 'refs/heads/main' && 'prod' || 'dev' }}
path: .
- name: Set Deployment Variables from GitHub Actions
run: |
if [ "${{ github.ref }}" == "refs/heads/main" ]; then
echo "DEPLOY_PORT=${{ vars.PROD_DEPLOY_PORT }}" >> $GITHUB_ENV
echo "DEPLOY_NAME=${{ vars.PROD_DEPLOY_NAME }}" >> $GITHUB_ENV
echo "DEPLOY_DIR=${{ vars.PROD_DEPLOY_DIR }}" >> $GITHUB_ENV
elif [ "${{ github.ref }}" == "refs/heads/dev" ]; then
echo "DEPLOY_PORT=${{ vars.STAGING_DEPLOY_PORT }}" >> $GITHUB_ENV
echo "DEPLOY_NAME=${{ vars.STAGING_DEPLOY_NAME }}" >> $GITHUB_ENV
echo "DEPLOY_DIR=${{ vars.STAGING_DEPLOY_DIR }}" >> $GITHUB_ENV
fi
- name: Create Deployment Directory if Not Exists
run: |
sshpass -p "${{ secrets.SERVER_PASSWORD }}" ssh -o StrictHostKeyChecking=no \
"${{ secrets.SERVER_USER }}@${{ secrets.SERVER_IP }}" << EOF
if [ ! -d "/home/${{ secrets.SERVER_USER }}/${{ env.DEPLOY_DIR }}" ]; then
echo "Creating directory /home/${{ secrets.SERVER_USER }}/${{ env.DEPLOY_DIR }}"
mkdir -p /home/${{ secrets.SERVER_USER }}/${{ env.DEPLOY_DIR }}
else
echo "Directory /home/${{ secrets.SERVER_USER }}/${{ env.DEPLOY_DIR }} already exists"
fi
EOF
- name: Deploy to Server
run: |
echo "Deploying .next build to server..."
sshpass -p "${{ secrets.SERVER_PASSWORD }}" scp -o StrictHostKeyChecking=no -r .next public/ package.json pnpm-lock.yaml .env \
"${{ secrets.SERVER_USER }}@${{ secrets.SERVER_IP }}:/home/${{ secrets.SERVER_USER }}/${{ env.DEPLOY_DIR }}"
- name: Start Application on Server
run: |
sshpass -p "${{ secrets.SERVER_PASSWORD }}" ssh -o StrictHostKeyChecking=no \
"${{ secrets.SERVER_USER }}@${{ secrets.SERVER_IP }}" << 'EOF'
set -e # Stop script on error
export DEPLOY_NAME="${{ env.DEPLOY_NAME }}"
export DEPLOY_PORT="${{ env.DEPLOY_PORT }}"
export DEPLOY_DIR="${{ env.DEPLOY_DIR }}"
cd /home/${{ secrets.SERVER_USER }}/$DEPLOY_DIR
pnpm install --frozen-lockfile
pm2 delete $DEPLOY_NAME || true
PORT=$DEPLOY_PORT pm2 start pnpm --name $DEPLOY_NAME -- start
EOF