Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: fix and update CI/CD pipelines for packer #24

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 74 additions & 65 deletions .github/workflows/packer-build.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
name: Instance Refresh

env:
ARTIFACT_NAME: webapp
AWS_REGION: us-east-1
ENVIRONMENT: test

on:
workflow_dispatch:
# push:
# branches: [master]
push:
branches: [master]
jobs:
tests:
runs-on: ubuntu-latest

environment: ${{ vars.ENVIRONMENT }}
name: Run unit tests
steps:
- uses: actions/checkout@v4
- name: "Create .env file"
run: |
touch .env
echo ENVIRONMENT=${{ env.ENVIRONMENT }} >> .env
echo HOSTNAME=${{ secrets.HOSTNAME}} >> .env
echo PORT=${{ secrets.PORT }} >> .env
echo ENVIRONMENT=${{ vars.ENVIRONMENT }} >> .env
echo HOSTNAME=${{ vars.HOSTNAME}} >> .env
echo PORT=${{ vars.PORT }} >> .env
echo DATABASE=${{ vars.DATABASE }} >> .env
echo DBUSER=${{ vars.DBUSER }} >> .env
echo DBPASSWORD=${{ secrets.DBPASSWORD }} >> .env
Expand All @@ -34,6 +29,7 @@ jobs:
packer:
needs: tests
runs-on: ubuntu-latest
environment: ${{ vars.ENVIRONMENT }}
name: Build AWS Custom AMI with REST API app artifacts
steps:
- name: Checkout code from branch
Expand All @@ -42,21 +38,21 @@ jobs:
- name: Build artifacts
run: |
touch .env
echo ENVIRONMENT=${{ env.ENVIRONMENT }} >> .env
echo HOSTNAME=${{ secrets.HOSTNAME}} >> .env
echo PORT=${{ secrets.PORT }} >> .env
echo ENVIRONMENT=${{ vars.ENVIRONMENT }} >> .env
echo HOSTNAME=${{ vars.HOSTNAME}} >> .env
echo PORT=${{ vars.PORT }} >> .env
echo DATABASE=${{ vars.DATABASE }} >> .env
echo DBUSER=${{ vars.DBUSER }} >> .env
echo DBPASSWORD=${{ secrets.DBPASSWORD }} >> .env
zip -r webapp.zip ./
- name: Packer variables
run: |
touch aws/packer/ami.pkrvars.hcl
cd aws/packer && touch ami.pkrvars.hcl
echo ami_prefix=\"${{ vars.AMI_PREFIX }}\" >> ami.pkrvars.hcl
echo OS=\"${{ vars.OS }}\" >> ami.pkrvars.hcl
echo ubuntu_version=\"${{ vars.VERSION }}\" >> ami.pkrvars.hcl
echo ssh_username=\"${{ secrets.SSH }}\" >> ami.pkrvars.hcl
echo ssh_username=\"${{ vars.SSH_USERNAME }}\" >> ami.pkrvars.hcl
echo subnet_id=\"${{ secrets.SUBNET_ID }}\" >> ami.pkrvars.hcl
echo dev_id=\"${{ secrets.DEV_ID }}\" >> ami.pkrvars.hcl
echo prod_id=\"${{ secrets.PROD_ID }}\" >> ami.pkrvars.hcl
Expand All @@ -80,53 +76,66 @@ jobs:
with:
version: "latest"

- name: Packer init, format validate and build custom AMI
run: |
cd aws/packer &&
packer fmt .
packer init ami.pkr.hcl
packer validate -evaluate-datasources --var-file=ami.pkrvars.hcl ami.pkr.hcl
packer build --var-file=ami.pkrvars.hcl ami.pkr.hcl
- name: Run `packer fmt`
id: fmt
run: "packer fmt ."
- name: Run `packer init`
id: init
run: "packer init ami.pkr.hcl"
- name: Run `packer validate`
id: validate
run: "packer validate -evaluate-datasources --var-file=ami.pkrvars.hcl ami.pkr.hcl"
- name: Run `packer build`
id: build
run: "packer build --var-file=ami.pkrvars.hcl ami.pkr.hcl"

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_PROD_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_PROD_SECRET_KEY }}
aws-region: ${{ vars.AWS_REGION }}
- name: Instance Refresh automation
run: |
sudo apt-get install jq
ASG_NAME=${{ secrets.ASG_NAME }}
LAUNCH_CONFIG=${{ secrets.LAUNCH_CONFIG }}
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
# - name: Packer init, format validate and build custom AMI
# run: |
# packer fmt .
# packer init ami.pkr.hcl
# packer validate -evaluate-datasources --var-file=ami.pkrvars.hcl ami.pkr.hcl
# packer build --var-file=ami.pkrvars.hcl ami.pkr.hcl

# - name: Configure AWS Credentials
# uses: aws-actions/configure-aws-credentials@v4
# with:
# aws-access-key-id: ${{ secrets.AWS_PROD_ACCESS_KEY }}
# aws-secret-access-key: ${{ secrets.AWS_PROD_SECRET_KEY }}
# aws-region: ${{ vars.AWS_REGION }}

# - name: Instance Refresh automation
# run: |
# sudo apt-get install jq
# ASG_NAME=${{ secrets.ASG_NAME }}
# LAUNCH_CONFIG=${{ secrets.LAUNCH_CONFIG }}
# 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
5 changes: 4 additions & 1 deletion .github/workflows/packer-validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
jobs:
validate:
runs-on: ubuntu-latest
environment: ${{ vars.ENVIRONMENT }}
name: Validate packer template for the custom AMI
steps:
- name: Checkout code from branch
Expand All @@ -25,7 +26,7 @@ jobs:
echo ami_prefix=\"${{ vars.AMI_PREFIX }}\" >> ami.pkrvars.hcl
echo OS=\"${{ vars.OS }}\" >> ami.pkrvars.hcl
echo ubuntu_version=\"${{ vars.VERSION }}\" >> ami.pkrvars.hcl
echo ssh_username=\"${{ secrets.SSH }}\" >> ami.pkrvars.hcl
echo ssh_username=\"${{ vars.SSH_USERNAME }}\" >> ami.pkrvars.hcl
echo subnet_id=\"${{ secrets.SUBNET_ID }}\" >> ami.pkrvars.hcl
echo dev_id=\"${{ secrets.DEV_ID }}\" >> ami.pkrvars.hcl
echo prod_id=\"${{ secrets.PROD_ID }}\" >> ami.pkrvars.hcl
Expand All @@ -49,3 +50,5 @@ jobs:
packer fmt .
packer init ami.pkr.hcl
packer validate -syntax-only --var-file=ami.pkrvars.hcl ami.pkr.hcl
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ jobs:
- run: npm ci
- run: npx semantic-release
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
11 changes: 4 additions & 7 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
name: Unit Tests

env:
ENVIRONMENT: test

on:
workflow_dispatch:
pull_request:
Expand Down Expand Up @@ -38,9 +35,9 @@ jobs:
- name: "Create .env file"
run: |
touch .env
echo ENVIRONMENT=${{ env.ENVIRONMENT }} >> .env
echo HOSTNAME=${{ secrets.HOSTNAME}} >> .env
echo PORT=${{ secrets.PORT }} >> .env
echo ENVIRONMENT=${{ vars.ENVIRONMENT }} >> .env
echo HOSTNAME=${{ vars.HOSTNAME}} >> .env
echo PORT=${{ vars.PORT }} >> .env
echo DATABASE=${{ vars.DATABASE }} >> .env
echo DBUSER=${{ vars.DBUSER }} >> .env
echo DBPASSWORD=${{ secrets.DBPASSWORD }} >> .env
Expand All @@ -52,4 +49,4 @@ jobs:
env:
POSTGRES_HOST: localhost
POSTGRES_PORT: 5432
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}