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
- 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.
Enable the App Engine Admin, Cloud Build 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:
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:
Set the status of the App Engine Admin role to Enable.
Deploy your app
In a terminal window, go to the directory containing the repository:
cd hello-world
Deploy the sample app:
gcloud app deploy app.yaml
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
In a terminal window, go to the directory containing the repository:
cd hello-world
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
Add
cloudbuild.yaml
to the repository:git add .
Commit the file with a comment describing the history of this action:
git commit -m "Add cloudbuild.yaml file"
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
In the Google Cloud console, open the Cloud Build Triggers page.
If your Google Cloud project isn't selected, click Select a project, and then click the name of your Google Cloud project.
Click Create Trigger.
The Create trigger page opens.
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/
.
- In the Name field, type
Click Create to save your build trigger.
Push a change to your app
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)
Add the file to Git:
git add .
Commit the file with a comment describing the history of this action:
git commit -m "Update app to demonstrate build triggers"
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
In the Google Cloud console, open the Cloud Build Triggers page.
If your Google Cloud project isn't selected, click Select a project, and then click the name of your Google Cloud project.
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
In the Google Cloud console, open the Cloud Build Triggers page.
If your Google Cloud project isn't selected, click Select a project, and then click the name of your Google Cloud project.
On the same line as the trigger you want to delete, click More more_vert, and then click Delete.
Delete the repository
In the Google Cloud console, open the All repositories page for Cloud Source Repositories.
Hold the pointer over the repository you want to delete and click Settings settings.
The General settings page opens.
Click Delete this repository delete.
The Remove repository dialog opens.
Type the name of the repository you want to delete.
Click Delete.
What's next
- Learn more about Cloud Build.