added template nam5 #9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Packer CI | |
env: | |
DBHOST: ${{ secrets.DBHOST }} | |
DBUSER: ${{ secrets.DBUSER }} | |
DBPASSWORD: ${{ secrets.DBPASSWORD }} | |
DBPORT: ${{ secrets.DBPORT }} | |
DBNAME: ${{ secrets.DBNAME }} | |
on: | |
push: | |
branches: [main] | |
jobs: | |
validate-packer-build: | |
name: Validate Packer Template and Run Tests | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name : Install MySQL | |
run: | | |
sudo apt-get update | |
sudo systemctl start mysql.service | |
sudo mysql --user=${{ secrets.DBUSER }} --password=${{ secrets.DBPASSWORD }} -e "CREATE DATABASE IF NOT EXISTS ${DBNAME};" | |
- name: Checkout Repository | |
uses: actions/checkout@v2 | |
- name: Set up Node.js | |
uses: actions/setup-node@v2 | |
with: | |
node-version: "14" | |
- name: Creating log folder and file | |
run: | | |
sudo mkdir -p /var/log/webapp | |
sudo touch /var/log/webapp/logger.log | |
- name: Install All Dependencies | |
run: npm install | |
- name: Running Integration Test | |
run: npm test | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Zip application | |
run: zip -r webapp.zip . | |
- name: Authenticate with Google Cloud | |
id: auth | |
uses: google-github-actions/auth@v2 | |
with: | |
credentials_json: ${{ secrets.AMRESH_DEVKEY }} | |
- name: Set up Cloud SDK | |
uses: google-github-actions/setup-gcloud@v2 | |
- name: Use gcloud CLI | |
run: gcloud info | |
- name: Install Packer | |
run: packer init ./packer/packer.pkr.hcl | |
- name: FMT Packer | |
run: packer fmt ./packer/packer.pkr.hcl | |
- name: Run Packer validate | |
run: packer validate ./packer/packer.pkr.hcl | |
- name: Run Packer Image Build | |
run: | | |
packer build -machine-readable ./packer/packer.pkr.hcl | tee output.txt || exit 0 | |
IMAGE_NAME=$(grep 'artifact,0,id' output.txt | cut -d, -f6) | |
echo "::set-output name=image_name::${IMAGE_NAME}" | |
- name: Create Instance Template | |
run: | | |
gcloud compute instance-templates create ${{ secrets.GCP_TEMPLATE_NAME}} \ | |
--project=${{ secrets.GCP_PROJECT_ID }} \ | |
--machine-type=n1-standard-1 \ | |
--network-interface=subnet=webapp,no-address \ | |
--instance-template-region=${{ secrets.GCP_REGION }} \ | |
--metadata=startup-script="#!/bin/bash | |
if [ ! -f \"/opt/webapp/.env\" ]; then | |
touch /opt/webapp/.env | |
fi | |
echo 'DBHOST=${{ secrets.GCP_DB_HOST }}' > /opt/webapp/.env | |
echo 'DBUSER=${{ secrets.GCP_DB_USER }}' >> /opt/webapp/.env | |
echo 'DBPASSWORD=${{ secrets.GCP_DB_PASSWORD }}' >> /opt/webapp/.env | |
echo 'DBNAME=${{ secrets.GCP_DB_NAME }}' >> /opt/webapp/.env | |
" \ | |
--maintenance-policy=MIGRATE \ | |
--provisioning-model=STANDARD \ | |
--service-account=webapp@${{ secrets.GCP_PROJECT_ID }}.iam.gserviceaccount.com \ | |
--scopes=https://www.googleapis.com/auth/cloud-platform \ | |
--region=${{ secrets.GCP_REGION }} \ | |
--tags=load-balanced-backend,allow-health-check,http-server \ | |
--create-disk=auto-delete=yes,boot=yes,device-name=persistent-disk-0,image=projects/${{ secrets.GCP_PROJECT_ID }}/global/images/custom-image-45,kms-key=projects/${{ secrets.GCP_PROJECT_ID }}/locations/${{ secrets.GCP_REGION }}/keyRings/${{ secrets.GCP_KMS_KEY_RING }}/cryptoKeys/${{ secrets.GCP_KMS_CRYPTO_KEY }},mode=rw,size=100,type=pd-standard \ | |
--no-shielded-secure-boot \ | |
--shielded-vtpm \ | |
--shielded-integrity-monitoring \ | |
--reservation-affinity=any | |
- name: Updating GCP Instance Group Manager | |
run: | | |
gcloud compute instance-groups managed set-instance-template ${{ secrets.GCP_GROUP_MANAGER }} \ | |
--project=${{ secrets.GCP_PROJECT_ID }} \ | |
--region=${{ secrets.GCP_REGION }} \ | |
--template=projects/${{ secrets.GCP_PROJECT_ID }}/regions/${{ secrets.GCP_REGION }}/instanceTemplates/${{ secrets.GCP_TEMPLATE_NAME }} | |
- name: Start Rolling Update | |
run: | | |
gcloud compute instance-groups managed rolling-action start-update ${{ secrets.GCP_GROUP_MANAGER }} \ | |
--project=${{ secrets.GCP_PROJECT_ID }} \ | |
--type='proactive' \ | |
--version=template=projects/${{ secrets.GCP_PROJECT_ID }}/regions/${{ secrets.GCP_REGION }}/instanceTemplates/${{ secrets.GCP_TEMPLATE_NAME }} \ | |
--region=${{ secrets.GCP_REGION }} | |
- name: Wait for Instance Group to Reach Target State | |
run: | | |
gcloud compute instance-groups managed wait-until ${{ secrets.GCP_GROUP_MANAGER }} --version-target-reached --region=${{ secrets.GCP_REGION }} |