Auto Deploy Code to GCP from Git Workflow

Working on CICD or have project code available in git repository and not sure how to deploy the code to GCP automatically? Below steps are going to help you out to setup GCP auto deployment.

Enabling workflow auto deployment to GCP will make work easy and also enables CICD approach. Whenever code is pushed to git, the code itself will be capable to deploy in GCP automatically.

All you need to do is, just add a .yaml file in git workflow and you are done. Deploying the code to GCP manually is time consuming work as well as every time it requires to have local setup in  the system, however enabling auto deployment in git repository can avoid this additional work.

From security point of view, you do not need to disclose GCP keys or any security keys while auto deploying to GCP.

Please follow the below steps to enable auto deployment code to GCP using git workflow.

Prerequisites:

  • GCP account or access.
  • Project created in GCP.
  • GCP deployment Key.
  • Git repository.
  • Project code available in git.

If you have Python code in git repository-

Steps:

  • Clone git repository code to your local system.
  • Go to project folder and create a another folder as .github and inside .github create another folder name workflows. (github/workflows)

  • Inside workflows create a file name deploy_gcp.yml (you can give any name but file should be .yml)

If you project code is Python-

  • Copy and paste the below code in deploy_gcp.yml file
name: Deploy to GCP
on:
  push:
    branches:
      - "*"
jobs:
  deploy_prod:
    name: Deploy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - run: echo Running deploy
      - name: Setup and configure gcloud CLI
        uses: google-github-actions/setup-gcloud@master
        with:
          project_id: Provide the name of your project in GCP
          service_account_key: provide GCP service account key
          export_default_credentials: true    
      - name: Deploy_gcp
        run: |
          gcloud app deploy app.yaml –quiet

For security reason please keep GCP service account key in git secrets and access the secret using key name provide in git secret. Example-

service_account_key: ${{secrets.SERVICE_ACCOUNT}}

  • Now in project folder create a file with name app.yaml

  • Copy and paste the below code in app.yaml file
runtime: python
threadsafe: yes
env: flex
service: provide any service name that you want to deploy(ex- myfirstservice)
runtime_config:
  python_version: 3
handlers:
- url: .*
  script: main.app
automatic_scaling:
  min_num_instances: 1
  max_num_instances: 10
resources:
  cpu: 1
  memory_gb: 2
  disk_size_gb: 10
env_variables: Add environemtn variables if any. example-
  my_project: "my_first_project_in_gcp"

If your project code is Nodejs-

  • Copy and paste the below code in deploy_gcp.yml file
name: Deploy to GCP
on:
  push:
    branches:
      - "*"
jobs:
  deploy_prod:
    name: Deploy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-node@v1
        with:
          node-version: "10.x"
          registry-url: "https://registry.npmjs.org"
      - run: npm ci
        env:
          CI: false
      - run: npm run build --if-present
      - run: npm audit fix  
      - run: echo Running deploy
      - name: Setup and configure gcloud CLI
        uses: google-github-actions/setup-gcloud@master
        with:
          project_id: Provide the name of your project in GCP
          service_account_key: provide GCP service account key
          export_default_credentials: true
      - name: Deploy_gcp
        run: |
          gcloud app deploy app.yaml --quiet

For security reason please keep GCP service account key in git secrets and access the secret using key name provide in git secret. Example-

service_account_key: ${{secrets.SERVICE_ACCOUNT}}

  • Now in project folder create a file with name app.yaml
  • Copy and paste the below code in app.yaml file
runtime:  nodejs12
service: provide any service name that you want to deploy(ex- myfirstservice)
handlers:
  - url: /
    secure: always
    static_files: build/index.html
    upload: build/index.html
  - url: /
    secure: always
    static_dir: build

Whenever code is pushed to git, auto deployment will be started. The log and activity you can view under git Actions tab. Once click on running job the log can be seen.

Also, after execution log can be viewed on clicking (…) three dots at right top corner shown under git action tab.

These are the basic feature of git workflow; however, git Action settings can be changed under git Settings -> Actions option.

Comments are closed.