Building repositories from GitHub

Stay organized with collections Save and categorize content based on your preferences.

GitHub triggers enable you to automatically build on Git pushes and pull requests and view your build results on GitHub and Google Cloud console. Additionally, GitHub triggers support all the features supported by the existing GitHub triggers and use the Cloud Build GitHub app to configure and authenticate to GitHub.

This page explains how to create GitHub triggers and build on GitHub using the Cloud Build GitHub app.

Before you begin

  • Enable the Cloud Build API.

    Enable the API

Creating a GitHub trigger

Console

To create GitHub triggers using the Google Cloud console:

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

    Open the 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.

  5. Enter the following trigger settings:

    • Name: Enter a name for your trigger.

    • Region: Select the region for your trigger.

      • If you select global as the region, Cloud Build uses the default pool to run your build.
      • If you select a non-global region and the build config file associated with the trigger specifies a private pool, Cloud Build uses the private pool to run your build. In this case, the region you specify in your trigger must match the region where you created your private pool.
      • If you select a non-global region and the build config file associated with the trigger does not specify a private pool, Cloud Build uses the default pool to run your build in the same region as your trigger.
    • Description (optional): Enter a description for your trigger.

    • Event: Select the repository event to invoke your trigger.

      • Push to a branch: Set your trigger to start a build on commits to a particular branch.

      • Push new tag: Set your trigger to start a build on commits that contain a particular tag.

      • Pull request (Cloud Source Repositories not supported): Set your trigger to start a build on commits to a pull request.

    • Source: Select the repository and the corresponding branch or tag to watch for events.

      • Repository: From the list of available repositories, select the desired repository. To connect a new repository, see Connecting additional repositories.

      • Branch or Tag: Specify a regular expression with the branch or tag value to match. For information on acceptable regular expression syntax, see RE2 syntax.

      • Comment control: If you selected Pull request (GitHub App only) as your Event, choose one of the following options to control whether a build will automatically be executed by the trigger:

        • Required except for owners and collaborators: When a pull request is created or updated by a repository owner or collaborator, builds will automatically be executed by the trigger. If an external contributor initiates the action, builds will only be executed after an owner or collaborator comments /gcbrun on the pull request.

        • Required: When a pull request is created or updated by any contributor, builds will only be executed after an owner or collaborator comments /gcbrun on the pull request. Builds are executed each time a change to a pull request is made.

        • Not required: When a pull request is created or updated by any contributor, builds will automatically be executed by triggers.

    • Included files (optional): Changes affecting at least one of these files will invoke a build.

    • Ignored files (optional): Changes only affecting ignored files will not invoke a build.

    • Configuration: Select the build config file located in your remote repository or create an inline build config file to use for your build.

      • Type: Select the type of configuration to use for your build.
        • Autodetected: Cloud Build autodetects your configuration type if you have a cloudbuild.yaml or Dockerfile in your repository.
        • Cloud Build configuration file (yaml or json): Use a build config file for your configuration.
        • Dockerfile: Use a Dockerfile for your configuration.
        • Buildpacks: Use buildpacks for your configuration.
      • Location: Specify the location for your configuration.

        • Repository: If your config file is located in your remote repository, provide the location of your build config file or the Dockerfile directory and a name for the resulting image. If your configuration is a Dockerfile, you can optionally provide a timeout for your build. When you've provided the Dockerfile and image name, you'll see a preview of the docker build command that your build will execute.
        • Inline: If you selected Cloud Build configuration file (yaml or json) as your configuration option, you can specify your build config inline. Click Open Editor to write your build config file in the Google Cloud console using YAML or JSON syntax. Click Done to save your build config.
    • Substitution variables (optional): If you selected thCloud Build config file as your build config option, you can choose to define trigger-specific substitution variables using this field. For example, say you're creating multiple triggers where each trigger deploys your app to a specific environment. You can specify that your app is deployed to an environment in your build config file and then use this field to define substitution variables specifying which environment this trigger should deploy to. For information on specifying substitution values in build config files, see Substituting variable values.

    • Approval (optional): Check the box to require approval before your build executes. To learn more about approvals, see Gate builds on approval.

    • Build logs (optional): Check the box to send build logs to GitHub. To learn how to view build logs, see Viewing build logs.

    • Service account: Select the service account to use when invoking your trigger. If you do not select a service account, the default Cloud Build service account is used.

  6. Click Create to save your build trigger.

To create GitHub triggers using gcloud commands, see the gcloud commands for Creating a build trigger.

gcloud

To create GitHub triggers using gcloud commands, run the following command:

gcloud beta builds triggers create github \
    --repo-name=REPO_NAME \
    --repo-owner=REPO_OWNER \
    --branch-pattern=BRANCH_PATTERN \ # or --tag-pattern=TAG_PATTERN
    --build-config=BUILD_CONFIG_FILE \
    --include-logs-with-status

Where:

  • REPO_NAME is the name of your repository.
  • REPO_OWNER is the username of the repository owner.
  • BRANCH_PATTERN is the branch name in your repository to invoke the build on.
  • TAG_PATTERN is the tag name in your repository to invoke the build on.
  • BUILD_CONFIG_FILE is the path to your build configuration file.
  • [OPTIONAL] --include-logs-with-status is a flag you can specify to show build logs for your repositories. This flag is supported for builds from GitHub and GitHub Enterprise repositories. To learn how to view build logs, see Viewing build logs.

Building and viewing your changes

To build using GitHub triggers, you'll need to push and commit changes to your connected source repository or configure your build on pull requests. Once you have checked in your changes, Cloud Build will build your code.

To view your build changes on GitHub, go to the Checks tab in your repository.

Screenshot of the conversation tab

You'll see that Cloud Build has built your changes. You'll also see other build details such as the time it took to build your code and the build ID.

To view your build changes in Cloud Build, click on View more details on Google Cloud Build. The Build details page in Google Cloud console opens where you can see build information such as status, logs, and build steps.

Different types of GitHub-based triggers

If your source code is in GitHub, Cloud Build provides two ways by which you can automatically execute builds. This section explains the two GitHub-based triggers and compares their features.

  • GitHub legacy triggers: When you create a GitHub legacy trigger, Cloud Build mirrors your GitHub repository in Cloud Source Repositories and uses the mirrored repository for all its operations. You can create and manage GitHub triggers using the Google Cloud console.

  • GitHub triggers: This type of trigger uses the Cloud Build GitHub app to configure and authenticate to GitHub. GitHub triggers allow you to automatically start builds on Git pushes and pull requests and view build results on GitHub and the Google Cloud console. You can create and manage GitHub triggers using Google Cloud console or the Cloud Build API, as described on this page.

  • GitHub Enterprise triggers: This type of trigger enables you to invoke builds in response to commits or pull requests on a GitHub Enterprise instance. You can build repositories from GitHub Enterprise using the Google Cloud console or the Cloud Build API.

The following table compares GitHub legacy triggers, GitHub triggers, and GitHub Enterprise triggers:

Feature GitHub legacy triggers GitHub triggers GitHub Enterprise triggers
Execute builds on pushes to the source code Yes Yes Yes
Execute builds on pull requests No Yes Yes
Create trigger using Google Cloud console Yes Yes Yes
Create trigger using the Cloud Build API No Yes Yes
Create trigger using the Cloud Build GitHub app No Yes Yes
View build status on Google Cloud console Yes Yes Yes
View build status on GitHub No Yes Yes

Data sharing

GitHub triggers send data to the Cloud Build GitHub app. The data sent to the app helps you identify triggers by name and see build results on GitHub.

The following data is currently shared between Google Cloud and GitHub app:

  • Cloud project ID
  • Trigger name
  • Build logs

If you created triggers prior to August 2020, data sharing may not be enabled for your project. You can enable data sharing for all GitHub triggers in your project by clicking Enable on the Cloud Build Data sharing tab.

If you have required status checks enabled for a GitHub repository, enabling data sharing may temporarily break status checks. You can adjust status check configurations to look for your trigger name by:

  • Disabling any Cloud Build-specific required checks on the GitHub repository
  • Ensuring that data sharing is enabled in Cloud Build
  • Executing a new build in Cloud Build that posts statuses to your repository
  • Re-enabling required status checks, selecting trigger name

What's next