How to Setup Gitlab CI Pipeline For Google Cloud App Engine?
So we will be completing the Automation in 3 Steps:
- Creating the Service Account or Already using the Existing one.
- Configure the Gitlab CL Settings.
- Adding Required Script in the Exiting code.
- All Set for Automation!
Service Account
First go to your GCP Console and navigate to IAM & Admin then to the Service Accounts. So for the new GCP Project the service account automatically get assigned or if you want give the access to the another account then you can create the new one. Now we have two cases:
Case1: Default Service account
Now you need to go to key tab and then create the JSON Key (That is require for Gitlab CI Configuration)
Case2 : Creating New Service Account
- Create the new service account and assign the App Engine Admin (This is required for the giving the rights to Gitlab)
- Then create the JSON key in the same way mentioned above.
Now visit the Storage and go to the default bucket for the App Engine with name:
- staging.PROJECT-ID.appspot.c
- us.artifacts.PROJECT-ID.appspot.com
Then add the service account as the member of this bucket while giving the permissions of Storage Object Creator and Storage Object Viewer.
Last Step to enable the Cloud-build and App Engine Admin API with the associated billing account.
Configuring the Gitlab CI
Now visit the setting tab of repo and navigate to CI/CD then find the variable section.
Now add two variable with name PROJECT_ID and SERVICE_ACCOUNT:
PROJECT_ID: Put the ID from Project Setting page.
SERVICE_ACCOUNT: Copy and paste the JSON key which was downloaded while creating the service account key.
Now create the .gitlab-ci.yml file in the root of your code.
image: google/cloud-sdk:alpine
deploy_production:
stage: deploy
environment: Production
only:
- master
script:
- echo $SERVICE_ACCOUNT > /tmp/$CI_PIPELINE_ID.json
- gcloud auth activate-service-account --key-file /tmp/$CI_PIPELINE_ID.json
- gcloud --quiet --project $PROJECT_ID app deploy app.yaml dispatch.yaml
deploy_staging:
stage: deploy
environment: Staging
only:
- staging
script:
- echo $SERVICE_ACCOUNT > /tmp/$CI_PIPELINE_ID.json
- gcloud auth activate-service-account --key-file /tmp/$CI_PIPELINE_ID.json
- gcloud --quiet --project $PROJECT_ID app deploy staging-app.yaml
after_script:
- rm /tmp/$CI_PIPELINE_ID.json
You can change the branch name for Production and Staging environment as per your requirements.
Now push the .gitlab-ci.yml file to the repo after eliminating it from .gitignore file and you are good to go.
After successful code push check the CI/CD on the Gitlab project you can find the JOB is created also you can check the logs from there.
Congo so from now onwards Git Push will now automatically going to deploy the code to respective environment on the App Engine.