Deploy to Google Cloud

This guide will help you configure Bitbucket Pipelines to deploy automatically to Google Cloud App Engine. You'll see first how to set up your credentials before looking into the simple application deployment.

You may also be interested in this Bitbucket repo: Deploy to Google App Engine.

 

Install the Google Cloud SDK

If you don't have the Cloud SDK installed on the image that you're using for your pipelines, just use these commands:

curl -o /tmp/google-cloud-sdk.tar.gz https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-155.0.0-linux-x86_64.tar.gz
tar -xvf /tmp/google-cloud-sdk.tar.gz -C /tmp/
/tmp/google-cloud-sdk/install.sh -q
source /tmp/google-cloud-sdk/path.bash.inc

You'll need to have Python installed on the image as well. We've found that v2.7.9 works (the version installed on the node:4.6.0 image used below) but v3.6 fails.

The following Bitbucket Pipelines configuration will install the Google Cloud SDK and test it by displaying its version:

bitbucket-pipelines.yml
image: node:4.6.0

pipelines:
  default:
    - step:
        script: # Modify the commands below to build your repository.
          # Downloading the Google Cloud SDK
          - curl -o /tmp/google-cloud-sdk.tar.gz https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-155.0.0-linux-x86_64.tar.gz
          - tar -xvf /tmp/google-cloud-sdk.tar.gz -C /tmp/
          - /tmp/google-cloud-sdk/install.sh -q
          - source /tmp/google-cloud-sdk/path.bash.inc
          - gcloud -v

 

Note that this install method is using a specific version of the Cloud SDK to avoid using an interactive installer. You can check for the latest version of the SDK on the Google Cloud website and adapt the script accordingly if required.

You can check your bitbucket-pipelines.yml file with our online validator.

 

Set up your credentials

We will use the Application Default Credentials to get authorization to call Google Cloud services.

First, you'll need to enable the admin APIs for Google App Engine if it's not already done for your project. Simply go to API Manager > Library and search for Google App Engine Admin API. Navigate to the corresponding API admin page and click the link to enable the API on top of the page.

Once you have enabled API, go to API Manager > Credentials and follow the steps below to generate your credentials:

  1. In the Create credentials dropdown select Service account key.
  2. In the next page select Compute Engine default service account in the Service account dropdown.
  3. Select JSON as the format.
  4. Hit the create button.
  5. Save the generated JSON file on your local machine (this should be done automatically).

You can now add the credentials to Bitbucket Pipelines by encoding it in the base64 format and saving it as a Pipelines environment variable.

Open up your terminal and browse to the location of your JSON credential file. Then run the command below to encode your file in base64 format. Copy the output of the command to your clipboard.

base64 <your-credentials-file.json>

Go to your repository settings in Bitbucket and navigate to Pipelines > Environment variablesCreate a new variable named GCLOUD_API_KEYFILE and paste the encoded service account credentials in it.

Add another variable called GCLOUD_PROJECT and set the value to the key of your Google Cloud project:

 

Now we can verify that your credentials are properly installed by trying to authenticate using the Google Cloud SDK and Bitbucket Pipelines.

bitbucket-pipelines.yml
image: node:4.6.0

pipelines:
  default:
    - step:
        script: # Modify the commands below to build your repository.
          # Downloading the Google Cloud SDK
          - curl -o /tmp/google-cloud-sdk.tar.gz https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-155.0.0-linux-x86_64.tar.gz
          - tar -xvf /tmp/google-cloud-sdk.tar.gz -C /tmp/
          - /tmp/google-cloud-sdk/install.sh -q
          - source /tmp/google-cloud-sdk/path.bash.inc
          # Authenticating with the service account key file
          - echo $GCLOUD_API_KEYFILE | base64 --decode --ignore-garbage > ./gcloud-api-key.json
          - gcloud auth activate-service-account --key-file gcloud-api-key.json
          # Linking to the Google Cloud project
          - gcloud config set project $GCLOUD_PROJECT

 

If you push the bitbucket-pipelines.yml configuration above to your repository you should get a green result confirming that your service account credentials have been activated:

 

Deploying to Google Cloud App Engine

After completing the authentication step above you can deploy your application to Google App Engine using the appropriate command for your application type. For application types based on a yaml configuration file you can use the gcloud app deploy command:

- gcloud -q app deploy app.yaml

For a Java application deployed using the appengine maven plugin you should make sure you're using a docker image containing maven and add the following commands to your pipeline script:

- gcloud components install app-engine-java
- 'mvn install appengine:deploy'

 

A full pipeline configuration that deploys automatically to Google App Engine after running tests would look like the following:

bitbucket-pipelines.yml
image: node:4.6.0

pipelines:
  default:
    - step:
        script: # Modify the commands below to build your repository.
          # Downloading the Google Cloud SDK
          - curl -o /tmp/google-cloud-sdk.tar.gz https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-155.0.0-linux-x86_64.tar.gz
          - tar -xvf /tmp/google-cloud-sdk.tar.gz -C /tmp/
          - /tmp/google-cloud-sdk/install.sh -q
          - source /tmp/google-cloud-sdk/path.bash.inc
          # Authenticating with the service account key file
          - echo $GCLOUD_API_KEYFILE | base64 --decode --ignore-garbage > ./gcloud-api-key.json
          - gcloud auth activate-service-account --key-file gcloud-api-key.json
          # Linking to the Google Cloud project
          - gcloud config set project $GCLOUD_PROJECT
          # Deploying the application
          - gcloud -q app deploy app.yaml

or for a maven-based Java application:

bitbucket-pipelines.yml
image: maven:3.5.0-jdk-7

pipelines:
  default:
    - step:
        script: # Modify the commands below to build your repository.
          # Downloading the Google Cloud SDK
          - curl -o /tmp/google-cloud-sdk.tar.gz https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-155.0.0-linux-x86_64.tar.gz
          - tar -xvf /tmp/google-cloud-sdk.tar.gz -C /tmp/
          - /tmp/google-cloud-sdk/install.sh -q
          - source /tmp/google-cloud-sdk/path.bash.inc
          # Authenticating with the service account key file
          - echo $GCLOUD_API_KEYFILE | base64 --decode --ignore-garbage > ./gcloud-api-key.json
          - gcloud auth activate-service-account --key-file gcloud-api-key.json
          # Linking to the Google Cloud project
          - gcloud config set project $GCLOUD_PROJECT
          # Deploying the application
          - gcloud components install app-engine-java
          - 'mvn install appengine:deploy'

 

Remember, you can check your bitbucket-pipelines.yml file with our online validator.

最終更新日 2017 年 11 月 6 日

この翻訳に満足しましたか?

はい
いいえ
この記事についてのフィードバックを送信する

お探しの情報が見つかりませんか?

コミュニティへの質問

Powered by Confluence and Scroll Viewport.