diff --git a/.github/workflows/master_eventhubapiserver.yml b/.github/workflows/master_eventhubapiserver.yml index 7cdb823..829d331 100644 --- a/.github/workflows/master_eventhubapiserver.yml +++ b/.github/workflows/master_eventhubapiserver.yml @@ -1,7 +1,4 @@ -# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy -# More GitHub Actions for Azure: https://github.com/Azure/actions - -name: Build and deploy ASP.Net Core app to Azure Web App - EventHubAPIServer +name: Build, push, and deploy Docker image for EventHub API on: push: @@ -9,51 +6,70 @@ on: - master workflow_dispatch: -jobs: - build: - runs-on: windows-latest +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: Set up .NET Core - uses: actions/setup-dotnet@v4 - with: - dotnet-version: "8.x" - - - name: Store dependencies - run: dotnet restore source/EventHub.Presentation/EventHub.Presentation.csproj + - name: Log in to Docker Hub + run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin - - name: Build with dotnet - run: dotnet build source/EventHub.Presentation/EventHub.Presentation.csproj -c Release -o ./build + - name: Build Docker image + run: | + docker build -t $DOCKER_IMAGE_NAME -f source/EventHub.Presentation/Dockerfile . - - name: dotnet publish - run: dotnet publish source/EventHub.Presentation/EventHub.Presentation.csproj -c Release -o ./publish - - - name: Upload artifact for deployment job - uses: actions/upload-artifact@v4 - with: - name: .net-app - path: ./publish + - name: Push Docker image to Docker Hub + run: docker push $DOCKER_IMAGE_NAME deploy: - runs-on: windows-latest - needs: build - environment: - name: "Production" - url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} - + runs-on: ubuntu-latest + needs: build-and-push + steps: - - name: Download artifact from build job - uses: actions/download-artifact@v4 + - name: Setup SSH + uses: webfactory/ssh-agent@v0.8.0 with: - name: .net-app + ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} + + - name: Install expect for SSH password automation + run: sudo apt-get install -y expect - - name: Deploy to Azure Web App - id: deploy-to-webapp - uses: azure/webapps-deploy@v3 - with: - app-name: "EventHubAPIServer" - slot-name: "Production" - package: . - publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_2E162A0AE3134EF8992B4DD6656A4E66 }} + - 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: | + ssh -o StrictHostKeyChecking=no $SERVER_USERNAME@$SERVER_IP << EOF + # 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 \ + --restart always \ + -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:$MONGO_DB_ROOT_PASSWORD@eventhub.hangfire: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 diff --git a/deploy/docker-compose.production.yml b/deploy/docker-compose.production.yml index d1f3697..8988287 100644 --- a/deploy/docker-compose.production.yml +++ b/deploy/docker-compose.production.yml @@ -2,16 +2,26 @@ version: '3.8' services: eventhub.api: - image: eventhubcontainer.azurecr.io/eventhub-api:linux-latest + image: ${DOCKER_REGISTRY-}eventhub-api:${PLATFORM:-linux}-${TAG:-latest} container_name: eventhub.api environment: - - ASPNETCORE_ENVIRONMENT=Production + - ASPNETCORE_ENVIRONMENT=Development - ASPNETCORE_URLS=http://+:80 - "ConnectionStrings:DefaultConnectionString=Server=eventhub.db,1433;Database=EventHubDB;User Id=sa;Password=@Admin123;TrustServerCertificate=True;Multipleactiveresultsets=true" - "ConnectionStrings:CacheConnectionString=eventhub.cache:6379" - "SeqConfiguration:ServerUrl=http://eventhub.seq:5341" - "HangfireSettings:Storage:ConnectionString=mongodb://admin:admin@eventhub.hangfire:27017/hangfire-webapi?authSource=admin" build: - context: ../source/EventHub.Presentation - dockerfile: Dockerfile - restart: always \ No newline at end of file + context: .. + dockerfile: source/EventHub.Presentation/Dockerfile + restart: always + ports: + - "5002:80" + volumes: + - ${APPDATA}/Microsoft/UserSecrets:/home/app/.microsoft/usersecrets:ro + - ${APPDATA}/ASP.NET/Https:/home/app/.aspnet/https:ro + +networks: + default: + name: eventhub + driver: bridge \ No newline at end of file