Skip to content

CI with Gradle and Bazel

Audience: build and release engineers · about 15 minutes · Prerequisites: The strukts CLI and CLI exit codes and JSON.

What it is

The strukts CLI is the only thing a build system needs to know about: it takes a model folder, prints JSON, and sets an exit code. This page shows three ways to call it from a pipeline:

  1. a plain shell or CI-service job (works anywhere Node.js runs)
  2. the Gradle demo in tools/gradle-demo/
  3. the Bazel demo in tools/bazel-demo/, which builds the CLI itself inside Bazel

The Gradle and Bazel files in this repository are working demos wired to the models that ship with struktJS. To use the same pattern for your own models, copy the idea and change the project paths; the sections below say what to change.

What was verified

The exit codes, flags and JSON shown on this page were produced by running the CLI. The Gradle and Bazel files are the demos kept in this repository and are described from their source; the generic CI snippets in the first section are examples, not workflows that this repository's own CI runs.

Decide what should fail the build

You want Command Fails on exit code
Models must have no check errors strukts check --project-dir <dir> 3 (and 1, 2)
Warnings count too strukts check --project-dir <dir> --fail-on warning 3
Tests must pass strukts test --project-dir <dir> --junit build/junit.xml 5
Code must generate strukts generate --project-dir <dir> --output build/generated 4
All of the above plus a manifest strukts package --project-dir <dir> --output build/packages 3 or 4

Exit 1 (a model could not load) and 2 (bad command line) are always real failures. A report-only job accepts 0 and 3 so it can publish the findings without breaking the build; a gate accepts only 0.

A plain CI job

  1. Build the standalone CLI once and keep it as a build artifact (a folder, not a single file):

    npm ci
    npm run build:cli          # writes dist/strukts-cli/
    
  2. On any machine with Node.js 20 to 22, call it with node, no node_modules needed:

    node dist/strukts-cli/strukts.mjs check    --project-dir ./models/my-project  > build/check.json
    node dist/strukts-cli/strukts.mjs test     --project-dir ./models/my-project  --junit build/junit.xml
    node dist/strukts-cli/strukts.mjs package  --project-dir ./models/my-project  --output build/packages
    
  3. Let your CI service read the JUnit file (build/junit.xml) for its test dashboard, and archive build/check.json and build/packages/.

A GitHub Actions job following those steps (an example you adapt; this repository's own CI does not call the CLI):

jobs:
  models:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with: { node-version: 22.x, cache: npm }
      - run: npm ci
      - run: npm run build:cli
      - name: Check models (gate)
        run: node dist/strukts-cli/strukts.mjs check --project-dir models/my-project
      - name: Test models
        run: node dist/strukts-cli/strukts.mjs test --project-dir models/my-project --junit build/junit.xml
      - name: Package
        run: node dist/strukts-cli/strukts.mjs package --project-dir models/my-project --output build/packages
      - uses: actions/upload-artifact@v7
        with: { name: models, path: build/packages }

A GitLab CI job for the same steps (also an example to adapt, not run by this repository):

models:
  image: node:22
  script:
    - npm ci
    - npm run build:cli
    - node dist/strukts-cli/strukts.mjs check --project-dir models/my-project
    - node dist/strukts-cli/strukts.mjs test --project-dir models/my-project --junit build/junit.xml
  artifacts:
    when: always
    reports:
      junit: build/junit.xml

Jenkins and other services follow the same shape: build the CLI, run each verb as a shell step, publish build/junit.xml with the service's JUnit step, and let a non-zero exit code fail the stage. Which exit code means what is in CLI exit codes and JSON.

Notes:

  • GitHub Actions marks a step failed on any non-zero exit code, so the gate needs no extra logic. To make check a report instead, append || [ $? -eq 3 ] to the command.
  • npm run build:cli takes a while; cache dist/strukts-cli/ between runs keyed on the commit of struktJS you use.
  • jsdom is not part of the bundle. If your tests contain assertions that replay a keypress or an intention, run npm install jsdom in the directory you run from, or pass --skip-unsupported and accept that those assertions are reported as unsupported instead of checked. See Troubleshooting.
  • If a project carries its own *.lang.js, add --trust-project-code only for code your own team reviewed.

Gradle

tools/gradle-demo/build.gradle.kts defines one Exec task per verb. It runs node tools/strukts-cli/bin.mjs, so it needs only Node.js (no npx, no download), and it requires Gradle 9 and Java 17.

Run the tasks from tools/gradle-demo/:

Task What it runs Exit codes it accepts
struktsList list 0
struktsInfo info 0
struktsCheckPythonTour check --project python-tour 0 and 3 (the sample has deliberate findings)
struktsGenerateHello generate --project hello 0
struktsGenerateMultiRootDemo generate --project multi-root-demo 0
struktsGenerateKernelf generate --project kernelf-demo (writes .kf) 0
struktsGenerateMazeExplorer generate --project maze-explorer (writes .html) 0
struktsCheckAll check --all 0 and 3
struktsGenerateAll generate --all 0
struktsTestAll test --all --junit tools/gradle-demo/build/junit.xml 0 only: a failing test fails the build
struktsPackageHello package --project hello 0
struktsBuild all of the above

The pattern worth copying is the helper that honours exit codes instead of ignoring them:

fun struktsExec(name: String, allow: Set<Int>, verb: String, vararg extraArgs: String) =
    tasks.register<Exec>(name) {
        workingDir = repoRoot
        commandLine(listOf("node", struktsCli, verb) + extraArgs)
        isIgnoreExitValue = true
        doLast {
            val code = executionResult.get().exitValue
            if (code !in allow) throw GradleException("[$name] strukts exited with code $code (accepted: $allow)")
        }
    }

To use it for your own models:

  1. Point workingDir at the folder where the CLI lives and commandLine at either tools/strukts-cli/bin.mjs (a checkout) or dist/strukts-cli/strukts.mjs (the standalone build).
  2. Replace the --project <id> arguments with --project-dir <your folder>.
  3. In a real pipeline, give the check task setOf(0) instead of setOf(0, 3) so findings fail the build. The demo accepts 3 only because several shipped samples are deliberately illustrative.

The demo does not generate one Gradle task per project from strukts list; the ...All tasks cover the same ground with one CLI call each.

Bazel

tools/bazel-demo/ is a working Bazel setup (Bazel 9 through bazelisk) that is hermetic: it pins its own Node.js, and it builds the CLI bundle inside Bazel from the repository's sources, so a fresh clone needs no npm install and no npm run build:cli. A model target depends on that bundle, so changing an engine or plugin file re-runs the affected model actions.

The strukts_model macro

Each model folder becomes a few targets through the macro in tools/bazel-demo/defs.bzl:

load("//tools/bazel-demo:defs.bzl", "strukts_model")

strukts_model(name = "hello", dir = "samples/hello", out_files = ["hello/hello.py"])
strukts_model(name = "python_tour", dir = "samples/python-tour")
strukts_model(
    name = "decision_table",
    dir = "samples/decision-table",
    out_files = ["decision-table/decision-table.py", "decision-table/decision-table.tests.py", "decision-table/http-status-category.py", "decision-table/shipping-tiers.py", "decision-table/tax-brackets.py"],
    with_tests = True,
)
Argument Meaning Default
name Base name for the targets: <name>_check, <name>_generate, <name>_test required
dir The model folder, relative to the root package; its files are the action's inputs required
out_files The files generate will produce, written <project id>/<root>.<ext> where the id is the folder name. Bazel needs them named up front. Leave unset for a model whose model.json says "generatable": false none (no _generate target)
allow_findings True makes _check a report that succeeds on exit 3 and writes <name>.check.json; False makes it a gate that fails on any finding True
with_tests Also create <name>_test, which runs the suites and writes <name>.junit.xml; failing tests fail the build False

Build targets like this:

bazel build //:hello_check //:hello_generate
bazel build //:multi_root_demo_generate
bazel build //:python_tour_check            # exit 3 is accepted for a report target
bazel build //:decision_table_test         # writes bazel-bin/decision_table.junit.xml
bazel build //:kernelf_demo_generate        # .kf output
bazel build //:maze_explorer_generate       # .html output
bazel build //:strukts_cli_bundle           # just the CLI

Rules you must follow

  • Declare every model target in the root BUILD.bazel. A model folder must not have a BUILD.bazel of its own: a folder with one becomes a separate Bazel package and silently drops out of the bundle's embedded samples.
  • Exit codes are honoured. A report-style _check accepts exactly 0 and 3. Exit codes 1, 2, 4 and 5 fail the build, and so does 3 when allow_findings = False.
  • The .bazelrc uses a strict action environment, so the host PATH is not visible to the actions.
  • If the bundle ever needs a new npm package, add it with an exact version to tools/strukts-cli/build/package.json and regenerate the lock file: cd tools/strukts-cli/build && npx pnpm@9 install --lockfile-only --ignore-workspace.

Platforms

macOS (arm64) and Linux (arm64) have been verified. Windows is untested for the Bazel route. Linux x86_64 shares the arm64 resolution but was not run. If you need the lowest-effort route or Windows, use the Gradle demo or the plain CI job above, which only need Node.js.

Using this in your own repository

The demo's macro points at //:strukts_cli_bundle, a target built from struktJS's own sources, so it lives in the struktJS repository. For your own repository the simplest dependable approach is the standalone bundle: build dist/strukts-cli/ once (npm run build:cli), keep it as a pinned input (a checked-in folder or a fetched archive), and write your genrules to call node <bundle>/strukts.mjs <verb> --project-dir <dir> as defs.bzl does. Keep the exit-code handling from the macro (rc=0; ... || rc=$$?; test ...) because Bazel genrules run under set -e. Re-pointing the macro at an external bundle has not been tried in this repository.