Setting up automated deployments

You can configure automated deployment for your workloads on Google Kubernetes Engine (GKE). This topic shows you how to set up a simple continuous integration/continuous delivery (CI/CD) pipeline for the workload. When you push a change to your repository, Cloud Build builds and deploys the container to your GKE cluster.

How automated deployment works

If you have a workload that references a single-container image in Kubernetes Engine, you can configure automated deployment, so that your application is re-built and deployed each time a code change is checked in to the repository.

With automated deployment, you define your workload as YAML and store that YAML in your repository (consistent with best practices). GKE generates YAML that you can use for this purpose.

  • If you don't yet have YAML that defines your workload, you can use the generated YAML.

    Save it in your repository. Cloud Build uses it to deploy your workload each time.

  • If you already have YAML that defines your workload, you can point GKE to its location in your repository.

    You can compare the YAML in your repo with the recommended YAML to ensure you're deploying the correct workload in the correct way.

Before you begin

Before you start, make sure you have performed the following tasks:

Set up default gcloud settings using one of the following methods:

  • Using gcloud init, if you want to be walked through setting defaults.
  • Using gcloud config, to individually set your project ID, zone, and region.

Using gcloud init

If you receive the error One of [--zone, --region] must be supplied: Please specify location, complete this section.

  1. Run gcloud init and follow the directions:

    gcloud init

    If you are using SSH on a remote server, use the --console-only flag to prevent the command from launching a browser:

    gcloud init --console-only
  2. Follow the instructions to authorize gcloud to use your Google Cloud account.
  3. Create a new configuration or select an existing one.
  4. Choose a Google Cloud project.
  5. Choose a default Compute Engine zone.

Using gcloud config

  • Set your default project ID:
    gcloud config set project project-id
  • If you are working with zonal clusters, set your default compute zone:
    gcloud config set compute/zone compute-zone
  • If you are working with regional clusters, set your default compute region:
    gcloud config set compute/region compute-region
  • Update gcloud to the latest version:
    gcloud components update
  • Make sure you have a workload that consists of a single container image.

    Workloads that reference multiple container images are not supported.

  • Make sure the container image is stored in Container Registry, in the same GCP project as your GKE cluster.

    You need to have the image in Container Registry, and you need to have deployed it at least once, so that you have a workload in GKE.

  • Your source-code repository must contain a Dockerfile to build the image.

    This repository can be hosted on one of the following:

    • Cloud Source Repositories
    • GitHub
    • Bitbucket If you use GitHub or Bitbucket, you need to grant GCP accesss to your repository.
  • Make sure the following additional APIs are enabled in Google Cloud Platform:

Create the deployment

To create a deployment with automated deployment, perform the following steps:

  1. On the GKE Workloads page, click the name of the workload you want to deploy.

    This opens the Deployment details page for the workload.

  2. Click the Actions menu, and select Automated deployment.

    The Set up automated deployment screen appears.

  3. Select the repository that contains your source code to build this workload.

    1. From the Repository Provider drop-down, choose the provider for your repository.

      automated deployment supports the following source control hosts:

      • Cloud Source Repositories
      • GitHub
      • Bitbucket

      If you are using GitHub or Bitbucket, authenticate with your username and password.

    2. From the Repository drop-down, select the name of the repository containing the source code for your workload.

  4. Click Continue and specify the Build configuration information:

    1. Enter the name of the branch from which you want to build, or a regular expression (RegExp2) to match the branch or branches. The default branch is master. When you push to this branch, Cloud Build builds your application.

    2. Specify the directory of your Dockerfile (relative to the root of the repository).

    3. Provide the file name of your Dockerfile containing your build information.

  5. Click Continue.

  6. Provide the location of the configuration YAML.

    The system automatically generates a set of YAML, which you can use to populate the YAML file in your repository.

    1. Click View Google-recommended YAML to see the generated YAML.

    2. Compare it to your own YAML, if you have it, and update yours based on this one.

      Otherwise, create a new file and populate it with the generated YAML. This helps ensure the workload that gets deployed matches what's currently running. The file can have any name, but must have a .yaml or .yml extension.

    3. Specify the directory where the YAML is located, relative to the root of your repository.

    If there is more than one YAML file in the specified directory, GKE uses all of them.

  7. Click Set up to finish configuring your automated deployment.

    You might be prompted to grant permissions to the Cloud Build service account. This is required.

What happens next?

Your automated deployment is now set up for the selected workload. This means…

  • You now have a Cloud Build trigger that builds a container image after a push to the selected branch of the repository.

  • Cloud Build deploys a new revision of your workload to the cluster where the original image was deployed.

    For more information on your workload, see the Deployment Details page (Revision History tab).