Implementing continuous delivery with Travis CI and App Engine

Testing is an important aspect of creating software. To ensure that unit tests are run against every build of a project, many engineering organizations have adopted the practice of continuous integration (CI), which involves using tools, such as Jenkins or Travis CI, to ensure that all new code is automatically and consistently tested for errors. Continuous delivery (CD) goes one step further by ensuring you can deploy every build into a production-like environment and then pass integration tests in that environment. You can run integration tests by using a CI/CD service on Google Cloud to deploy your app as part of the build process.

Although this article discusses Travis CI, most of the ideas apply to other CI tools, such as Jenkins. Travis integrates with GitHub, runs tests in isolation, and best of all, when running on Google Cloud, you don't need to manage your own infrastructure.

About the tutorial

This tutorial demonstrates a Python Flask app that runs on App Engine and interacts with the Google Books API. The app demonstrates how Travis CI can deploy and run end-to-end tests in a staging environment as part of the testing process, triggered by a git push command. The following diagram shows an overview of the process.

Travis CI runs local tests between GitHub and Google Cloud deployment

Travis CI has built-in support for deploying to the App Engine standard or flexible environments by using deployment providers. When you use these providers, some of the basic setup steps stay the same, but the configuration file is simpler. This tutorial demonstrates what these providers do. In this tutorial, Travis CI downloads the Cloud SDK, which can also be used for purposes other than deployment, such as running local emulators for your unit tests. Although this tutorial uses the App Engine deployment provider, refer to the Travis CI documentation, for more information on other providers.

This tutorial creates new service account credentials and API keys that you should only use for this tutorial. To simplify this tutorial, the encrypted keys are uploaded to GitHub. This is a security anti-pattern that you should avoid in production environments.


  • Configure Cloud Shell for use with GitHub.
  • Configure GitHub and Travis CI accounts.
  • Fork the sample code repo and enable it in Travis.
  • Create an App Engine app and create credentials for Travis CI.
  • Install the Travis command-line tools.
  • Update the sample code for your project and use Travis to encrypt secrets.
  • Push new code to GitHub and have Travis CI automatically test the code on App Engine.


This tutorial uses the following billable components of Google Cloud:

  • App Engine

To generate a cost estimate based on your projected usage, use the pricing calculator. New Google Cloud users might be eligible for a free trial.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  4. Enable the Compute Engine, Google Books, and App Engine APIs.

    Enable the APIs

  5. Create accounts for GitHub and Travis CI.

When you finish this tutorial, you can avoid continued billing by deleting the resources you created. For more information, see Cleaning up.

Configuring Cloud Shell for your GitHub account

For the majority of this tutorial you are using Cloud Shell, including committing code to a GitHub repository. GitHub only allows commits by using SSH connections; therefore, Cloud Shell needs to be configured with SSH keys.

  1. In the Cloud Console, click Activate Cloud Shell.

    GO TO Cloud Shell

  2. Generate a new SSH key.

  3. Add the new SSH key to your GitHub account.

  4. Verify that your SSH key can communicate with GitHub.

Configuring Travis CI with your GitHub account

  1. On the Travis CI page, sign in with your GitHub account.
  2. On the Sign in to GitHub to continue to Travis CI page, enter your GitHub credentials and then click Sign In.
  3. Click Authorize travis-pro.
  4. On the GitHub Apps Integration page, click Activate.
  5. Click Approve & Install.

Forking the sample code repository

You fork the repository in GitHub to replace the sample project's credentials with your own project's credentials.

  1. Go to the sample code repository, and then click Fork.

    Sample code repository in GitHub

  2. In Cloud Shell, set up your local git config.

    git config --global ""
    git config --global "Your Name"
  3. Clone the sample code. Replace [YOUR-GITHUB-USERNAME] with your username.

    git clone[YOUR-GITHUB-USERNAME]/continuous-deployment-demo.git

Verifying your fork is in Travis CI

  • On the Travis CI website, in the upper-right corner, click your username to open your account settings.

    On the Repositories tab, the continuous-deployment-demo repository is listed.

    Repositories in Travis CI

    Now, every time you push a commit to this repository, Travis CI runs the configured tests and provides a detailed report of the tests' results.

Install Travis command-line tools

  • In Cloud Shell, install the Travis client.

    sudo gem install travis

Create an App Engine app

  • In Cloud Shell, create your App Engine app. Replace [REGION] with your Google Cloud region.

    gcloud app create --region=[REGION]

Creating credentials

To deploy the app to your Google Cloud project, you must create two types of credentials:

  • Service account credentials that enable the Cloud SDK to authenticate with your Google Cloud project.
  • A public API key that the App Engine app uses to communicate with the Books API.

Create a service account

  1. In the Cloud Console, open the IAM Service Accounts page.


  2. Click Create Service Account

  3. Enter a Service account name, such as continuous-integration-test.

  4. Click Create.

  5. In the Role menu, select App Engine > App Engine Admin.

  6. Click +Add Another Role, and then select Cloud Storage > Storage Admin.

  7. Click Done.

  8. Click Actions for the continuous-integration-test service account.

  9. Click Create Key.

  10. Under Key type, select JSON.

  11. Click Create. The Cloud Console downloads a new JSON file to your computer. The name of this file starts with your project ID.

  12. To upload this file to Cloud Shell, click More , and then select Upload file.

    Upload button in Cloud Shell

  13. Move and rename this file to the root of your GitHub repo.

    mv ~/<your_credentials_filename>.json ~/continuous-deployment-demo/client-secret.json

Create the Books API Key

  1. In the Google Cloud Console, open the API Credentials page.

    Go to the Credentials page

  2. Go to Create credentials > API key.

    Create credentials page

  3. A key is automatically created. Copy the API key, and then click Close.

  4. In Cloud Shell, add the new API key to the file. Replace [YOUR_API_KEY] with the API key you copied previously.

    export API_KEY=[YOUR_API_KEY]
    cd ~/continuous-deployment-demo
    echo "key = '${API_KEY}'" >

Encrypt the credentials for Travis CI to use

These credentials are added to a public GitHub repository, and therefore need to be encrypted.

Travis CI is able to decrypt only one file. You create a tar archive that contains the required credentials, encrypt the archive locally, add the encrypted archive to the app repository, and configure Travis to decrypt the credentials during the deployment process. For more information, see encrypting files for Travis CI.

  1. In Cloud Shell, create a tar archive file that contains the necessary credentials:

    tar -czf credentials.tar.gz client-secret.json
  2. Using the Travis CI command-line tools, log into Travis CI:

    travis login --pro
  3. When asked to complete shell installation, enter Y, and then enter your GitHub credentials.

  4. Encrypt the archive of credentials:

    travis encrypt-file credentials.tar.gz --pro
  5. When prompted to overwrite the existing file, enter yes.

  6. Copy the output line that begins with openssl aes-256-cbc to the clipboard.

    Console output that begins with `openssl`

  7. Configure Travis CI to decrypt the credentials by updating .travis.yml using sed. Replace [YOUR_COMMAND_OUTPUT] with the console output you copied in the previous step.

    sed -i 's/DECRYPT_COMMAND/[YOUR_COMMAND_OUTPUT]/g' .travis.yml
  8. Edit the .travis.yml file and the file to reflect your project name:

    sed -i "s/continuous-deployment-python/${DEVSHELL_PROJECT_ID}/g" .travis.yml
    sed -i "s/continuous-deployment-python/${DEVSHELL_PROJECT_ID}/g"
  9. Add the encrypted archive of credentials to the repository:

    git add credentials.tar.gz.enc .travis.yml
    git commit -m "Adds encrypted credentials for Travis and updates project name"

Commit to GitHub and test with Travis CI

Now that you have configured the sample app for Travis CI and enabled Travis CI on your fork of the project in GitHub, you can push the latest version of the code to watch a build and testing cycle.

git push origin master

While the test runs, you can watch the build output in real time on the Travis dashboard. If both tests and deployment to App Engine succeed, your CI/CD process has run successfully. The process is defined by the .travis.yml file, for more information on the .travis.yml visit the Travis CI documentation.

Travis dashboard with test's output

Additional output

Cleaning up

To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.

You can delete the project entirely or stop the App Engine.

Delete the project

  1. In the Cloud Console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

What's next