Deploy Development Environment #103
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |