frontend deployment by Karan0005 #27
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: 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 | |
# Function to deploy frontend container | |
deploy_frontend() { | |
# Pull the latest image | |
echo "Pulling latest frontend image..." | |
docker pull ${{ secrets.DOCKER_USERNAME }}/frontend:${{ env.DOCKER_TAG }} || { | |
echo "Error: Failed to pull frontend image."; exit 1; | |
} | |
# Check if the container is running | |
if [ "$(docker ps -q -f name=frontend)" ]; then | |
# Stop and remove the old container if it exists | |
echo "Stopping and removing old frontend container..." | |
docker stop frontend && docker rm frontend || { | |
echo "Error: Failed to stop/remove frontend container."; exit 1; | |
} | |
fi | |
# Run the new container | |
echo "Starting new frontend 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 }} || { | |
echo "Error: Failed to start new frontend container."; exit 1; | |
} | |
} | |
# Function to roll back to backup frontend container | |
rollback_frontend() { | |
echo "Rolling back to previous frontend container..." | |
# Stop and remove failed container (if it exists) | |
docker stop frontend || true | |
docker rm frontend || true | |
# Pull the backup image | |
echo "Pulling backup frontend image..." | |
docker pull ${{ secrets.DOCKER_USERNAME }}/frontend:${{ env.DOCKER_TAG }}_backup || { | |
echo "Error: Failed to pull backup frontend image."; exit 1; | |
} | |
# 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 "Error: Failed to start backup frontend container."; exit 1; | |
} | |
echo "Rolled back to the previous frontend container." | |
} | |
# Deploy frontend container and rollback if it fails | |
deploy_frontend || rollback_frontend | |
# Remove old frontend images (dangling images) | |
docker images --filter "dangling=true" --format "{{.ID}}" | xargs -r docker rmi || true |