Quickstart for automating App Engine deployments with Cloud Build

This topic describes how to automatically deploy an application stored in Cloud Source Repositories to App Engine when there is a new commit.

Before you begin

  1. Complete the steps from Quickstart Creating a Repository.

    After you complete the quickstart, you will have an application you can deploy to App Engine.

  2. Enable the following 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 do not allow certain actions, such as deploying to App Engine.

You can enable your service account to deploy to App Engine by granting the account additional IAM roles to the Cloud Build service account.

To grant App Engine access to the Cloud Build service account:

  1. Open the IAM page in console

    Open the IAM page

  2. Select your project and click Continue.

  3. In the list of members, look for your Cloud Build service account named [PROJECT_NUMBER]@cloudbuild.gserviceaccount.com, where [PROJECT_NUMBER] is your GCP project project number.
  4. Click the pencil icon in that row.
  5. Click Add another role.
  6. From the Select a role dropdown menu, select App Engine and then select App Engine Admin.
  7. Click Save.

Deploy your application

  1. From a terminal window, navigate to the directory containing the repository.

    cd hello-world
    
  2. Deploy the sample application.

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

    gcloud app browse
    

    The browser window should now read:

    Hello world!
    

Create a cloudbuild.yaml file

  1. From a terminal window, navigate to the directory containing the repository.

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

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

Add the cloudbuild.yaml file to your repository

  1. Add the file to the repository.

    git add .
    
  2. Commit the file.

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

    git push origin master
    

Create a build trigger

  1. Open the Cloud Build page in the Google Cloud Platform Console.

    Open the Cloud Build page

  2. Select your project and click Open.

  3. Click Create trigger.

  4. Select Cloud Source Repository.

  5. From the list of available repositories, select the hello-world repository, then click Continue.

  6. In the Name box, type:

    App Engine Test
    
  7. Under Trigger type, select Branch.

  8. Under Build Configuration, select cloudbuild.yaml.

  9. In the cloudbuild.yaml location box, type:

    /cloudbuild.yaml
    
  10. Click Create Trigger.

Push a change to your application

  1. From a terminal window, use a text editor to update the main.py file to read as follows:

    #!/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.

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

    git push origin master
    

View your build in progress

  1. Open the Build triggers page in the Google Cloud Platform Console.

    Open the Build triggers page

  2. Select your project and click Open.

  3. In the left nav, click Build 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 checkmark appears next to the build entry.

Re-test your application

From, a terminal window, open your application:

gcloud app browse

The browser window now reads:

I update automatically!

Clean up

To avoid incurring charges to your GCP account for the resources used in this quickstart:

Delete the build trigger

  1. Open the Build triggers page in the Google Cloud Platform Console.

    Open the Build triggers page

  2. Select your project and click Open.

  3. Locate the trigger you created.

  4. Click the More button More button next to the trigger you want to delete.

  5. Select Delete.

Delete the repository

  1. Open the All Repositories view for Cloud Source Repositories:

    Open Cloud Source Repositories

  2. Hover over the repository and click the Gear button.

    The General Settings screen opens.

  3. Click Delete This Repository.

    The Remove Repository screen opens.

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

  5. Click Delete.

What's next

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Source Repositories