Skip to content

fix docker run

fix docker run #81

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
env:
SERVER_IP: ${{ secrets.SERVER_IP }}
SERVER_USERNAME: ${{ secrets.SERVER_USERNAME }}
SERVER_PASSWORD: ${{ secrets.SERVER_PASSWORD }}
DATABASE_USERNAME: ${{ secrets.DATABASE_USERNAME }}
DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}
MONGO_DB_ROOT_USERNAME: ${{ secrets.MONGO_DB_ROOT_USERNAME }}
MONGO_DB_ROOT_PASSWORD: ${{ secrets.MONGO_DB_ROOT_PASSWORD }}
run: |
sshpass -p "$SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no $SERVER_USERNAME@$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 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=$DATABASE_USERNAME;Password=$DATABASE_PASSWORD;TrustServerCertificate=True;Multipleactiveresultsets=true" \
-e "ConnectionStrings:CacheConnectionString=eventhub.cache:6379" \
-e "SeqConfiguration:ServerUrl=http://eventhub.seq:5341" \
-e "HangfireSettings:Storage:ConnectionString=mongodb://$MONGO_DB_ROOT_USERNAME:[email protected]:27017/hangfire-webapi?authSource=admin" \
-v "${APPDATA}/Microsoft/UserSecrets:/home/app/.microsoft/usersecrets:ro" \
-v "${APPDATA}/ASP.NET/Https:/home/app/.aspnet/https:ro" \
$DOCKER_IMAGE_NAME
EOF