Automate App Engine deployments with Cloud Build

This quickstart shows how to automatically deploy an app stored in Cloud Source Repositories to App Engine after a new commit.

Before you begin

  1. Complete the steps from Create a code repository in Cloud Source Repositories.

    After you complete that quickstart, you have an app you can deploy to App Engine.

  2. Enable the App Engine Admin, Cloud Build APIs.

    Enable the APIs

Grant App Engine access to the Cloud Build service account

Cloud Build uses a service account to deploy your code. The default permissions for this account don't allow certain actions, such as deploying to App Engine.

Enable your service account to deploy to App Engine by granting the account additional Identity and Access Management (IAM) roles:

  1. In the Google Cloud console, open the Cloud Build Settings page:

    Open the Cloud Build Settings page

    You'll see the Service account permissions page:

    The Service account permissions page.

  2. Set the status of the App Engine Admin role to Enable.

Deploy your app

  1. In a terminal window, go to the directory containing the repository:

    cd hello-world
    
  2. Deploy the sample app:

    gcloud app deploy app.yaml
    
  3. Verify that your app is running:

    gcloud app browse
    

    If your app is running, the browser displays the message Hello, World!.

Create a cloudbuild.yaml file

  1. In a terminal window, go to the directory containing the repository:

    cd hello-world
    
  2. Using a text editor, create a file named cloudbuild.yaml, and then paste the following configuration information:

    steps:
    - name: "gcr.io/cloud-builders/gcloud"
      args: ["app", "deploy"]
    timeout: "1600s"
    

Add the cloudbuild.yaml file to your repository

  1. Add cloudbuild.yaml to the repository:

    git add .
    
  2. Commit the file with a comment describing the history of this action:

    git commit -m "Add cloudbuild.yaml file"
    
  3. Using the git push command, add the contents of the local Git repository to Cloud Source Repositories:

    git push origin master
    

Create a build trigger

  1. In the GCP Console, open the Cloud Build Triggers page.

    Open the Triggers page

  2. If your Google Cloud project isn't selected, click Select a project, and then click the name of your Google Cloud project.

  3. Click Create Trigger.

    The Create trigger page opens.

  4. Fill out the following options:

    • In the Name field, type app-engine-test.
    • Under Event, select Push to a branch.
    • Under Source, select hello-world as your Repository and ^master$ as your Branch.
    • Under Configuration, select Cloud Build configuration file (yaml or json).
    • In the Cloud Build configuration file location field, type cloudbuild.yaml after the /.
  5. Click Create to save your build trigger.

Push a change to your app

  1. In a terminal window, use a text editor to update the main.py file by pasting the following code:

    #!/usr/bin/env python
    
    import webapp2
    
    class MainHandler(webapp2.RequestHandler):
        def get(self):
            self.response.write('I update automatically!')
    
    app = webapp2.WSGIApplication([
        ('/', MainHandler)
    ], debug=True)
    
  2. Add the file to Git:

    git add .
    
  3. Commit the file with a comment describing the history of this action:

    git commit -m "Update app to demonstrate build triggers"
    
  4. Using the git push command, add the contents of the local Git repository to Cloud Source Repositories:

    git push origin master
    

View your build in progress

  1. In the GCP Console, open the Cloud Build Triggers page.

    Open the Triggers page

  2. If your Google Cloud project isn't selected, click Select a project, and then click the name of your Google Cloud project.

  3. Click History.

    A list of all builds opens. At the top is a new entry that represents the build that began after you pushed your change to Cloud Source Repositories. When the build is ready, a green check mark appears next to the build entry.

Re-test your app

In a terminal window, open your app:

gcloud app browse

The browser now displays the message I update automatically!.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.

Delete the build trigger

  1. In the GCP Console, open the Cloud Build Triggers page.

    Open the Triggers page

  2. If your Google Cloud project isn't selected, click Select a project, and then click the name of your Google Cloud project.

  3. On the same line as the trigger you want to delete, click More , and then click Delete.

Delete the repository

  1. In the GCP Console, open the All repositories page for Cloud Source Repositories.

    Open Cloud Source Repositories

  2. Hold the pointer over the repository you want to delete and click Settings .

    The General settings page opens.

  3. Click Delete this repository .

    The Remove repository dialog opens.

  4. Type the name of the repository you want to delete.

  5. Click Delete.

What's next