Deploying NodeJS application to Google App Engine
Follow instructions on website https://cloud.google.com/appengine/docs/standard/nodejs/building-app
Prerequisites
-
Have a Google Cloud Account + Your NodeJS Application
In this example you will use Use gcloud app deploy and gcloud app browse to deploy and view your service from the Google Cloud CLI (Command Line Interface).
Deploy Node JS Project to Google App Engine with Github Actions CI/CD
*** see full example ****
Prerequisites
-
Have a Google Cloud Account
Have creted your NodeJS application and Deployed it to Github
STEP 1: Setup Google Cloud Project
Follow Official instructions on Google Cloud Project Setup for GAE: https://cloud.google.com/appengine/docs/standard/nodejs/building-app/creating-project
STEP 2: Google Cloud Service Account Setup
A Google Cloud Service Account is a special type of Google account that belongs to your application or a virtual machine (VM) rather than to an individual user. It is used to authenticate and authorize your application to access Google Cloud services, APIs, and other resources securely. Here is how you set it up:
- Go to the Google Cloud Console.
- Navigate to IAM & Admin → Service Accounts.
- Click on Create Service Account.
- Provide an appropriate name and description for the service account. For instance, use github-ci-cd as it will be utilized for Github CI/CD.
-
Assign the following roles:
- App Engine Admin
- Cloud Build Service Account
- Service Account User
- Storage Object Admin
-
Click the three dots and select Manage keys.
-
Click on Add Key → Create New Key.
-
Choose the JSON key type and securely download it. Remember, this key grants access to Google Cloud resources, so keep it safe.
STEP 3: Create app.yaml and Explanation (and shows deploy locally)
What is app.yaml?
- The app.yaml file configures settings and routing for Google App Engine applications.
Placement:
- Keep the app.yaml in your project's root directory alongside your source code.
Example Configuration:
# [START app_yaml] runtime: nodejs18 service: node-express-api # [END app_yaml] |
Explanation of Configuration:
- runtime: Specifies the runtime environment (e.g., nodejs18).
- service: Defines the service name, typically a project-specific prefix or subdomain.
This configuration example directs incoming requests to the appropriate static files within the build directory, which further handles the different API routing.
Deploy your App Locally:
Deploy your Node app by executing the command in root directory of your project:
gcloud app deploy |
This command will package and upload your compiled build to Google Cloud. The deployment process may take a few minutes.
After deployment completion, you'll receive a URL where your Node app is hosted. Open the URL to check the deployed Node App.
STEP 4: Setup to Deploy from GitHub
CI/CD and GitHub Actions:
-
CI/CD: CI (Continuous Integration) and CD (Continuous Deployment) automate the software delivery process. CI involves merging code changes into a shared repository frequently, automatically running tests, and validating changes. CD automates the deployment of validated code changes to production or staging environments.
-
CI/CD in GitHub Actions: GitHub Actions provide workflows to automate tasks in your repository, including CI/CD processes. These workflows are defined in YAML files and can be triggered by various events like code pushes, pull requests, etc.
Storing Service Account Key and Project ID in GitHub Secrets:
- Store your Service Account Key and Project ID as secrets in your GitHub repository to securely access them during the CI/CD process.
- You can create Github secrets by Github Repository → Settings → Secrets & Variables → Actions → Secrets tab → New Repository Secret
- Secrets:
- GCP_SA_KEY: Your entire JSON of Service Account Key generated in previous step.
- GCP_PROJECT_ID Your GCP Project ID.
Workflow File Name & Placement:
- The workflow file should be named gcp-deploy.yml.
- Place this file in the .github/workflows directory in your project. Refer the below given repository in case of any confusion.
- Paste the below configuration in recently created yml file.
Configuration
name: Deploy to Goggle App Engine (GAE) on: push: branches: - main pull_request: branches: - main jobs: build-and-deploy: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Setup Node.js and yarn uses: actions/setup-node@v2 with: node-version: '18' - name: Install dependencies run: yarn install --frozen-lockfile - name: Build Node Project run: yarn build - name: Google Cloud Auth uses: 'google-github-actions/auth@v2' with: credentials_json: '${{ secrets.GCP_SA_KEY }}' project_id: ${{ secrets.GCP_PROJECT_ID }} - name: Set up Cloud SDK uses: 'google-github-actions/setup-gcloud@v2' - name: Deploy to Google App Engine run: | gcloud app deploy app.yaml --quiet |