Getting started with GitHub Actions + CUE
The CUE Central Registry provides a well-known location for well-known schemas, including those for YAML workflow files used by GitHub Actions. This guide shows you how to get started defining your GitHub Actions workflows 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.
$ cue mod init cue.exampleYou 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 GitHub. The commands in this guide work in any setup.
Create a workflow
Declare a GitHub Actions workflow in CUE. This one is based on an example from GitHub’s documentation:
package cicd
import "cue.dev/x/githubactions"
workflows: example: githubactions.#Workflow & {
name: "learn-github-actions"
"run-name": "${{ github.actor }} is learning GitHub Actions"
on: ["push"]
jobs: "check-bats-version": {
"runs-on": "ubuntu-latest"
steps: [
{uses: "actions/checkout@v4"},
{uses: "actions/setup-node@v4", with: "node-version": "20"},
{run: "npm install -g bats"},
{run: "bats -v"},
]
}
}
In later guides we’ll add more entries to the workflows struct.
The import at the top references the appropriate curated module for the workflow.
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
$ cue mod tidyTidying 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 workflow
$ cue vet -cBecause cue vet doesn’t display any errors, you know that the curated schema has validated your workflow.
Export your workflow as YAML
Before exporting your workflow you’ll need to create a directory to hold it, as expected by GitHub Actions:
$ mkdir -p .github/workflows/
$ cue export --outfile .github/workflows/workflow.yml -e workflows.exampleIf you chose to export the workflows.example shown above,
your validated YAML workflow will look like this:
name: learn-github-actions
jobs:
check-bats-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "20"
- run: npm install -g bats
- run: bats -v
"on":
- push
run-name: ${{ github.actor }} is learning GitHub ActionsRun your workflow
The cue.mod and .github directories need to be stored in your git
repository, along with your workflow.cue file.
After recording them in a commit you can push your branch to GitHub and trigger
the workflow.
Whenever you update your CUE workflow, re-run the cue export command shown above. Then use git to record any changes to these files and directories.
Next steps
CUE’s language features are often used to reduce boilerplate, so that important differences stand out when you’re defining multiple configuration files. Discover how CUE makes configurations clearer, more succinct, and easier to understand in DRYing up GitHub Actions workflows.