2022-08-16 20:24:58 +02:00

93 lines
3.6 KiB
YAML

name: Demo
on:
workflow_dispatch:
push:
branches:
- main
paths:
- ".github/workflows/demo**"
env:
AWS_REGION: eu-west-1
STACK_NAME: haystack-demo-production-instance
ENVIRONMENT: production
TEMPLATE_FILE: .github/workflows/demo/ec2-autoscaling-group.yaml
VPC_STACK: haystack-demo-production-vpc
INSTANCE_TYPE: g4dn.2xlarge
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: AWS Authentication
uses: aws-actions/configure-aws-credentials@67fbcbb121271f7775d2e7715933280b06314838
with:
aws-region: ${{ env.AWS_REGION }}
role-to-assume: ${{ secrets.DEMO_AWS_DEPLOY_ROLE }}
- name: Deploy demo
env:
CF_KEY_NAME: ${{ secrets.DEMO_CF_KEY_NAME }}
CF_IMAGE_ID: ${{ secrets.DEMO_CF_IMAGE_ID }}
CF_IAM_INSTANCE_PROFILE: ${{ secrets.DEMO_CF_IAM_INSTANCE_PROFILE }}
run: |
echo -e "\n* Deploying CloudFormation Stack ${STACK_NAME}"
start_ts=$(date +"%s")
# Deploy the CloudFormation stack as a background process
aws cloudformation deploy \
--template-file "${TEMPLATE_FILE}" \
--stack-name ${STACK_NAME} \
--parameter-overrides \
"Environment=${ENVIRONMENT}" \
"VPCStack=${VPC_STACK}" \
"GitRepositoryURL=${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" \
"GitBranchName=${GITHUB_REF_NAME}" \
"GitCommitHash=${{ github.sha }}" \
"InstanceType=${INSTANCE_TYPE}" \
"KeyName=${CF_KEY_NAME}" \
"ImageId=${CF_IMAGE_ID}" \
"IamInstanceProfile=${CF_IAM_INSTANCE_PROFILE}" \
--capabilities CAPABILITY_IAM > /dev/null &
# Save the pid for the background deploy process
deploy_pid=$!
echo -e "\n** Progress of deployment for CloudFormation Stack ${STACK_NAME}"
# Show stack events while the background deploy process is still running
touch stack-events.prev
while kill -0 $deploy_pid 2>/dev/null
do
sleep 2
aws cloudformation describe-stack-events --stack-name ${STACK_NAME} 2>/dev/null \
| jq -r --arg start_ts ${start_ts} '.StackEvents[] | (.Timestamp | sub("(?<x>T\\d+:\\d+:\\d+).*$"; "\(.x)Z") | fromdate) as $dt | select($dt >= ($start_ts|tonumber)) | "\($dt|todateiso8601)\t\(.LogicalResourceId)\t\(.ResourceStatus)\t\(.ResourceStatusReason // "")"' \
| column -t -s $'\t' \
| tac > stack-events.new
# describe-stack-events dumps all the events history but
# we are only interested in printing only the new ones on each iteration
tail -n $(($(wc -l < stack-events.new) - $(wc -l < stack-events.prev))) stack-events.new
mv stack-events.new stack-events.prev
done
LogGroupName=$(aws cloudformation describe-stacks \
--stack-name ${STACK_NAME} \
--query 'Stacks[0].Outputs[?OutputKey==`LogGroupName`].OutputValue' \
--output text)
LogGroupURL="https://${AWS_REGION}.console.aws.amazon.com/cloudwatch/home?region=${AWS_REGION}#logsV2:log-groups/log-group/$(echo ${LogGroupName} | sed 's#/#$252F#g')"
echo -e "\n* EC2 instance CloudWatch logs can be found at ${LogGroupURL}"
# wait will exit whith the same exit code as the background deploy process
# so we pass or fail the CI job based on the result of the deployment
wait $deploy_pid