-
Notifications
You must be signed in to change notification settings - Fork 3
85 lines (67 loc) · 3.45 KB
/
deploy_prod.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
name: Deploy to EC2 Prod
on:
push:
branches:
- add-auto-deploy
# on:
# workflow_dispatch:
jobs:
deploy:
runs-on: ubuntu-latest
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
AWS_AUTO_SCALING_GROUP_NAME: ${{ secrets.AWS_AUTO_SCALING_GROUP_NAME }}
AWS_SECURITY_GROUP_ID: ${{ secrets.AWS_SECURITY_GROUP_ID }}
AWS_EC2_SSH_KEY: ${{ secrets.AWS_EC2_SSH_KEY }}
steps:
- name: Install jq
run: sudo apt-get install -y jq
- name: Set up AWS CLI
run: |
mkdir -p ~/.aws
echo "[default]" > ~/.aws/credentials
echo "aws_access_key_id = $AWS_ACCESS_KEY_ID" >> ~/.aws/credentials
echo "aws_secret_access_key = $AWS_SECRET_ACCESS_KEY" >> ~/.aws/credentials
echo "region = $AWS_REGION" >> ~/.aws/credentials
# - name: Fetch EC2 instances from Auto Scaling Group
# run: |
# INSTANCE_IDS=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names $AWS_AUTO_SCALING_GROUP_NAME --query "AutoScalingGroups[0].Instances[*].InstanceId" --output json)
# echo "Fetched instances from Auto Scaling Group: $INSTANCE_IDS"
# # INSTANCE_ID_STRING=$(echo $INSTANCE_IDS | jq -r '. | join(" ")')
# # echo "Fetched instances from Auto Scaling Group: $INSTANCE_ID_STRING"
- name: Open SSH port to GitHub Actions IP
run: |
aws ec2 authorize-security-group-ingress --group-id $AWS_SECURITY_GROUP_ID --protocol tcp --port 22 --cidr $(curl -s https://api.ipify.org)/32
- name: Save the private key to a file
run: |
echo "$AWS_EC2_SSH_KEY" > ~/.aws/key.pem
chmod 400 ~/.aws/key.pem # Set proper permissions
- name: Fetch EC2 instances from Auto Scaling Group
run: |
INSTANCE_IDS=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names $AWS_AUTO_SCALING_GROUP_NAME --query "AutoScalingGroups[0].Instances[*].InstanceId" --output json)
- name: Extract the instance IDs using jq (JSON processor)
run: |
set -x
IFS=$'\n' read -d '' -r -a INSTANCES <<< "$(echo "$INSTANCE_IDS" | jq -r '.[]')"
set +x
- name: Iterate over the array
run: |
for INSTANCE_ID in "${INSTANCES[@]}"; do
echo "Processing instance: $INSTANCE_ID"
# Get instance IP
INSTANCE_IP=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query "Reservations[0].Instances[0].PublicIpAddress" --output json)
# Remove double quotes from INSTANCE_IP
INSTANCE_IP="${INSTANCE_IP//\"}"
echo "Instance IP: $INSTANCE_IP"
echo "ssh -i ~/.aws/key.pem -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ubuntu@$INSTANCE_IP 'cd /home/ubuntu && ./update_mygeneset pull_src'"
ssh -i ~/.aws/key.pem -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ubuntu@$INSTANCE_IP 'cd /home/ubuntu && ./update_mygeneset pull_src'
done
- name: Close SSH port to GitHub Actions IP (even on failure)
run: |
aws ec2 revoke-security-group-ingress --group-id $AWS_SECURITY_GROUP_ID --protocol tcp --port 22 --cidr $(curl -s https://api.ipify.org)/32
if: always()
- name: Setup tmate debug session on failure
if: ${{ failure() }}
uses: mxschmitt/action-tmate@v3