update #196
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: Deploy to AWS Lightsail | |
| on: | |
| push: | |
| branches: | |
| - main | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| # Checkout code | |
| - name: Checkout code | |
| uses: actions/checkout@v2 | |
| # Get Commit Details | |
| - name: Get Commit Details | |
| id: commit_info | |
| run: | | |
| echo "commit_message=$(git log -1 --pretty=%B | sed ':a;N;$!ba;s/\n/ /g' | sed 's/"_\\"_/g')" >> $GITHUB_ENV | |
| echo "commit_author=$(git log -1 --pretty=%an)" >> $GITHUB_ENV | |
| # Setup SSH | |
| - name: Setup SSH | |
| uses: webfactory/ssh-agent@v0.5.2 | |
| with: | |
| ssh-private-key: ${{ secrets.LIGHTSAIL_SSH_PRIVATE_KEY }} | |
| # Verify SSH connection | |
| - name: Verify SSH Connection | |
| run: | | |
| ssh -o StrictHostKeyChecking=no ${{ secrets.LIGHTSAIL_USER }}@${{ secrets.LIGHTSAIL_HOST }} "echo Connection successful" | |
| # Copy files to Lightsail | |
| - name: Copy files to Lightsail | |
| run: | | |
| rsync -avz -e "ssh -o StrictHostKeyChecking=no" ./ ${{ secrets.LIGHTSAIL_USER }}@${{ secrets.LIGHTSAIL_HOST }}:/backend | |
| # SSH and deploy with Docker | |
| - name: SSH and Deploy Docker | |
| run: | | |
| ssh -o StrictHostKeyChecking=no ${{ secrets.LIGHTSAIL_USER }}@${{ secrets.LIGHTSAIL_HOST }} << 'EOF' | |
| cd /backend | |
| # Backup the current running container | |
| if docker ps -q -f name=backend; then | |
| docker tag backend:latest backend:backup || echo "Failed to tag existing image" | |
| fi | |
| # Try to build and deploy the new Docker container | |
| if ! docker-compose -f docker-compose-prod.yml down || ! docker-compose -f docker-compose-prod.yml up -d --build --remove-orphans; then | |
| echo "Deployment failed. Rolling back..." | |
| docker-compose -f docker-compose-prod.yml down | |
| if docker images | grep -q 'backend:backup'; then | |
| docker tag backend:backup backend:latest | |
| docker-compose -f docker-compose-prod.yml up -d | |
| echo "Rollback to the previous version was successful." | |
| else | |
| echo "No backup available for rollback. Exiting..." | |
| exit 1 | |
| fi | |
| fi | |
| echo "Deployment successful" | |
| EOF | |
| # Notify Slack - Success | |
| - name: Notify Slack - Success | |
| if: success() | |
| uses: slackapi/slack-github-action@v2.0.0 | |
| with: | |
| method: chat.postMessage | |
| token: ${{ secrets.SLACK_BOT_TOKEN }} | |
| payload: | | |
| channel: ${{ secrets.SLACK_CHANNEL_ID }} | |
| text: "✅ Backend Deployment to AWS Lightsail succeeded! 🚀" | |
| attachments: | |
| - color: "36a64f" | |
| fields: | |
| - title: "Commit" | |
| short: true | |
| value: "${{ env.commit_message }}" | |
| - title: "By" | |
| short: true | |
| value: "${{ github.actor }} | ${{ env.commit_author }}" | |
| - title: "Status" | |
| short: true | |
| value: "Success" | |
| - title: "Deployment Details" | |
| short: true | |
| value: "The latest code was successfully deployed to AWS Lightsail." | |
| # Notify Slack - Failure | |
| - name: Notify Slack - Failure | |
| if: failure() | |
| uses: slackapi/slack-github-action@v2.0.0 | |
| with: | |
| method: chat.postMessage | |
| token: ${{ secrets.SLACK_BOT_TOKEN }} | |
| payload: | | |
| channel: ${{ secrets.SLACK_CHANNEL_ID }} | |
| text: "❌ Backend Deployment to AWS Lightsail failed! 😞" | |
| attachments: | |
| - color: "ff0000" | |
| fields: | |
| - title: "Commit" | |
| short: true | |
| value: "${{ env.commit_message }}" | |
| - title: "By" | |
| short: true | |
| value: "${{ github.actor }} | ${{ env.commit_author }}" | |
| - title: "Status" | |
| short: true | |
| value: "Failed" | |
| - title: "Error" | |
| short: true | |
| value: "The deployment process encountered an issue. Check the logs for details." |