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

Required IAM permissions

Grant the App Engine Admin role and Service Account User to the Cloud 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 tool 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.
    • 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:

     gcloud builds submit 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. Build 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