Skip to content

frontend deployment by Karan0005 #21

frontend deployment by Karan0005

frontend deployment by Karan0005 #21

name: frontend_pipeline
run-name: frontend deployment by ${{ github.actor }}
on:
push:
branches:
- develop
- uat
- main
paths:
- 'apps/frontend/**'
- 'libs/shared/**'
jobs:
build:
name: Build Frontend
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to DockerHub
uses: docker/login-action@v3.3.0
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Set Environment Variables
run: |
case $GITHUB_REF in
refs/heads/develop)
echo "APP_ENV=DEV" >> $GITHUB_ENV
echo "PORT_FRONTEND=80" >> $GITHUB_ENV
echo "DOCKER_TAG=develop" >> $GITHUB_ENV
;;
refs/heads/uat)
echo "APP_ENV=UAT" >> $GITHUB_ENV
echo "PORT_FRONTEND=80" >> $GITHUB_ENV
echo "DOCKER_TAG=uat" >> $GITHUB_ENV
;;
refs/heads/main)
echo "APP_ENV=PROD" >> $GITHUB_ENV
echo "PORT_FRONTEND=80" >> $GITHUB_ENV
echo "DOCKER_TAG=prod" >> $GITHUB_ENV
;;
esac
- name: Backup existing image if exists
run: |
IMAGE_EXISTS=$(docker manifest inspect ${{ secrets.DOCKER_USERNAME }}/frontend:${{ env.DOCKER_TAG }} > /dev/null 2>&1 && echo "YES" || echo "NO")
if [ "$IMAGE_EXISTS" = "YES" ]; then
echo "Backup existing image..."
docker pull ${{ secrets.DOCKER_USERNAME }}/frontend:${{ env.DOCKER_TAG }}
docker tag ${{ secrets.DOCKER_USERNAME }}/frontend:${{ env.DOCKER_TAG }} ${{ secrets.DOCKER_USERNAME }}/frontend:${{ env.DOCKER_TAG }}_backup
docker push ${{ secrets.DOCKER_USERNAME }}/frontend:${{ env.DOCKER_TAG }}_backup
else
echo "No existing image to backup."
fi
- name: Build and Push Frontend Docker Image
uses: docker/build-push-action@v6
with:
context: .
file: devops/frontend/Dockerfile
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/frontend:${{ env.DOCKER_TAG }}
build-args: |
APP_ENV=${{ env.APP_ENV }}
PORT_FRONTEND=${{ env.PORT_FRONTEND }}
deploy:
name: Deploy Frontend
runs-on: ubuntu-latest
needs: build
steps:
- name: Set Environment Variables
run: |
case $GITHUB_REF in
refs/heads/develop)
echo "APP_ENV=DEV" >> $GITHUB_ENV
echo "PORT_FRONTEND=80" >> $GITHUB_ENV
echo "DOCKER_TAG=develop" >> $GITHUB_ENV
echo "SERVER_HOST=${{ secrets.DEV_SERVER_HOST }}" >> $GITHUB_ENV
echo "SERVER_PASS_PHRASE=${{ secrets.DEV_SERVER_PASS_PHRASE }}" >> $GITHUB_ENV
echo "SERVER_PRIVATE_SSH_KEY<<EOF" >> $GITHUB_ENV
echo "${{ secrets.DEV_SERVER_PRIVATE_SSH_KEY }}" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "SERVER_USER=${{ secrets.DEV_SERVER_USER }}" >> $GITHUB_ENV
;;
refs/heads/uat)
echo "APP_ENV=UAT" >> $GITHUB_ENV
echo "PORT_FRONTEND=80" >> $GITHUB_ENV
echo "DOCKER_TAG=uat" >> $GITHUB_ENV
echo "SERVER_HOST=${{ secrets.UAT_SERVER_HOST }}" >> $GITHUB_ENV
echo "SERVER_PASS_PHRASE=${{ secrets.UAT_SERVER_PASS_PHRASE }}" >> $GITHUB_ENV
echo "SERVER_PRIVATE_SSH_KEY<<EOF" >> $GITHUB_ENV
echo "${{ secrets.UAT_SERVER_PRIVATE_SSH_KEY }}" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "SERVER_USER=${{ secrets.UAT_SERVER_USER }}" >> $GITHUB_ENV
;;
refs/heads/main)
echo "APP_ENV=PROD" >> $GITHUB_ENV
echo "PORT_FRONTEND=80" >> $GITHUB_ENV
echo "DOCKER_TAG=prod" >> $GITHUB_ENV
echo "SERVER_HOST=${{ secrets.PROD_SERVER_HOST }}" >> $GITHUB_ENV
echo "SERVER_PASS_PHRASE=${{ secrets.PROD_SERVER_PASS_PHRASE }}" >> $GITHUB_ENV
echo "SERVER_PRIVATE_SSH_KEY<<EOF" >> $GITHUB_ENV
echo "${{ secrets.PROD_SERVER_PRIVATE_SSH_KEY }}" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "SERVER_USER=${{ secrets.PROD_SERVER_USER }}" >> $GITHUB_ENV
;;
esac
- name: Deploy to Digital Ocean Droplet via SSH
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ env.SERVER_HOST }}
username: ${{ env.SERVER_USER }}
key: ${{ env.SERVER_PRIVATE_SSH_KEY }}
passphrase: ${{ env.SERVER_PASS_PHRASE }}
script: |
# Check if Docker is installed
if ! command -v docker &> /dev/null; then
echo "Docker not found, installing..."
# Install Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
else
echo "Docker is already installed."
fi
# Pull the latest image
docker pull ${{ secrets.DOCKER_USERNAME }}/frontend:${{ env.DOCKER_TAG }}
# Check if the container is running
if [ "$(docker ps -q -f name=frontend)" ]; then
# Stop and remove the old container if it exists
docker stop frontend
docker rm frontend
fi
# Run the new container
docker run -d \
--restart always \
--name frontend \
-p ${{ env.PORT_FRONTEND }}:${{ env.PORT_FRONTEND }} \
-e APP_ENV=${{ env.APP_ENV }} \
-e PORT_FRONTEND=${{ env.PORT_FRONTEND }} \
${{ secrets.DOCKER_USERNAME }}/frontend:${{ env.DOCKER_TAG }}
# Check if the new container started successfully
if [ "$(docker ps -q -f name=frontend)" ]; then
echo "Frontend container started successfully."
else
echo "New frontend container failed to start. Rolling back to backup..."
# Stop and remove failed container (if it exists)
docker stop frontend || true
docker rm frontend || true
# Pull the latest image
docker pull ${{ secrets.DOCKER_USERNAME }}/frontend:${{ env.DOCKER_TAG }}_backup
# Start the previous backed-up container
docker run -d \
--restart always \
--name frontend \
-p ${{ env.PORT_FRONTEND }}:${{ env.PORT_FRONTEND }} \
-e APP_ENV=${{ env.APP_ENV }} \
-e PORT_FRONTEND=${{ env.PORT_FRONTEND }} \
${{ secrets.DOCKER_USERNAME }}/frontend:${{ env.DOCKER_TAG }}_backup
echo "Rolled back to the previous frontend container."
fi
# Remove old frontend images (dangling images)
docker images --filter "dangling=true" --format "{{.ID}}" | xargs -r docker rmi || true