From 8574258bb162bf6f0f9fda1ee78854c229b00225 Mon Sep 17 00:00:00 2001 From: Siddharth Rawat Date: Fri, 12 Jan 2024 12:16:27 -0500 Subject: [PATCH] ci: fix and update CI/CD pipelines for packer --- .github/workflows/packer-build.yml | 139 ++++++++++++++------------ .github/workflows/packer-validate.yml | 5 +- .github/workflows/release.yml | 2 +- .github/workflows/unit-test.yml | 11 +- 4 files changed, 83 insertions(+), 74 deletions(-) diff --git a/.github/workflows/packer-build.yml b/.github/workflows/packer-build.yml index 53193c6..617a176 100644 --- a/.github/workflows/packer-build.yml +++ b/.github/workflows/packer-build.yml @@ -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 @@ -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 @@ -42,9 +38,9 @@ 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 @@ -52,11 +48,11 @@ jobs: - 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 @@ -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 diff --git a/.github/workflows/packer-validate.yml b/.github/workflows/packer-validate.yml index 192c0f2..016e76c 100644 --- a/.github/workflows/packer-validate.yml +++ b/.github/workflows/packer-validate.yml @@ -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 @@ -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 @@ -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 }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index f8be31b..3fede76 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -20,4 +20,4 @@ jobs: - run: npm ci - run: npx semantic-release env: - GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index b540fc6..9760148 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -1,8 +1,5 @@ name: Unit Tests -env: - ENVIRONMENT: test - on: workflow_dispatch: pull_request: @@ -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 @@ -52,4 +49,4 @@ jobs: env: POSTGRES_HOST: localhost POSTGRES_PORT: 5432 - GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}