# Build a Pipeline with AI

> Have an AI assistant write and check a Mélodium pipeline.

Web page: https://docs.cadence.ci/en/with-ai/build-pipeline

Once the [Mélodium MCP server is installed](https://docs.cadence.ci/en/with-ai/install-mcp.md), you can ask an AI assistant to write or adapt a CI/CD pipeline for your project, with the assistant validating its own work against the server as it goes instead of guessing. Installing the [Mélodium skill](https://docs.cadence.ci/en/with-ai/install-skill.md) alongside it is optional but still useful, it keeps the same reference material loaded in context for tools that don't call MCP servers reliably on every turn. Two approaches work well: starting from nothing, or adapting the official CI/CD template.

## From Scratch

Use this when you have no existing Mélodium project and want the AI to generate everything from a plain description.

### Describe Your Pipeline

Tell the AI what your project needs in plain terms, you don't need to know any Mélodium syntax. A useful prompt covers what the project is (language, build tool, test framework), what steps the pipeline runs and in what order, what container image each step needs, whether steps can run in parallel, and whether one step produces an artifact the next step consumes.

**Example:**

```text
Create a Mélodium CI/CD project for a Rust application.
The pipeline should have two steps:
1. Build: run "cargo build --release" in a rust:latest container, output the binary.
2. Test: run "cargo test" in a rust:latest container, in parallel with the build.

Use simpleStep for both. Wire them so build and test run in parallel,
both triggered by startup.
Generate a complete project with Compo.toml and a main.mel file.
Use location="compose" for local testing.
```

### Review the Generated Project

The AI will produce a `Compo.toml` and one or more `.mel` files. If it has MCP access, ask it to run `check_program` on them first, it validates syntax and lists entrypoints instantly, well before you run anything. Also check that `Compo.toml` declares `std`, `cicd`, `process`, and `work` at minimum; that the `->` wiring actually reflects the order and parallelism you described; and that a `main` treatment is declared as an entrypoint in `Compo.toml`. Also look at the `cpu`, `memory`, and `storage` values in `simpleStep`, the defaults rarely match your real workload, so adjust them.

### Run Locally

Ask the AI to write the files directly, or copy the generated content into a new project folder. Then run:

```sh
melodium .melodium-ci/Compo.toml
```

With `location="compose"` in the `CicdDispatchEngine`, Mélodium uses Podman Compose or Docker Compose to spin up runners locally, so no Cadence.CI account is needed for this first test.

> [!NOTE]
>
> The container runtime must be running. This is the default with Docker. On Podman, start the service first: `podman system service --time 0 unix:///run/user/1000/podman/podman.sock`

---

## From the CI/CD Template

Use this when you'd rather start from a working scaffold and have the AI adapt it to your project, instead of generating everything from scratch.

### Create the Template Project

At the root of your repository:

```sh
melodium new --template cicd --path .melodium-ci cicd
```

This creates `.melodium-ci/` with a `Compo.toml`, a `lib-root.mel` with working `build` and `test` treatments, and an `advanced.mel` with a more explicit runner setup. The template is already a valid CI/CD program, the AI's job is just to adapt it to your project.

### Share the Template and Describe Your Needs

Open your AI conversation, attach `.melodium-ci/lib-root.mel`, and describe what your project needs. The AI will rewrite the `build` and `test` treatments, and add others, to match.

**Example:**

```text
Here is my Mélodium CI/CD template (lib-root.mel).
My project is a Node.js application.

Update the pipeline to:
1. Install dependencies with "npm ci" in a node:20 container.
2. Run "npm run build" and "npm test" in parallel after install completes.
3. If both succeed, run "npm run deploy" in a node:20 container.

Keep the same entrypoint parameters. Use simpleStep throughout.
```

**Example with artifact passing:**

```text
Update the pipeline so the build step compiles a Go binary
and streams it directly to the test step, which runs integration tests
against the binary without recompiling.
Use out_file in the build step and simpleStepWithInput in the test step.
```

### Review the Changes

Read through the modified `.mel` file before running it. Make sure the `->` wiring matches the order you described, that steps sharing the same `trigger` source are meant to run concurrently, and that artifact file names line up: if one step uses `out_file` and the next uses `in_file`, the names need to match. The template ships with small resource defaults (`cpu=100`, `memory=500`) and container images that were picked for the example, both are worth revisiting for your workload.

Ask the AI to validate syntax through the MCP server's `check_program` tool at any point, or run it yourself:

```sh
melodium check .melodium-ci/lib-root.mel
```

### Run and Iterate

```sh
export MELODIUM_API_TOKEN=<YOUR_RUN_TOKEN>
melodium run --api-report .melodium-ci/Compo.toml \
  --repository_clone_url <FULL_CLONE_URL>.git \
  --repository_clone_ref main
```

The steps run on machines Cadence.CI starts in your connected cloud account. Or with local distribution, no cloud account needed:

```sh
export MELODIUM_API_TOKEN=<YOUR_RUN_TOKEN>
melodium run --api-report .melodium-ci/Compo.toml \
  --work_location compose \
  --repository_clone_url <FULL_CLONE_URL>.git \
  --repository_clone_ref main
```

---

## Iterating with the AI

In both approaches, keep asking the AI to refine the pipeline as you go. Some prompts that work well:

- *"Add a deploy step that only runs if both build and test succeed."*
- *"Make the build step stream the binary to the test step instead of rebuilding."*
- *"Add a lint step that runs in parallel with the build."*
- *"Add a step that always runs, even if the build fails."*
- *"Switch to the advanced runner setup so I can control container resources individually."*
- *"Check this with the MCP server before showing it to me."*

The AI can read and modify the `.mel` files it generated. For the advanced runner setup (`setupRunner` / `stepOn` / `stopRunner`), ask it to rewrite using `advanced.mel` as the base, the MCP server's `get_language_guide` tool (and the skill, if installed) has the full reference for those treatments.

You can always validate the result without running the full pipeline, either by asking the AI to call `check_program` on it, or directly:

```sh
melodium check .melodium-ci/lib-root.mel
```

## Going Further

Once the pipeline works locally, connect it to Cadence.CI and your Git service:

- [Run on GitHub Actions](https://docs.cadence.ci/en/github/run-on-github.md)
- [Run on GitLab CI](https://docs.cadence.ci/en/gitlab/run-on-gitlab.md)
- [Advanced pipeline patterns](https://docs.cadence.ci/en/from-template/advanced-implementation.md)
