This page explains how to deploy applications to App Engine using Cloud Build. If you're new to Cloud Build, read the quickstarts and the build configuration overview first.
App Engine is a fully managed, serverless platform for developing and hosting web applications at scale. For more information on App Engine, read the App Engine documentation.
Before you begin
Enable the App Engine API:
To run the
gcloud
commands on this page, install the Google Cloud CLI.Have your application source code that you want to build and deploy to App Engine handy. Your source code needs to be stored in a repository, such as Cloud Source Repositories, GitHub, or Bitbucket.
Required IAM permissions
In the Google Cloud console, select your project.
Grant the Cloud Build Service Account role to the App Engine default service account. If service account does not appear in the list, locate service account in Service Accounts page.
Grant the App Engine Admin role and Service Account User to the build service account:
Open the Cloud Build Settings page:
Set the status of the App Engine Admin role and the Service Account User role to Enabled.
Configuring the deployment
Cloud Build enables you to use any publicly available container image
to execute your tasks. You can do this by specifying the image in a build step
in the Cloud Build config file.
App Engine provides the gcloud app deploy
command, which builds an image with
your source code and deploys that image on App Engine. You can use the cloud-sdk
image
as a build step in your config file to invoke gcloud
commands within the image.
Arguments passed to this build step are passed to the gcloud CLI directly,
allowing you to run any gcloud
command in this image.
To deploy an application to App Engine, use the following steps:
Create a Cloud Build configuration file named
cloudbuild.yaml
orcloudbuild.json
.In the config file:
- Add a
name
field to specify thecloud-sdk
build step. - Add an
entrypoint
field to use thebash
tool whencloud-sdk
is invoked. In the
args
field, invoke thegcloud app deploy
command and set atimeout
for App Engine to use when it invokes Cloud Build. This is required because Cloud Build build steps and builds have a default timeout of 10 minutes and App Engine deployments could take longer than that to complete. Specifying a longer timeout will make sure that the build doesn't timeout ifgcloud app deploy
takes longer than 10 minutes to complete.Timeout errors when using the App Engine standard environment: You can configure timeouts as described here only when using the App Engine flexible environment. The App Engine standard environment does not allow the build timeout to be configured. If you're using Cloud Build for deploying on the App Engine standard environment, and your build is failing with a timeout error, consider using the App Engine flexible environment or Cloud Run instead of the App Engine standard environment.
Add a build
timeout
value of more than 10 minutes.
YAML
steps: - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk' entrypoint: 'bash' args: ['-c', 'gcloud config set app/cloud_build_timeout 1600 && gcloud app deploy'] timeout: '1600s'
JSON
{ "steps": [ { "name": "gcr.io/google.com/cloudsdktool/cloud-sdk", "entrypoint": "bash", "args": [ "-c", "gcloud config set app/cloud_build_timeout 1600 && gcloud app deploy" ] } ], "timeout": "1600s" }
- Add a
Start the build, where
SOURCE_DIRECTORY
is the path or URL to the source code andREGION
is one of the supported build regions to start the build:gcloud builds submit --region=REGION SOURCE_DIRECTORY
Continuous deployment
You can automate the deployment of your software to App Engine by creating Cloud Build triggers. You can configure your triggers to build and deploy images whenever you update your source code.
To automate your deployment to App Engine:
In your repository, add a config file with steps to invoke the
gcloud app deploy
command:YAML
steps: - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk' entrypoint: 'bash' args: ['-c', 'gcloud config set app/cloud_build_timeout 1600 && gcloud app deploy'] timeout: '1600s'
JSON
{ "steps": [ { "name": "gcr.io/google.com/cloudsdktool/cloud-sdk", "entrypoint": "bash", "args": [ "-c", "gcloud config set app/cloud_build_timeout 1600 && gcloud app deploy" ] } ], "timeout": "1600s" }
Create a build trigger with the config file created in the previous step:
Open the Triggers page in the Google Cloud console:
Select your project from the project selector drop-down menu at the top of the page.
Click Open.
Click Create trigger.
On the Create trigger page, enter the following settings:
Enter a name for your trigger.
Select the repository event to start your trigger.
Select the repository that contains your source code and build config file.
Specify the regex for the branch or tag name that will start your trigger.
Configuration: Choose the build config file you created previously.
Click Create to save your build trigger.
Anytime you push new code to your repository, you will automatically start a build and deploy on App Engine.
For more information on creating Cloud Build triggers, see Creating and managing build triggers.
What's next
- Learn how to deploy on Cloud Run
- Learn how to perform blue/green deployments on Compute Engine
- Learn how to deploy on GKE
- Learn how to deploy on Cloud Run functions
- Learn how to deploy on Firebase
- Learn how to troubleshoot build errors.