|
1 |
| -# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy |
2 |
| -# More GitHub Actions for Azure: https://github.com/Azure/actions |
3 |
| - |
4 |
| -name: Build and deploy ASP.Net Core app to Azure Web App - EventHubAPIServer |
| 1 | +name: Build, push, and deploy Docker image for EventHub API |
5 | 2 |
|
6 | 3 | on:
|
7 | 4 | push:
|
8 | 5 | branches:
|
9 | 6 | - master
|
10 | 7 | workflow_dispatch:
|
11 | 8 |
|
12 |
| -jobs: |
13 |
| - build: |
14 |
| - runs-on: windows-latest |
| 9 | +env: |
| 10 | + DOCKER_IMAGE_NAME: tranvuongduy2003/eventhub-api:linux-latest # Replace with your Docker Hub repo name |
| 11 | + CONTAINER_NAME: "eventhub.api" |
15 | 12 |
|
| 13 | +jobs: |
| 14 | + build-and-push: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
16 | 17 | steps:
|
17 | 18 | - uses: actions/checkout@v4
|
18 | 19 |
|
19 |
| - - name: Set up .NET Core |
20 |
| - uses: actions/setup-dotnet@v4 |
21 |
| - with: |
22 |
| - dotnet-version: "8.x" |
23 |
| - |
24 |
| - - name: Store dependencies |
25 |
| - run: dotnet restore source/EventHub.Presentation/EventHub.Presentation.csproj |
| 20 | + - name: Log in to Docker Hub |
| 21 | + run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin |
26 | 22 |
|
27 |
| - - name: Build with dotnet |
28 |
| - run: dotnet build source/EventHub.Presentation/EventHub.Presentation.csproj -c Release -o ./build |
| 23 | + - name: Build Docker image |
| 24 | + run: | |
| 25 | + docker build -t $DOCKER_IMAGE_NAME -f source/EventHub.Presentation/Dockerfile . |
29 | 26 |
|
30 |
| - - name: dotnet publish |
31 |
| - run: dotnet publish source/EventHub.Presentation/EventHub.Presentation.csproj -c Release -o ./publish |
32 |
| - |
33 |
| - - name: Upload artifact for deployment job |
34 |
| - uses: actions/upload-artifact@v4 |
35 |
| - with: |
36 |
| - name: .net-app |
37 |
| - path: ./publish |
| 27 | + - name: Push Docker image to Docker Hub |
| 28 | + run: docker push $DOCKER_IMAGE_NAME |
38 | 29 |
|
39 | 30 | deploy:
|
40 |
| - runs-on: windows-latest |
41 |
| - needs: build |
42 |
| - environment: |
43 |
| - name: "Production" |
44 |
| - url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} |
45 |
| - |
| 31 | + runs-on: ubuntu-latest |
| 32 | + needs: build-and-push |
| 33 | + |
46 | 34 | steps:
|
47 |
| - - name: Download artifact from build job |
48 |
| - uses: actions/download-artifact@v4 |
| 35 | + - name: Setup SSH |
| 36 | + uses: webfactory/[email protected] |
49 | 37 | with:
|
50 |
| - name: .net-app |
| 38 | + ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} |
| 39 | + |
| 40 | + - name: Install expect for SSH password automation |
| 41 | + run: sudo apt-get install -y expect |
51 | 42 |
|
52 |
| - - name: Deploy to Azure Web App |
53 |
| - id: deploy-to-webapp |
54 |
| - uses: azure/webapps-deploy@v3 |
55 |
| - with: |
56 |
| - app-name: "EventHubAPIServer" |
57 |
| - slot-name: "Production" |
58 |
| - package: . |
59 |
| - publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_2E162A0AE3134EF8992B4DD6656A4E66 }} |
| 43 | + - name: Deploy Docker container on remote server |
| 44 | + env: |
| 45 | + SERVER_IP: ${{ secrets.SERVER_IP }} |
| 46 | + SERVER_USER: ${{ secrets.SERVER_USERNAME }} |
| 47 | + SERVER_PASSWORD: ${{ secrets.SERVER_PASSWORD }} |
| 48 | + run: |
| 49 | + expect -c " |
| 50 | + spawn ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP |
| 51 | + expect \"password:\" |
| 52 | + send \"$SERVER_PASSWORD\r\" |
| 53 | + expect \"# \" |
| 54 | + send \"docker pull $DOCKER_IMAGE_NAME\r\" |
| 55 | + expect \"# \" |
| 56 | + send \"docker stop $CONTAINER_NAME || true && docker rm $CONTAINER_NAME || true\r\" |
| 57 | + expect \"# \" |
| 58 | + send \"docker run -d --name $CONTAINER_NAME \ |
| 59 | + --network eventhub \ |
| 60 | + --restart always \ |
| 61 | + -p 5002:80 \ |
| 62 | + -e ASPNETCORE_ENVIRONMENT=Development \ |
| 63 | + -e ASPNETCORE_URLS=http://+:80 \ |
| 64 | + -e "ConnectionStrings:DefaultConnectionString=Server=eventhub.db,1433;Database=EventHubDB;User Id=sa;Password=@Admin123;TrustServerCertificate=True;Multipleactiveresultsets=true" \ |
| 65 | + -e "ConnectionStrings:CacheConnectionString=eventhub.cache:6379" \ |
| 66 | + -e "SeqConfiguration:ServerUrl=http://eventhub.seq:5341" \ |
| 67 | + -e "HangfireSettings:Storage:ConnectionString=mongodb://admin:[email protected]:27017/hangfire-webapi?authSource=admin" \ |
| 68 | + -v "${APPDATA}/Microsoft/UserSecrets:/home/app/.microsoft/usersecrets:ro" \ |
| 69 | + -v "${APPDATA}/ASP.NET/Https:/home/app/.aspnet/https:ro" \ |
| 70 | + $DOCKER_IMAGE_NAME\r\" |
| 71 | + expect \"# \" |
| 72 | + send \"exit\r\" |
| 73 | + interact |
| 74 | + " |
0 commit comments