-
Notifications
You must be signed in to change notification settings - Fork 14
119 lines (104 loc) · 4.96 KB
/
on-demand-env.yaml
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
name: Deploy Development Environment
on:
workflow_dispatch:
inputs:
prNumber:
description: 'Pull Request Number'
required: false
branch:
description: 'Git Branch'
required: false
commitHash:
description: 'Git Commit Hash'
required: false
envExpiryTimeMinutes:
description: 'Minutes until environment is removed ( max 180 )'
required: false
default: '60'
discordUserId:
description: 'Discord ID to get tagged in the notification channel'
required: false
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_QA }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_QA }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Prepare EC2 user data script with environment variables
run: |
PR_NUMBER=${{ github.event.inputs.prNumber }}
BRANCH=${{ github.event.inputs.branch }}
COMMIT_HASH=${{ github.event.inputs.commitHash }}
DISCORD_WEBHOOK=${{ secrets.DISCORD_WEBHOOK }}
DISCORD_USER_ID=${{ github.event.inputs.discordUserId }}
ARCHIVE_RPC=${{ secrets.ON_DEMAND_ENV_ARCHIVE_RPC }}
FORKED_NETWORK_ADDRESS=${{ secrets.ON_DEMAND_ENV_FORKED_NETWORK_ADDRESS }}
REPUTATION_URL=${{ secrets.ON_DEMAND_ENV_REPUTATION_URL }}
NETWORK_ID=${{ secrets.ON_DEMAND_ENV_NETWORK_ID }}
ENV_NAME=${{ vars.ON_DEMAND_ENV_DYNAMODB_ENV_NAME }}
# Make a copy of the original script
cp ./.github/workflows/scripts/on-demand-env-script.sh ./temp-on-demand-env-script.sh
# Insert env vars after the shebang line
sed -i "2iexport PR_NUMBER=$PR_NUMBER" ./temp-on-demand-env-script.sh
sed -i "3iexport BRANCH=$BRANCH" ./temp-on-demand-env-script.sh
sed -i "4iexport COMMIT_HASH=$COMMIT_HASH" ./temp-on-demand-env-script.sh
sed -i "5iexport DISCORD_WEBHOOK=$DISCORD_WEBHOOK" ./temp-on-demand-env-script.sh
sed -i "6iexport DISCORD_USER_ID=$DISCORD_USER_ID" ./temp-on-demand-env-script.sh
sed -i "7iexport ARCHIVE_RPC=$ARCHIVE_RPC" ./temp-on-demand-env-script.sh
sed -i "8iexport FORKED_NETWORK_ADDRESS=$FORKED_NETWORK_ADDRESS" ./temp-on-demand-env-script.sh
sed -i "9iexport REPUTATION_URL=$REPUTATION_URL" ./temp-on-demand-env-script.sh
sed -i "10iexport NETWORK_ID=$NETWORK_ID" ./temp-on-demand-env-script.sh
sed -i "11iexport ENV_NAME=$ENV_NAME" ./temp-on-demand-env-script.sh
- name: Provision EC2 instance
run: |
INSTANCE_PROFILE=${{ vars.ON_DEMAND_ENV_EC2_INSTANCE_PROFILE_NAME }}
SECURITY_GROUP=${{ vars.ON_DEMAND_ENV_EC2_SG_ID }}
GITHUB_RUN_NUMBER=${{ github.run_number }}
ENV_DURATION=${{ github.event.inputs.envExpiryTimeMinutes }}
LAUNCH_SPECIFICATION=$(cat <<EOF
{
"ImageId": "ami-0eb260c4d5475b901",
"InstanceType": "t3.2xlarge",
"IamInstanceProfile": {
"Name": "${INSTANCE_PROFILE}"
},
"SecurityGroupIds": ["${SECURITY_GROUP}"],
"UserData": "$(base64 --wrap=0 ./temp-on-demand-env-script.sh)",
"BlockDeviceMappings": [
{
"DeviceName": "/dev/sda1",
"Ebs": {
"VolumeSize": 80
}
}
]
}
EOF
)
SPOT_REQUEST_ID=$(aws ec2 request-spot-instances \
--spot-price "0.33" \
--instance-count 1 \
--type "one-time" \
--launch-specification "${LAUNCH_SPECIFICATION}" \
--query 'SpotInstanceRequests[0].SpotInstanceRequestId' --output text)
INSTANCE_ID=""
while [ -z $INSTANCE_ID ]; do
sleep 10 # Wait for a short period to ensure AWS has processed the request
INSTANCE_ID=$(aws ec2 describe-spot-instance-requests \
--spot-instance-request-ids $SPOT_REQUEST_ID \
--query 'SpotInstanceRequests[0].InstanceId' --output text)
done
echo "INSTANCE_ID=$INSTANCE_ID" >> $GITHUB_ENV
aws ec2 create-tags \
--resources $INSTANCE_ID \
--tags Key=Name,Value=dev-env-github-run-number-${GITHUB_RUN_NUMBER}-minutes-${ENV_DURATION}
- name: Fetch instance public IP
run: |
INSTANCE_IP=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query 'Reservations[0].Instances[0].PublicIpAddress' --output text)
echo "INSTANCE_IP=$INSTANCE_IP" >> $GITHUB_ENV