CD #952
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: CD | |
| on: | |
| workflow_run: | |
| workflows: ["CI"] | |
| types: | |
| - completed | |
| jobs: | |
| deploy: | |
| if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Add SSH key | |
| run: echo "${{ secrets.EC2_SSH_KEY }}" > /tmp/ssh_key && chmod 600 /tmp/ssh_key | |
| - name: Determine deployment target | |
| id: determine-target | |
| run: | | |
| backend_target=$(ssh -i /tmp/ssh_key -o StrictHostKeyChecking=no -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o ConnectTimeout=10 ec2-user@${{ secrets.LIVE_SERVER_IP }} "cat /home/ec2-user/backend_target.txt || (echo 'back_green' > /home/ec2-user/backend_target.txt && cat /home/ec2-user/backend_target.txt)") | |
| if [ "$backend_target" = "back_blue" ]; then | |
| new_target="back_green" | |
| else | |
| new_target="back_blue" | |
| fi | |
| echo "backend_target=$backend_target" >> $GITHUB_OUTPUT | |
| echo "new_target=$new_target" >> $GITHUB_OUTPUT | |
| ssh -i /tmp/ssh_key -o StrictHostKeyChecking=no -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o ConnectTimeout=10 ec2-user@${{ secrets.LIVE_SERVER_IP }} "echo '$new_target' > /home/ec2-user/backend_target.txt" | |
| - name: Login to Docker Hub on EC2 instance | |
| run: | | |
| ssh -i /tmp/ssh_key -o StrictHostKeyChecking=no -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o ConnectTimeout=10 ec2-user@${{ secrets.LIVE_SERVER_IP }} "echo '${{ secrets.DOCKERHUB_PW }}' | docker login -u '${{ secrets.DOCKERHUB_USERNAME }}' --password-stdin" | |
| - name: Pull latest image and deploy new version | |
| run: | | |
| ssh -i /tmp/ssh_key -o StrictHostKeyChecking=no -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o ConnectTimeout=10 ec2-user@${{ secrets.LIVE_SERVER_IP }} " | |
| docker pull moaguide/moaguide:back_last | |
| cd /home/ec2-user | |
| docker-compose -f docker-compose-back.yml up -d --no-deps \${{ steps.determine-target.outputs.new_target }} | |
| " | |
| - name: Health check new environment | |
| id: health-check | |
| run: | | |
| ssh -i /tmp/ssh_key -o StrictHostKeyChecking=no -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o ConnectTimeout=10 ec2-user@${{ secrets.LIVE_SERVER_IP }} " | |
| health_url='' | |
| if [ '${{ steps.determine-target.outputs.new_target }}' = 'back_blue' ]; then | |
| health_url='http://${{ secrets.Domain }}:8080/hc' | |
| else | |
| health_url='http://${{ secrets.Domain }}:8081/hc' | |
| fi | |
| for i in {1..10}; do | |
| http_status=\$(curl -s -o /dev/null -w '%{http_code}' \$health_url) | |
| if [ \"\$http_status\" -eq \"200\" ]; then | |
| echo 'Health check passed!' | |
| exit 0 | |
| fi | |
| echo 'Waiting for healthy status...' | |
| sleep 30 | |
| done | |
| echo 'Health check failed!' | |
| exit 1 | |
| " | |
| - name: Update current file | |
| if: failure() | |
| run: | | |
| backend_target=$(ssh -i /tmp/ssh_key -o StrictHostKeyChecking=no -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o ConnectTimeout=10 ec2-user@${{ secrets.LIVE_SERVER_IP }} "cat /home/ec2-user/backend_target.txt || (echo 'back_green' > /home/ec2-user/backend_target.txt && cat /home/ec2-user/backend_target.txt)") | |
| if [ "$backend_target" = "back_blue" ]; then | |
| new_target="back_green" | |
| else | |
| new_target="back_blue" | |
| fi | |
| echo "backend_target=$backend_target" >> $GITHUB_OUTPUT | |
| echo "new_target=$new_target" >> $GITHUB_OUTPUT | |
| ssh -i /tmp/ssh_key -o StrictHostKeyChecking=no -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o ConnectTimeout=10 ec2-user@${{ secrets.LIVE_SERVER_IP }} "echo '$new_target' > /home/ec2-user/backend_target.txt" | |
| - name: Update NGINX configuration and reload | |
| if: success() | |
| run: | | |
| ssh -t -i /tmp/ssh_key -o StrictHostKeyChecking=no -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o ConnectTimeout=10 ec2-user@${{ secrets.LIVE_SERVER_IP }} " | |
| if [ '${{ steps.determine-target.outputs.new_target }}' = 'back_blue' ]; then | |
| sed -i 's/server back_green:8081/server back_blue:8080/' /home/ec2-user/nginx/nginx.conf | |
| else | |
| sed -i 's/server back_blue:8080/server back_green:8081/' /home/ec2-user/nginx/nginx.conf | |
| fi | |
| docker-compose -f docker-compose-back.yml exec nginx nginx -s reload | |
| " | |
| - name: Stop old environment | |
| if: success() | |
| run: | | |
| ssh -t -i /tmp/ssh_key -o StrictHostKeyChecking=no -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o ConnectTimeout=10 ec2-user@${{ secrets.LIVE_SERVER_IP }} " | |
| if [ '${{ steps.determine-target.outputs.new_target }}' = 'back_blue' ]; then | |
| docker-compose -f docker-compose-back.yml stop back_green | |
| else | |
| docker-compose -f docker-compose-back.yml stop back_blue | |
| fi | |
| " |