Merge pull request #35 from SLUDI/wallet-recovery #9
This file contains hidden or 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 → Deploy Backend App to EC2 | |
| on: | |
| push: | |
| branches: [ "prod" ] | |
| env: | |
| IMAGE_NAME: tishan001/sludi | |
| REMOTE_DIR: /home/ubuntu/sludi/backend | |
| jobs: | |
| build-and-push: | |
| name: Build image and push to Docker Hub | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up QEMU (for multi-platform builds, optional) | |
| uses: docker/setup-qemu-action@v2 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v4 | |
| with: | |
| context: . | |
| push: true | |
| tags: ${{ env.IMAGE_NAME }}:latest | |
| deploy: | |
| name: SSH to EC2 and run docker-compose | |
| runs-on: ubuntu-latest | |
| needs: build-and-push | |
| steps: | |
| - name: Deploying to EC2 | |
| run: echo "Deploying backend app to EC2..." | |
| - name: SSH and deploy backend | |
| uses: appleboy/ssh-action@v0.1.8 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USER }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| port: ${{ secrets.SSH_PORT || '22' }} | |
| script: | | |
| # Check docker is available | |
| docker --version || exit 1 | |
| # Go to backend directory | |
| cd ${{ env.REMOTE_DIR }} || exit 1 | |
| echo "docker-compose.yml located at $(pwd)/docker-compose.yml" | |
| # Stop and remove existing containers | |
| docker-compose down | |
| # Pull the latest image | |
| docker-compose pull || docker pull ${{ env.IMAGE_NAME }}:latest | |
| # Recreate and start containers | |
| docker-compose up -d --build | |
| # Cleanup unused images | |
| docker image prune -f || true | |
| echo "Backend deployment completed successfully!" |