Skip to content

💡 Updating scripts & Adding Documentation #5

💡 Updating scripts & Adding Documentation

💡 Updating scripts & Adding Documentation #5

Workflow file for this run

name: Build AMI and Deploy
on:
push:
branches:
- main
jobs:
build-ami:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install dependencies
run: npm install
- name: Display .env file
run: |
echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env
echo "DB_USER=${{ secrets.DB_USER }}" >> .env
echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env
echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env
echo "PORT=${{ secrets.PORT }}" >> .env
echo "CSVPATH='/home/admin/webapp/users.csv'" >> .env
echo "region=${{secrets.region}}">>.env
echo "topicarn=${{secrets.topicarn}}">>.env
cat .env
- name: Configure MySQL
run: |
sudo apt-get update
sudo apt-get install -y curl
sudo systemctl start mysql
sudo systemctl status mysql
mysql -u root -p"${{ secrets.MY_SQL_PASSWORD }}" -e "CREATE DATABASE Assignment3;"
- name: Run Tests
run: npm test
- name: Create Zip Archive
run: |
zip -r webapp.zip ./
- name: Set up Packer
run: |
wget https://releases.hashicorp.com/packer/1.7.3/packer_1.7.3_linux_amd64.zip
unzip packer_1.7.3_linux_amd64.zip
chmod +x packer
sudo mv packer /usr/local/bin/
shell: bash
- name: Initialize Packer
run: packer init aws-ubuntu.pkr.hcl
- name: Build AMI in dev account
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
id: build
run: |
packer build aws-ubuntu.pkr.hcl
AMI_ID=$(packer build -machine-readable aws-ubuntu.pkr.hcl | awk -F, '$0 ~/artifact,0,id/ {print $6}')
echo "AMI_ID is =$AMI_ID" >> $GITHUB_ENV
echo "::set-output name=ami_id::$AMI_ID"
- name: Configure AWS CLI for Demo Account
run: |
aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID_DEMO }}
aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY_DEMO }}
aws configure set region ${{ secrets.AWS_REGION_DEMO}}
- name: Instance Refresh automation / Continuous Delivery
run: |
sudo apt-get install jq
ASG_NAME=webAppAutoScalingGroup
LAUNCH_CONFIG=webAppLaunchTemplate
IMAGE=$(jq -r '.builds[-1].artifact_id' manifest.json | cut -d':' -f2)
IMAGE_ID=$(jq -n \
--arg img "$IMAGE" \
'{"ImageId": $img}')
aws ec2 create-launch-template-version \
--launch-template-name "$LAUNCH_CONFIG" \
--version-description updateAMI --source-version 1 \
--launch-template-data="$IMAGE_ID"
TEMPLATE_VERSION=$(aws ec2 describe-launch-template-versions --launch-template-name="$LAUNCH_CONFIG" \
--filters="Name=is-default-version,Values=false" \
--query 'sort_by(LaunchTemplateVersions, &CreateTime)[-1].[VersionNumber]' --output text)
aws autoscaling update-auto-scaling-group --auto-scaling-group-name $ASG_NAME \
--launch-template LaunchTemplateName="$LAUNCH_CONFIG",Version=$TEMPLATE_VERSION
aws autoscaling start-instance-refresh \
--auto-scaling-group-name $ASG_NAME --strategy Rolling
printf "Instance refresh in progress"
while true;
do
instance_refresh_status=$(aws autoscaling describe-instance-refreshes --auto-scaling-group-name $ASG_NAME \
--query "InstanceRefreshes[0].Status" --output text)
if [ "$instance_refresh_status" = "Successful" ]; then
printf "\nInstance refresh successful!"
break
fi
if [ "$instance_refresh_status" = "Cancelling" ] || [ "$instance_refresh_status" = "Cancelled" ] || [ "$instance_refresh_status" = "Failed" ]; then
printf "\nInstance refresh failed!"
exit 1
fi
echo -ne "."
sleep 10
done