Skip to content

Commit

Permalink
new idea part #9
Browse files Browse the repository at this point in the history
  • Loading branch information
parhamrahmani committed May 28, 2024
1 parent 34fa7f6 commit f3d0883
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions .github/workflows/deploy-to-ec2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,29 @@ jobs:
docker rm my-container || true
docker run -d --name my-container -p 80:5000 ${{ secrets.DOCKER_HUB_USERNAME }}/flask-app-image-repository:latest
EOF
- name: Define shell script function
run: |
echo 'update_instance() {
local ip=$1
ssh -o StrictHostKeyChecking=no -i /home/runner/.ssh/id_rsa ubuntu@$ip <<EOF
docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/flask-app-image-repository:latest
docker stop my-container || true
docker rm my-container || true
docker run -d --name my-container -p 80:5000 ${{ secrets.DOCKER_HUB_USERNAME }}/flask-app-image-repository:latest
EOF
echo "Updated instance with IP: $ip"
}' > $BASH_ENV
source $BASH_ENV
- name: Retrieve Autoscaling EC2 server IPs
id: get_autoscaling_instances_ips
run: |
aws autoscaling describe-auto-scaling-instances --query "AutoScalingInstances[*].InstanceId" --output text > autoscaling_instance_ids.txt
aws ec2 describe-instances --instance-ids $(cat autoscaling_instance_ids.txt) --query "Reservations[*].Instances[*].PublicIpAddress" --output text > autoscaling_instance_ips.txt
cat autoscaling_instance_ips.txt
ips_file="autoscaling_instance_ips.txt"
mapfile -t ips < "$ips_file"
# Loop through the IP addresses and run the update script
for ip in "${ips[@]}"; do
update_instance $ip
touch update_instance_$ip.sh
echo '#!/bin/bash' > "update_instance_$ip.sh"
echo '' >> update_instance_$ip.sh
Expand All @@ -106,27 +117,9 @@ jobs:
echo "shell script created for $ip"
echo "Running shell script for $ip"
chmod +x "update_instance_$ip.sh"
cat "update_instance_$ip.sh"
./update_instance_$ip.sh
done
- name: Update Autoscaling EC2 with new Docker image
run: |
cat autoscaling_instance_ips.txt
ips_file="autoscaling_instance_ips.txt"
IFS=$'\n' read -d '' -r -a ips < "$ips_file"
for i in "${!ips[@]}"; do
ssh -o StrictHostKeyChecking=no -i /home/runner/.ssh/id_rsa ubuntu@${ips[i]} << 'EOF'
docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/flask-app-image-repository:latest
docker stop my-container || true
docker rm my-container || true
docker run -d --name my-container -p 80:5000 ${{ secrets.DOCKER_HUB_USERNAME }}/flask-app-image-repository:latest
EOF
done
env:
AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
AWS_REGION: ${{ secrets.AWS_REGION }}
Expand Down

0 comments on commit f3d0883

Please sign in to comment.