Deploying to Google Compute Engine

3 minute read

To deploy to Google Compute Engine, you will need to create a container that can authenticate with your Google Account, and with the appropriate Google product, as well as run the Google Cloud CLI to execute your intended commands.

We maintain an example repository with an image stored on Docker Hub to simplify this process. You can copy setup instructions from this repo or reuse the Dockerfile, our turnkey Google Cloud image or our GCR authentication generator simply by adding the necessary elements from our Google Cloud repo to your codeship-services.yml file.

Authentication

To deploy to Compute Engine, you will need to define a Google Service account as well as add your Google credentials to your encrypted environment variables so that the gcloud utility can authenticate appropriately.

CodeShip Public Key

Some Google Cloud services will require that you add your CodeShip public key for authentication purposes.

Note that Google may fail authentication if you do not add the Google Cloud user the key is for to the end of the key. For example, if the Google Cloud user is deploy@CodeShip, you will want to add deploy@CodeShip to the end of the SSH key itself, otherwise Google will not load the key for the user appropriately.

Commands And Deployments

Creating Your Services

You will want to add a service which builds the Google Cloud deployment image, which is maintained by CodeShip, in your codeship-services.yml file. For example:

googleclouddeployment: image: codeship/google-cloud-deployment encrypted_env_file: - google-credentials.encrypted add_docker: true volumes: - ./:/deploy

Note that this example adds your Google Cloud account credentials as encrypted environment variables and mounts the repository as a volume to the /deploy folder inside the container so that it is usable as part of the build.

Deployment Commands

After defining your authentication variables and your deployment service, you will want to run deployment commands via your codeship-steps.yml file.

Because each step runs in a separate group of containers, you will likely want to bundle you Google Cloud commands together in a script file that you add to your repository and call from a step:

- name: google-cloud-deployment service: googleclouddeployment command: google-deploy.sh

Inside this deployment script will be all commands you want to run via the Google Cloud or Kubernetes CLI, both included in the [deployment image that we maintain]((https://hub.docker.com/r/codeship/google-cloud-deployment/).

Here is an example deployment script that you can use as a basis for your own deployments. Note that it authenticates at the top using the command discussed earlier.

#!/bin/bash # Authenticate with the Google Services codeship_google authenticate # switch to the directory containing your app.yml (or similar) configuration file # note that your repository is mounted as a volume to the /deploy directory cd /deploy/ # deploy the application gcloud compute copy-files "${LOCAL_PATH}" "${INSTANCE_NAME}:${REMOTE_PATH}"

Please see the documentation for gcloud compute copy-files on which options are available and how they affect the deployment.

Deploying without the gcloud CLI

Since Google Compute Engine instances are regular VMs, you don’t need the gcloud CLI to connect and deploy your application.

You can also add a SSH key pair to your Google account, and apply this keypair to your Google Cloud project. If you make the private key available to the deployment script, you can then use regular ssh and scp commands to copy files to your instances.

scp -i ~/.ssh/my-ssh-key "${LOCAL_PATH}" "${REMOTE_USERNAME}@${REMOTE_IP_ADDRESS}:${REMOTE_PATH}"