fix cicd and docker compose file #79
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: Build, push, and deploy Docker image for EventHub API | |
on: | |
push: | |
branches: | |
- master | |
workflow_dispatch: | |
env: | |
DOCKER_IMAGE_NAME: tranvuongduy2003/eventhub-api:linux-latest # Replace with your Docker Hub repo name | |
CONTAINER_NAME: "eventhub.api" | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Log in to Docker Hub | |
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin | |
- name: Build Docker image | |
run: | | |
docker build -t $DOCKER_IMAGE_NAME -f source/EventHub.Presentation/Dockerfile . | |
- name: Push Docker image to Docker Hub | |
run: docker push $DOCKER_IMAGE_NAME | |
deploy: | |
runs-on: ubuntu-latest | |
needs: build-and-push | |
steps: | |
- name: Setup SSH | |
uses: webfactory/[email protected] | |
with: | |
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | |
- name: Install sshpass | |
run: | | |
sudo apt-get update | |
sudo apt-get install -y sshpass | |
- name: Deploy Docker container on remote server | |
run: | | |
sshpass -p "${{ secrets.SERVER_PASSWORD}}" ssh -o StrictHostKeyChecking=no ${{ secrets.SERVER_USERNAME}}@${{ secrets.SERVER_IP}} << EOF | |
# Login to Docker Hub | |
echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin | |
# Pull the latest image from Docker Hub | |
docker pull $DOCKER_IMAGE_NAME | |
# Stop and remove the old container if it exists | |
docker stop $CONTAINER_NAME || true | |
docker container rm $CONTAINER_NAME || true | |
# Run the new container | |
docker run -d --name $CONTAINER_NAME \ | |
--network eventhub \ | |
-p 5002:80 \ | |
-e ASPNETCORE_ENVIRONMENT=Development \ | |
-e ASPNETCORE_URLS=http://+:80 \ | |
-e "ConnectionStrings__DefaultConnectionString=Server=eventhub.db,1433;Database=EventHubDB;User Id=${{ secrets.DATABASE_USERNAME }};Password=${{ secrets.DATABASE_PASSWORD }};TrustServerCertificate=True;Multipleactiveresultsets=true" \ | |
-e "ConnectionStrings__CacheConnectionString=eventhub.cache:6379" \ | |
-e "SeqConfiguration__ServerUrl=http://eventhub.seq:5341" \ | |
-e "HangfireSettings__Storage:ConnectionString=mongodb://${{ secrets.MONGO_DB_ROOT_USERNAME }}:${{ secrets.MONGO_DB_ROOT_PASSWORD }}@eventhub.hangfire:27017/hangfire-webapi?authSource=admin" \ | |
$DOCKER_IMAGE_NAME && docker container logs $CONTAINER_NAME | |
EOF |