Skip to content

Commit

Permalink
Update buildami.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
ankithreddypati committed Dec 4, 2023
1 parent a715756 commit cb8a0fa
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions .github/workflows/buildami.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: Build AMI and Update Infrastructure
name: Build AMI

on:
push:
branches:
- main

jobs:
build-and-update:
build:
runs-on: ubuntu-latest

services:
Expand Down Expand Up @@ -60,41 +60,46 @@ jobs:
-var "aws_secret_access_key=${{ secrets.AWS_SECRET_ACCESS_KEY }}" \
-var "aws_access_key=${{ secrets.AWS_ACCESS_KEY }}" \
debian12-ami.pkr.hcl | sudo tee output.txt
tail -2 output.txt | head -2 | awk 'match($0, /ami-.*/) { print substr($0, RSTART, RLENGTH) }' > ami_id.txt
tail -2 output.txt | head -2 | awk 'match($0, /ami-.*/) { print substr($0, RSTART, RLENGTH) }' >> ami.txt
- name: Fetch Launch Template Name and Update the autoscaling group
- name: Create new launch template version
env:
AWS_ACCESS_KEY_ID: ${{ secrets.DEMO_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.DEMO_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ${{ secrets.AWS_REGION }}
run: |
AMI_ID=$(cat ami_id.txt)
echo "AMI ID: $AMI_ID"
LAUNCH_TEMPLATE_NAME=$(aws ec2 describe-launch-templates \
--query "LaunchTemplates[?starts_with(LaunchTemplateName, 'launchTemplate')].LaunchTemplateName | [0]" \
--output text)
if [ -z "$LAUNCH_TEMPLATE_NAME" ]; then
echo "Launch template starting with 'launchTemplate' not found."
exit 1
fi
echo "Launch Template Name: $LAUNCH_TEMPLATE_NAME"
AMI_ID=$(cat ami.txt)
LAUNCH_TEMPLATE_NAME=$(aws ec2 describe-launch-templates --query 'LaunchTemplates[?starts_with(LaunchTemplateName, `launchTemplate`) == `true`].LaunchTemplateName' --output text)
aws ec2 create-launch-template-version --launch-template-name $LAUNCH_TEMPLATE_NAME --version-description latest --source-version 1 --launch-template-data "ImageId=$AMI_ID"
AUTO_SCALING_GROUP_NAME=$(aws autoscaling describe-auto-scaling-groups \
--query "AutoScalingGroups[?starts_with(AutoScalingGroupName, 'autoScalingGroup-')].AutoScalingGroupName" \
--output text | head -n 1)
- name: Update the autoscaling group
id: set_asg_name
env:
AWS_ACCESS_KEY_ID: ${{ secrets.DEMO_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.DEMO_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ${{ secrets.AWS_REGION }}
run: |
AUTO_SCALING_GROUP_NAME=$(aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[?starts_with(AutoScalingGroupName, 'autoScalingGroup')].AutoScalingGroupName" --output text | head -n 1)
if [ -z "$AUTO_SCALING_GROUP_NAME" ]; then
echo "No Auto Scaling Group found with the specified prefix."
exit 1
fi
echo "Auto Scaling Group Name: $AUTO_SCALING_GROUP_NAME"
echo "AUTO_SCALING_GROUP_NAME=$AUTO_SCALING_GROUP_NAME" >> $GITHUB_ENV
aws autoscaling update-auto-scaling-group --auto-scaling-group-name $AUTO_SCALING_GROUP_NAME --launch-template LaunchTemplateName=$LAUNCH_TEMPLATE_NAME ,Version='$Latest'
aws autoscaling update-auto-scaling-group \
--auto-scaling-group-name $AUTO_SCALING_GROUP_NAME \
--launch-template "LaunchTemplateName=$LAUNCH_TEMPLATE_NAME,Version=Latest"
REFRESH_ID=$(aws autoscaling start-instance-refresh --auto-scaling-group-name $AUTO_SCALING_GROUP_NAME --query 'InstanceRefreshId' --output text)
- name: Instance refresh and wait
env:
AWS_ACCESS_KEY_ID: ${{ secrets.DEMO_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.DEMO_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ${{ secrets.AWS_REGION }}
run: |
REFRESH_ID=$(aws autoscaling start-instance-refresh --auto-scaling-group-name "$AUTO_SCALING_GROUP_NAME" --query 'InstanceRefreshId' --output text)
STATUS="pending"
while [ "$STATUS" != "Successful" ]; do
echo "Waiting for instance refresh to complete..."
sleep 60
STATUS=$(aws autoscaling describe-instance-refreshes --auto-scaling-group-name $AUTO_SCALING_GROUP_NAME --instance-refresh-ids $REFRESH_ID --query 'InstanceRefreshes[0].Status' --output text)
STATUS=$(aws autoscaling describe-instance-refreshes --auto-scaling-group-name "$AUTO_SCALING_GROUP_NAME" --instance-refresh-ids $REFRESH_ID --query 'InstanceRefreshes[0].Status' --output text)
if [ "$STATUS" == "Failed" ]; then
echo "Instance refresh failed."
exit 1
Expand Down

0 comments on commit cb8a0fa

Please sign in to comment.