Deploy your application to Google Cloud on your CI/CD

This tutorial describes how to set up your CI/CD to deploy your application to Google Cloud.

For this example, we will be using Jenkins as our CI.

Kubernetes Buildpack

To deploy a project to Google Cloud, you need a couple of technologies, to not bother users with installing these technologies, we created a shopsys/kubernetes-buildpack image, which can be used for deployment.

Description of scripts included in Kubernetes Buildpack

Deploy to Kubernetes

Script used for building current state of an application into Google Cloud.

deploy-to-google-cloud.sh

Environment variables

Variable name Description
DOCKER_USERNAME docker login, docker account needs to have docker repository available
DOCKER_PASSWORD docker password
FIRST_DOMAIN_HOSTNAME domain hostname for first domain - without http://
SECOND_DOMAIN_HOSTNAME domain hostname for second domain - without http://
FIRST_DOMAIN_SSL_DIRECTORY path to SSL certificates for first domain
SECOND_DOMAIN_SSL_DIRECTORY path to SSL certificates for second domain
PROJECT_ID project id of your google project
GIT_COMMIT commit hash to build your images uniquely
WORKSPACE project root dir
GOOGLE_CLOUD_STORAGE_BUCKET_NAME unique name for GCS bucket

Mounts

Docker Socket

Docker socket is used to build and push image of php-fpm.

-v /var/run/docker.sock:/var/run/docker.sock
Terraform state

Terraform state mounted locally to be able to apply changes or destroy Terraform infrastructure.

-v ~/google-cloud/.terraform/tfstate:/tmp/infrastructure/google-cloud/tfstate
SSL Certificates

SSL certificates for secured protocol, mount these into folder of your choice that you choose by setting environment variable.

Folder with certificates needs to contain these 3 files:

File name Common name
tls.key private.key
tls.crt certificate.crt
ca.crt ca_bundle.crt
-v ~/path/to/certificates-1:$FIRST_DOMAIN_SSL_DIRECTORY
-v ~/path/to/certificates-2:$SECOND_DOMAIN_SSL_DIRECTORY
Google Account Service

Mount your service-account.json obtained from google-cloud

-v ~/google-cloud/service-account.json:/tmp/infrastructure/google-cloud/service-account.json \

Usage

Use Kubernetes buildpack with environment variable set:

docker run \
    -v $WORKSPACE:/tmp \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v ~/google-cloud/.terraform/tfstate:/tmp/infrastructure/google-cloud/tfstate \
    -v ~/google-cloud/service-account.json:/tmp/infrastructure/google-cloud/service-account.json \
    -v ~/path/to/certificates-1:$FIRST_DOMAIN_SSL_DIRECTORY \
    -v ~/path/to/certificates-2:$SECOND_DOMAIN_SSL_DIRECTORY \
    -e DOCKER_USERNAME \
    -e DOCKER_PASSWORD \
    -e GIT_COMMIT \
    -e GOOGLE_CLOUD_STORAGE_BUCKET_NAME \
    -e FIRST_DOMAIN_HOSTNAME \
    -e SECOND_DOMAIN_HOSTNAME \
    -e PROJECT_ID \
    -e FIRST_DOMAIN_SSL_DIRECTORY \
    -e SECOND_DOMAIN_SSL_DIRECTORY \
    --rm \
    shopsys/kubernetes-buildpack:0.2.0 \
    .ci/deploy-to-google-cloud.sh

After running the script above, you will have your application running in Google Cloud and each deployment will be executed same way.

Destroy Google Cloud Infrastructure

The script used for deleting infrastructure on google. Script sends request to Google Cloud to stop all the resources provided by Terraform and updates tfstate file.

destroy-google-cloud-infrastructure.sh

Environment variables

Variable name Description
PROJECT_ID project id of your google project
WORKSPACE project root dir
GOOGLE_CLOUD_STORAGE_BUCKET_NAME unique name for GCS bucket

Mounts

Docker Socket

Docker socket is used to build and push image of php-fpm

-v /var/run/docker.sock:/var/run/docker.sock
Terraform state

Terraform state mounted locally to be able to apply changes or destroy Terraform infrastructure.

-v ~/google-cloud/.terraform/tfstate:/tmp/infrastructure/google-cloud/tfstate

Usage

docker run \
    -v $WORKSPACE:/tmp \
    -v ~/google-cloud/.terraform/tfstate:/tmp/infrastructure/google-cloud/tfstate \
    -v ~/google-cloud/service-account.json:/tmp/infrastructure/google-cloud/service-account.json \
    -e GOOGLE_CLOUD_STORAGE_BUCKET_NAME \
    -e PROJECT_ID \
    shopsys/kubernetes-buildpack:0.2.0 \
    .ci/destroy-google-cloud-infrastructure.sh