deploy to ssh #66
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: Logging directory | |
run: | | |
echo $pwd | |
- name: Logging folder | |
run: | | |
echo $ls | |
- name: Build Docker image | |
run: | | |
docker build -t $DOCKER_IMAGE_NAME 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 expect for SSH password automation | |
run: sudo apt-get install -y expect | |
- name: Deploy Docker container on remote server | |
env: | |
SERVER_IP: ${{ secrets.SERVER_IP }} | |
SERVER_USER: ${{ secrets.SERVER_USERNAME }} | |
SERVER_PASSWORD: ${{ secrets.SERVER_PASSWORD }} | |
run: | |
expect -c " | |
spawn ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP | |
expect \"password:\" | |
send \"$SERVER_PASSWORD\r\" | |
expect \"# \" | |
send \"docker pull $DOCKER_IMAGE_NAME\r\" | |
expect \"# \" | |
send \"docker stop $CONTAINER_NAME || true && docker rm $CONTAINER_NAME || true\r\" | |
expect \"# \" | |
send \"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=sa;Password=@Admin123;TrustServerCertificate=True;Multipleactiveresultsets=true" \ | |
-e "ConnectionStrings:CacheConnectionString=eventhub.cache:6379" \ | |
-e "SeqConfiguration:ServerUrl=http://eventhub.seq:5341" \ | |
-e "HangfireSettings:Storage:ConnectionString=mongodb://admin:[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\r\" | |
expect \"# \" | |
send \"exit\r\" | |
interact | |
" |