# GitLab CI

Start a Hecaton workspace from a trusted merge request pipeline and read its result in the job.

## Configure the project

1. Register the repository as a Hecaton project. Create a [CI token](tokens.md) limited to that project.
2. Under **Settings → CI/CD → Variables**, add `HECATON_TOKEN` as a masked and protected variable. Add `HECATON_PROJECT_ID` and `HECATON_CLI_VERSION` with the project ID and a published CLI version.
3. Allow protected resources in merge request pipelines. The source and target branches must both be protected and belong to the same project. The person triggering the pipeline needs permission to push or merge into the target branch. Run the job only with trusted pipeline configuration. [GitLab merge request pipelines](https://docs.gitlab.com/ci/pipelines/merge_request_pipelines/#control-access-to-protected-variables-and-runners).

## Add the job

Add the following to `.gitlab-ci.yml`.

```yaml
hecaton:
  image: node:24
  timeout: 20m
  variables:
    GIT_STRATEGY: none
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_SOURCE_PROJECT_ID == $CI_PROJECT_ID && $CI_MERGE_REQUEST_TARGET_PROJECT_ID == $CI_PROJECT_ID && $CI_MERGE_REQUEST_SOURCE_BRANCH_PROTECTED == "true" && $CI_MERGE_REQUEST_TARGET_BRANCH_PROTECTED == "true"'
  script:
    - ': "${HECATON_TOKEN:?}"'
    - npm install --global --ignore-scripts "@hecaton-io/cli@${HECATON_CLI_VERSION:?}"
    - |
      if printf '%s\n' '{"resultContract":{"fields":{"summary":{"type":"string"}}}}' |
        hecaton workspace create --body-file - --project "${HECATON_PROJECT_ID:?}" --branch "${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME:?}" \
        --prompt "Review this branch and publish a summary. Do not edit the code." \
        --idempotency-key "gitlab-${CI_PROJECT_ID}-${CI_PIPELINE_ID}" \
        --wait --timeout 900 --json > hecaton.jsonl; then
        head -n 1 hecaton.jsonl
        tail -n 1 hecaton.jsonl
      else
        status=$?
        head -n 1 hecaton.jsonl
        tail -n 1 hecaton.jsonl
        exit "$status"
      fi
```

## Read the result

The first line identifies the workspace after admission. The last line contains its result or the CLI error. Use the workspace ID to continue in Hecaton if the job exits with a pending question or times out. Retrying a job in the same pipeline reuses the idempotency key.

See [CLI exit codes](cli.md) and [answering a pending question](local-agent.md).
