-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.yml
More file actions
134 lines (109 loc) · 4.62 KB
/
deploy.yml
File metadata and controls
134 lines (109 loc) · 4.62 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
name: Deploy LDAP Node.js Server
on:
pull_request:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
environment: dev
steps:
- name: Checkout the repository
uses: actions/checkout@v2
- name: Set up Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.3.0
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Fetch AMI ID for Amazon Linux
id: fetch_ami
run: |
os_filter="al2023-ami-*-x86_64"
owners="137112412989" # Amazon Linux owner ID
ami_id=$(aws ec2 describe-images \
--filters "Name=name,Values=$os_filter" "Name=state,Values=available" \
--owners $owners \
--query "Images | sort_by(@, &CreationDate)[-1].ImageId" \
--output text)
if [ -z "$ami_id" ]; then
echo "Failed to find valid AMI ID"
exit 1
fi
echo "ami_id=$ami_id" >> "$GITHUB_ENV"
- name: Initialize Terraform
run: cd terraform && terraform init
- name: Apply Terraform Configuration
run: |
cd terraform
terraform apply -auto-approve -var="ami_id=${{ env.ami_id }}"
- name: Fetch EC2 Instance Public IP
id: get-ec2-ip
run: |
# Get the most recent instance with the LDAPServer tag
IP=$(aws ec2 describe-instances \
--filters "Name=tag:Name,Values=LDAPServer" "Name=instance-state-name,Values=running" \
--query "Reservations[*].Instances[*].[LaunchTime,PublicIpAddress]" \
--output text | sort -r | head -n 1 | awk '{print $2}')
# Check if we got a valid IP
if [[ -z "$IP" ]]; then
echo "Error: Could not find running instance with tag LDAPServer"
exit 1
fi
# Set the environment variable correctly
echo "EC2_IP=${IP}" >> $GITHUB_ENV
echo "Found IP: ${IP}"
- name: Wait for EC2 to be ready
run: sleep 55 # Ensure system is ready
- name: Create .env file locally
run: |
cat > .env << EOF
DB_TYPE=${{ vars.DB_TYPE }}
MONGO_URI=${{ secrets.MONGO_URI }}
MONGO_DATABASE=${{ vars.MONGO_DATABASE }}
LDAP_BASE_DN=${{ vars.LDAP_BASE_DN }}
LDAP_PORT=${{ vars.LDAP_PORT }}
LDAP_CERT_CONTENT="${{ secrets.LDAP_CERT_CONTENT }}"
LDAP_KEY_CONTENT="${{ secrets.LDAP_KEY_CONTENT }}"
LDAP_URL=${{ secrets.LDAP_URL }}
EOF
echo "Created .env file"
- name: Deploy app to EC2
run: |
echo "${{ secrets.EC2_PRIVATE_KEY }}" > ec2-key.pem
chmod 600 ec2-key.pem
# First, clone the repository and set up the directory
ssh -o StrictHostKeyChecking=no -i ec2-key.pem ec2-user@${{ env.EC2_IP }} << 'EOSSH'
# Clone the repository
if [ -d "/home/ec2-user/LDAPServer" ]; then
rm -rf /home/ec2-user/LDAPServer
fi
git clone https://github.com/anishapant21/LDAPServer.git /home/ec2-user/LDAPServer
EOSSH
# Now copy the .env file to the server - place it in the src directory
scp -o StrictHostKeyChecking=no -i ec2-key.pem .env ec2-user@${{ env.EC2_IP }}:/home/ec2-user/LDAPServer/src/.env
# Finally, install dependencies and start the server
ssh -o StrictHostKeyChecking=no -i ec2-key.pem ec2-user@${{ env.EC2_IP }} << 'EOSSH'
# Install dependencies and start the Node.js server
cd /home/ec2-user/LDAPServer/src
npm install
# Kill any existing node process
pkill -f "node server.js" || true
# Start the server (already in the src directory)
nohup sudo node server.js > server.log 2>&1 &
# Verify the server has started
sleep 5
if pgrep -f "node server.js" > /dev/null; then
echo "Server started successfully"
ps aux | grep "node server.js" | grep -v grep
else
echo "Server failed to start"
tail -n 20 server.log
fi
EOSSH
- name: Output EC2 Instance Public IP
run: echo "LDAP Server running at http://${{ env.EC2_IP }}"