CD added #19
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: AMI creation | |
on: | |
push: | |
branches: | |
- main | |
env: | |
PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} | |
GCP_SA_KEY: ${{ secrets.GCP_SA_KEY }} | |
GCP_REGION: 'us-east1' # Make sure this is the correct region | |
MIG_NAME: 'webapp-manager' # The name of your managed instance group | |
SERVICE_ACCOUNT_EMAIL: ${{ secrets.GCP_SERVICE_ACCOUNT_EMAIL }} | |
NETWORK: 'assignment-9' # Replace with your actual network name | |
SUBNETWORK: 'webapp' # Replace with your actual subnetwork name | |
jobs: | |
ami-creation: | |
if: github.repository == 'csye6225-ramaraju/webapp' # action exec only if code is pushed to org repo main branch | |
name: build packer ami | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Configure MySQL | |
run: | | |
sudo apt-get update | |
sudo apt-get install -y mysql-server | |
sudo systemctl start mysql | |
sudo systemctl status mysql | |
sudo mysql --user=root --password=root -e "CREATE DATABASE IF NOT EXISTS restapi;" | |
sudo mysql --user=root --password=root -e "SHOW DATABASES;" | |
sudo mysql --user=root --password=root -e "USE restapi; CREATE TABLE IF NOT EXISTS Users (id CHAR(36) BINARY PRIMARY KEY, firstName VARCHAR(255) NOT NULL, lastName VARCHAR(255) NOT NULL, userName VARCHAR(255) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL, createdAt DATETIME NOT NULL, updatedAt DATETIME NOT NULL);" | |
# Setup Node.js | |
- name: Setup Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '16' | |
# Install Dependencies | |
- name: Install Dependencies | |
run: npm install | |
# Run Integration Tests | |
- name: Run Integration Tests | |
run: npm test user.test.js | |
# delete node modules before zipping | |
- name : delete node modules | |
run: rm -rf node_modules | |
# Create Artifact | |
- name: Create Artifact | |
run: zip -r application.zip ./* | |
# Upload the Artifact | |
- name: Upload Artifact | |
uses: actions/upload-artifact@v3 | |
with: | |
name: application-artifact | |
path: application.zip | |
# Setup Google Cloud | |
- name: Setup Google Cloud | |
uses: google-github-actions/setup-gcloud@v0.2.0 | |
with: | |
service_account_key: ${{ secrets.GCP_SA_KEY }} | |
project_id: ${{ secrets.GCP_PROJECT_ID }} | |
export_default_credentials: true | |
# Setup Packer | |
- name: Setup Packer | |
uses: hashicorp/setup-packer@main | |
with: | |
packer-version: 'latest' | |
- name: List files in the root and /tmp directory | |
run: | | |
ls -lah | |
ls -lah /tmp | |
- name: List files in the /tmp directory | |
run: | | |
ls -lah /tmp | |
# Build Machine Image with Packer | |
- name: Build Machine Image with Packer | |
run: | | |
packer init . | |
ls -lah /tmp | |
packer build -var 'project_id=${{ secrets.GCP_PROJECT_ID }}' webapp-packer.pkr.hcl | |
# CD | |
- name: Get the latest image name | |
id: get-latest-image | |
run: | | |
IMAGE_NAME=$(gcloud compute images list --filter="name:webapp-image*" --format="get(name)" --sort-by="~creationTimestamp" --limit=1) | |
echo "Latest image name is $IMAGE_NAME" | |
echo "::set-output name=image_name::$IMAGE_NAME" | |
# Create a new instance template with the new image and startup script | |
- name: Create a new instance template with startup script | |
run: | | |
gcloud compute instance-templates create "${{ env.MIG_NAME }}-template-${{ steps.get-latest-image.outputs.image_name }}" \ | |
--machine-type=e2-medium \ | |
--image="${{ steps.get-latest-image.outputs.image_name }}" \ | |
--image-project=${{ env.PROJECT_ID }} \ | |
--region=${{ env.GCP_REGION }} \ | |
--network="projects/${{ env.PROJECT_ID }}/global/networks/${{ env.NETWORK }}" \ | |
--subnet="projects/${{ env.PROJECT_ID }}/regions/${{ env.GCP_REGION }}/subnetworks/${{ env.SUBNETWORK }}" \ | |
--metadata=startup-script="${{ secrets.GCP_STARTUP_SCRIPT }}" \ | |
--service-account='${{ secrets.GCP_SERVICE_ACCOUNT_EMAIL }}' \ | |
--scopes='https://www.googleapis.com/auth/cloud-platform' | |
# Update managed instance group with the new template | |
- name: Update managed instance group | |
run: | | |
gcloud compute instance-groups managed set-instance-template ${{ env.MIG_NAME }} \ | |
--template="${{ env.MIG_NAME }}-template-${{ steps.get-latest-image.outputs.image_name }}" \ | |
--region=${{ env.GCP_REGION }} | |
#Start a basic rolling update | |
- name: Start a basic rolling update | |
run: | | |
gcloud compute instance-groups managed rolling-action start-update ${{ env.MIG_NAME }} \ | |
--version template="${{ env.MIG_NAME }}-template-${{ steps.get-latest-image.outputs.image_name }}" \ | |
--region=${{ env.GCP_REGION }} \ | |
--max-surge=3 \ | |
--max-unavailable=0 | |