AWS CD #1
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: AWS CD | |
on: | |
workflow_run: | |
workflows: ['AWS 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: | | |
frontend_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/frontend_target.txt || (echo 'front_green' > /home/ec2-user/frontend_target.txt && cat /home/ec2-user/frontend_target.txt)") | |
if [ "$frontend_target" = "front_blue" ]; then | |
new_target="front_green" | |
else | |
new_target="front_blue" | |
fi | |
echo "frontend_target=$frontend_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/frontend_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:front_last | |
cd /home/ec2-user | |
docker-compose -f docker-compose-front.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.DOMAIN }} " | |
health_url='' | |
if [ "$frontend_target" = 'front_blue' ]; then | |
health_url='http://${{ secrets.DOMAIN }}:3000' | |
else | |
health_url='http://${{ secrets.DOMAIN }}:3001' | |
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 frontend target file on failure | |
if: failure() | |
run: | | |
frontend_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/frontend_target.txt || (echo 'front_green' > /home/ec2-user/frontend_target.txt && cat /home/ec2-user/frontend_target.txt)") | |
if [ "$frontend_target" = "front_blue" ]; then | |
new_target="front_green" | |
else | |
new_target="front_blue" | |
fi | |
echo "frontend_target=$frontend_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/frontend_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 }}' = 'front_blue' ]; then | |
sed -i 's/server front_green:3000/server front_blue:3000/' /home/ec2-user/nginx/nginx.conf | |
else | |
sed -i 's/server front_blue:3000/server front_green:3000/' /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 -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 }}' = 'front_blue' ]; then | |
docker-compose -f docker-compose-front.yml stop front_green | |
else | |
docker-compose -f docker-compose-front.yml stop front_blue | |
fi | |
" |