Getting started with GitLab CI/CD + CUE

The CUE Central Registry provides a well-known location for well-known schemas, including those for YAML pipeline files used by GitLab CI/CD. This guide shows you how to get started defining your GitLab CI/CD pipelines in CUE using a curated module from the schema library.

Initialize your local CUE module

CUE that uses schemas and modules from the Central Registry needs to exist within its own CUE module.

TERMINAL
$ cue mod init cue.example

You can choose any module name you like, and change it later. It makes sense for your CUE module to exist at the root of a git repository that’s hosted on GitLab. The commands in this guide work in any setup.

Create a pipeline

Declare a GitLab pipeline in CUE. This one is based on an example from GitLab’s documentation:

pipeline.cue
package cicd

import "cue.dev/x/gitlab/gitlabci"

pipelines: example: gitlabci.#Pipeline & {
	default: image: "ruby:3.2"
	workflow: rules: [{if: "$CI_COMMIT_BRANCH"}]
	"deploy-pages": {
		stage: "deploy"
		script: [
			"gem install bundler",
			"bundle install",
			"bundle exec jekyll build -d public",
		]
		pages: true
		rules: [{if: "$CI_COMMIT_BRANCH == \"main\""}]
		environment: "production"
	}
	test: {
		stage: "test"
		script: [
			"gem install bundler",
			"bundle install",
			"bundle exec jekyll build -d test",
		]
		artifacts: paths: ["test"]
		rules: [{if: "$CI_COMMIT_BRANCH != \"main\""}]
	}
}

In later guides we’ll add more entries to the pipelines struct.

The import at the top references the appropriate curated module for the pipeline. Its path is currently temporary, but only while its proper location is being decided. The temporary path isn’t a problem: once a schema is published to the Central Registry, it is always available at that location. When versions are published at the curated module’s final location, use the cue refactor imports command to update your CUE.

Tidy your local CUE module

TERMINAL
$ cue mod tidy

Tidying a module is part of using curated modules from the Central Registry. Always use cue mod tidy when you use a curated module for the first time.

If you see an error message mentioning “too many requests” then login to the Central Registry and re-run this command. The Central Registry allows more requests from authenticated users.

Validate your pipeline

TERMINAL
$ cue vet -c

Because cue vet doesn’t display any errors, you know that the curated schema has validated your pipeline.

Export your pipeline as YAML

TERMINAL
$ cue export --outfile .gitlab-ci.yml -e pipelines.example

If you chose to export the pipelines.example shown above, your validated YAML pipeline will look like this:

.gitlab-ci.yml
default:
  image: ruby:3.2
workflow:
  rules:
    - if: $CI_COMMIT_BRANCH
deploy-pages:
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
  script:
    - gem install bundler
    - bundle install
    - bundle exec jekyll build -d public
  stage: deploy
  environment: production
  pages: true
test:
  rules:
    - if: $CI_COMMIT_BRANCH != "main"
  script:
    - gem install bundler
    - bundle install
    - bundle exec jekyll build -d test
  stage: test
  artifacts:
    paths:
      - test

Run your pipeline

The cue.mod directory needs to be stored in your git repository, along with your pipeline.cue and .gitlab-ci.yml files. After recording them in a commit you can push your branch to GitLab and trigger the pipeline.

Whenever you update your CUE pipeline, re-run the cue export command shown above. Then use git to record any changes to these files and directories.

Install CUE