Skip to content

made dockerfile for services and cd pipeline #1

made dockerfile for services and cd pipeline

made dockerfile for services and cd pipeline #1

Workflow file for this run

name: Build and Deploy the server to EC2
on:
push:
branches:
- main
paths:
- 'services/**'
- '.github/**'
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
# Check out the repository
- name: Check Out Repo
uses: actions/checkout@v3
# Log in to Docker Hub
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
# Build and push Docker image to Docker Hub
- name: Build and Push Docker Image
uses: docker/build-push-action@v5
with:
context: ./services
file: ./services/Dockerfile
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/hubble-services:latest
# Verify the pushed image
- name: Verify Pushed Image
run: docker pull ${{ secrets.DOCKER_USERNAME }}/hubble-services:latest
# Deploy the Docker image to an EC2 instance
- name: Deploy to EC2
uses: appleboy/ssh-action@v0.1.8
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
# Pull the latest application image
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/hubble-services:latest
# Pull the latest Redis image
sudo docker pull redis:alpine
# Stop and remove existing containers
sudo docker stop hubble-backend || true
sudo docker rm hubble-backend || true
sudo docker stop redis-container || true
sudo docker rm redis-container || true
# Run Redis container
sudo docker run -d --name redis-container -p ${{ secrets.REDIS_PORT }}:6379 redis:alpine
# Run application server container
sudo docker run -d --name hubble-backend -p ${{ secrets.PORT}}:${{ secrets.PORT}} -e "REDIS_PORT=${{ secrets.REDIS_PORT}}" -e "REDIS_HOST=${{ secrets.REDIS_HOST}}" -e "PORT=${{ secrets.PORT}}" ${{ secrets.DOCKER_USERNAME }}/hubble-services:latest