Continuous deployment from Git using Cloud Build

You can use Cloud Build to automate builds and deployments to Cloud Run by using Cloud Build trigger to automatically build and deploy your code whenever new commits are pushed to a given branch of a Git repository.

When you use a Cloud Build trigger to build containers, the source repository information is displayed in the Cloud Console for your service after you deploy to Cloud Run.

Before you begin

  • You have a git repository with a Dockerfile.
  • Enable the Cloud Build and Cloud Source Repositories APIs.

    Enable the APIs

Setting up continuous deployment from the Cloud Run user interface

The procedure varies slightly depending on whether you are setting up continuous deployment on a new service or on an existing service. Click the appropriate tab to learn more.

New service

  1. Go to Cloud Run

  2. Click Create service.

  3. Fill the Service Settings with your preferences.

  4. Click Next.

  5. Select Continuously deploy new revisions from a source repository.

    Set up with Cloud Build

  6. Click Set up with Cloud Build.

  7. Select the provider and the repository.

    • If you want to use GitHub or Bitbucket as a repository provider and you are not yet authenticated, click Authenticate and follow the instructions.

    Set up Continuous Deployment step 1

  8. Click Next.

  9. Fill the options in Build Configuration step:

    • Branch - indicates what source should be used when running the trigger. You can put the regex here. Matched branches are automatically verified and you can see them below the input. Note that if exactly one branch is matched, the trigger will be automatically executed after the creation.
    • Source location - location and name of the Dockerfile. The directory will be used as the Docker build context. All paths should be relative to the current directory.

    Set up Continuous Deployment step 2

  10. Click Save.

  11. Verify the selected settings.

    Set up Continuous Deployment verification

  12. Click Create.

  13. Note that you are redirected to the Service Details page, where you can track the progress of your Continuous Deployment set up.

  14. Once all steps are completed, note additional options:

Existing service

  1. Go to Cloud Run

  2. Locate the service in the services list, and click on it.

  3. Click Set up Continuous Deployment.

  4. Select the provider and the repository.

    • If you want to use GitHub or Bitbucket as a repository provider and you are not yet authenticated, click Authenticate and follow the instructions.

    Set up Continuous Deployment step 1

  5. Click Next.

  6. Fill the options in Build Configuration step:

    • Branch - indicates what source should be used when running the trigger. You can put the regex here. Matched branches are automatically verified and you can see them below the input. Note that if exactly one branch is matched, the trigger will be automatically executed after the creation.
    • Source location - location and name of the Dockerfile. The directory will be used as the Docker build context. All paths should be relative to the current directory.

    Set up Continuous Deployment step 2

  7. Click Save.

  8. The page reloads and displays the progress of the Continuous Deployment setup.

  9. Once all steps are completed, note additional options:

Setting up continuous deployment manually

Refer to Settng up continuous deployment manually if you need to use a manual procedure and not the UI.

Attach existing Cloud Build trigger to Cloud Run service.

If you already have an existing Cloud Build trigger, you can attach it to the service and take advantage of Cloud Console features in the Service Details page: Edit Continuous Deployment button and Build History chart.

To do so, you have to add a label with gcb-trigger-id as a key and the unique identifier of the Cloud Build trigger as value (not the trigger name). See instructions for setting up the label.