GoogleCloudPlatform / cloud-builders-community

Community-contributed images for Google Cloud Build
https://cloud.google.com/cloud-build/
Apache License 2.0
1.25k stars 851 forks source link

Add a Cloud Builder for CUE CLI #620

Open mmizutani opened 1 year ago

mmizutani commented 1 year ago

This PR introduces a Cloud Builder for CUE, a data constraint language for effective management of configurations, schemas, and data.

The CUE CLI is useful when, for instance, generating YAML resource definitions for Kubernetes and Google Cloud Run from type-safe templates. Its functionalities are more generic than those of kustomize, HCL (Terraform) and ksonnet.

A template file written in the CUE language can generate variations of YAML files based on input variables:

# template.cue

_env: "dev" | "stg" | "prd" @tag(env)
_project: =~"^my-project-[a-z]$" @tag(project)
_commit_hash: =~"^[0-9a-z]{7}$" | *_env @tag(commit_hash)

apiVersion: "serving.knative.dev/v1"
kind:       "Service"
metadata: {
  name: "myservice-\(_env)"
}
spec: {
  template: {
    spec: {
      containers: [{
        name:  "nginx"
        image: "us-docker.pkg.dev/\(_project)/myrepo-\(_env)/nginx:\(_commit_hash)"
      }]
    }
  }
}
$ cue eval template.cue -t env=dev -t project=my-project-a -t commit_hash=$(git rev-parse --short HEAD -- .) --out yaml 

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: myservice-dev
spec:
  template:
    spec:
      containers:
        - name: nginx
          image: us-docker.pkg.dev/my-project-a/myrepo-dev/nginx:1cdcc3e

An example usage of this Cloud Builder is as follows:

steps:
- name: 'gcr.io/${PROJECT_ID}/cue'
  args: ['--help']