Skip to content

Commit 784086a

Browse files
deploy to ssh
1 parent 2b9d31d commit 784086a

File tree

2 files changed

+71
-46
lines changed

2 files changed

+71
-46
lines changed
+56-41
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,74 @@
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
52

63
on:
74
push:
85
branches:
96
- master
107
workflow_dispatch:
118

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"
1512

13+
jobs:
14+
build-and-push:
15+
runs-on: ubuntu-latest
16+
1617
steps:
1718
- uses: actions/checkout@v4
1819

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
2622

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 .
2926
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
3829

3930
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+
4634
steps:
47-
- name: Download artifact from build job
48-
uses: actions/download-artifact@v4
35+
- name: Setup SSH
36+
uses: webfactory/[email protected]
4937
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
5142

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+
"

deploy/docker-compose.production.yml

+15-5
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,26 @@ version: '3.8'
22

33
services:
44
eventhub.api:
5-
image: eventhubcontainer.azurecr.io/eventhub-api:linux-latest
5+
image: ${DOCKER_REGISTRY-}eventhub-api:${PLATFORM:-linux}-${TAG:-latest}
66
container_name: eventhub.api
77
environment:
8-
- ASPNETCORE_ENVIRONMENT=Production
8+
- ASPNETCORE_ENVIRONMENT=Development
99
- ASPNETCORE_URLS=http://+:80
1010
- "ConnectionStrings:DefaultConnectionString=Server=eventhub.db,1433;Database=EventHubDB;User Id=sa;Password=@Admin123;TrustServerCertificate=True;Multipleactiveresultsets=true"
1111
- "ConnectionStrings:CacheConnectionString=eventhub.cache:6379"
1212
- "SeqConfiguration:ServerUrl=http://eventhub.seq:5341"
1313
- "HangfireSettings:Storage:ConnectionString=mongodb://admin:[email protected]:27017/hangfire-webapi?authSource=admin"
1414
build:
15-
context: ../source/EventHub.Presentation
16-
dockerfile: Dockerfile
17-
restart: always
15+
context: ..
16+
dockerfile: source/EventHub.Presentation/Dockerfile
17+
restart: always
18+
ports:
19+
- "5002:80"
20+
volumes:
21+
- ${APPDATA}/Microsoft/UserSecrets:/home/app/.microsoft/usersecrets:ro
22+
- ${APPDATA}/ASP.NET/Https:/home/app/.aspnet/https:ro
23+
24+
networks:
25+
default:
26+
name: eventhub
27+
driver: bridge

0 commit comments

Comments
 (0)