Skip to content

Instantly share code, notes, and snippets.

@Bogyie
Created March 13, 2023 12:25
Show Gist options
  • Save Bogyie/e392e8a8e9b63755e76393e6f44115b8 to your computer and use it in GitHub Desktop.
Save Bogyie/e392e8a8e9b63755e76393e6f44115b8 to your computer and use it in GitHub Desktop.
Blue-Green Deploy to AWS EC2
name: Blue-Green Deploy to AWS EC2
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
env:
CURRENT_COLOR: blue
NEW_COLOR: green
steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: 11
- name: Grant execute permission to Gradlew
run: chmod +x ./gradlew
- name: Build project
run: ./gradlew clean build
- name: Copy JAR file to EC2 instance
uses: appleboy/scp-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_KEY }}
port: ${{ secrets.EC2_PORT }}
source: ./build/libs/my-project.jar
target: /home/ec2-user/my-project-${{ env.NEW_COLOR }}.jar
- name: SSH into EC2 instance and start new application
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_KEY }}
port: ${{ secrets.EC2_PORT }}
script: |
cd /home/ec2-user/
sudo systemctl stop my-project-${{ env.CURRENT_COLOR }}
sudo systemctl start my-project-${{ env.NEW_COLOR }}
- name: Wait for new application to start
run: sleep 5
- name: Update load balancer with new application
uses: aws-actions/configure-aws-cli@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
env:
ELB_NAME: my-project-load-balancer
run: |
aws elbv2 describe-target-groups --names my-project-target-group-${{ env.CURRENT_COLOR }} --query "TargetGroups[0].TargetGroupArn" > current_target_group_arn.txt
aws elbv2 describe-target-groups --names my-project-target-group-${{ env.NEW_COLOR }} --query "TargetGroups[0].TargetGroupArn" > new_target_group_arn.txt
aws elbv2 modify-listener --listener-arn ${{ secrets.ELB_LISTENER_ARN }} --default-actions "Type=forward,TargetGroupArn=`cat new_target_group_arn.txt`"
aws elbv2 wait target-in-service --target-group-arn `cat new_target_group_arn.txt` --targets Id=`aws ec2 describe-instances --filters "Name=tag:Name,Values=my-project-${{ env.NEW_COLOR }}-instance" --query "Reservations[].Instances[].InstanceId" --output text`
- name: SSH into EC2 instance and stop old application
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_KEY }}
port: ${{ secrets.EC2_PORT }}
script: |
cd /home/ec2-user/
sudo systemctl stop my-project-${{ env.CURRENT_COLOR }}
- name: Delete old JAR file
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_KEY }}
port: ${{ secrets.EC2_PORT }}
script: |
cd /home/ec2-user/
rm my-project-${{ env.CURRENT_COLOR }}.jar
- name: Update environment variables
run: |
echo "CURRENT_COLOR=${{ env.NEW_COLOR }}" >> $GITHUB_ENV
echo "NEW_COLOR=$(if [[ ${{ env.NEW_COLOR }} == "blue" ]]; then echo "green"; else echo "blue"; fi)" >> $GITHUB_ENV
- name: Cleanup
run: |
rm current_target_group_arn.txt new_target_group_arn.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment