Deploying to App Engine

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:

    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

  1. In the Google Cloud console, select your project.

  2. 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.

    Go to Service Accounts

  3. Grant the App Engine Admin role and Service Account User to the build service account:

    1. Open the Cloud Build Settings page:

      Open the Cloud Build Settings page

    2. 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:

  1. Create a Cloud Build configuration file named cloudbuild.yaml or cloudbuild.json.

  2. In the config file:

    • Add a name field to specify the cloud-sdk build step.
    • Add an entrypoint field to use the bash tool when cloud-sdk is invoked.
    • In the args field, invoke the gcloud app deploy command and set a timeout 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 if gcloud 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"
    }
    
  3. Start the build, where SOURCE_DIRECTORY is the path or URL to the source code and REGION 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:

  1. 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"
    }
    
  2. Create a build trigger with the config file created in the previous step:

    1. Open the Triggers page in the Google Cloud console:

      Open Triggers page

    2. Select your project from the project selector drop-down menu at the top of the page.

    3. Click Open.

    4. Click Create trigger.

      On the Create trigger page, enter the following settings:

      1. Enter a name for your trigger.

      2. Select the repository event to start your trigger.

      3. Select the repository that contains your source code and build config file.

      4. Specify the regex for the branch or tag name that will start your trigger.

      5. Configuration: Choose the build config file you created previously.

    5. 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