# Writing a step

> Publish a composite step or a typed step for other repositories to use.
> **Available** in repositories branchd reads pipeline files for. It is switching repositories on one at a time: until yours is, it keeps running its `.forge-ci.yaml` or `.xeonr-ci.yaml`. See [What is available](https://docs.branchd.run/branchd/pipelines#what-is-available).

## A composite step for others

A composite step needs no build. Put it in a directory of a repository, list
the directory in `.branchd/packages.yaml`, and tag a version.

```yaml title="xeonr/forge/steps: .branchd/packages.yaml"
# xeonr/forge/steps: .branchd/packages.yaml
# What other repositories may use from xeonr/forge/steps. It is read at the head
# of the default branch, never at the commit being used.
packages:
  - setup-buf
  - .branchd/pipelines/deploy.yaml
```

```yaml title="xeonr/forge/steps: setup-buf/step.yaml"
# xeonr/forge/steps: setup-buf/step.yaml
# Used from another repository as xeonr/forge/steps/setup-buf@v1.
name: Set up buf
description: Installs buf.
inputs:
  version: {type: string, default: '1.56.0'}
outputs:
  path: ${{ steps.install.outputs.path }}
steps:
  - id: install
    run: |
      curl -fsSLo /usr/local/bin/buf \
        "https://github.com/bufbuild/buf/releases/download/v${{ inputs.version }}/buf-$(uname -s)-$(uname -m)"
      chmod +x /usr/local/bin/buf
      echo "path=/usr/local/bin/buf" >> "$BRANCHD_OUTPUT"
```

1. **Lay it out.** One directory per step, holding `step.yaml`
   (`setup-buf/step.yaml`). The directory's path is the step's name in a
   reference: `xeonr/forge/steps/setup-buf@v1`. Its keys are those of a
   [composite step](https://docs.branchd.run/branchd/pipelines/composite-steps).
2. **List it** in `.branchd/packages.yaml` on the default branch. Pipeline
   files declaring `on: call` are listed by their path the same way.
3. **Tag it** `vX.Y.Z`. Consumers write `@v1` and get your newest `v1.x.y`
   when their next run is compiled, so a new tag reaches them without their
   changing anything. Break compatibility only in a new major.
4. **Set the repository's visibility** to who should use it: company or
   organisation for a shared step repository. A private repository's steps
   are usable by that repository alone.
5. **Test it** from a second repository with `branchd ci plan`, which reads a
   sibling checkout's tags and commits (never its working tree): commit and
   tag in the step repository's clone first.

Keep a published step honest about what it touches: it runs with the
caller's secrets and token, and callers read it before they use it.

## A typed step

A typed step is one program that branchd runs in its own image. It reads
typed inputs, writes typed outputs, can ask for a token for one Xeonr
product, and fails with an error or an exit code. It is written in Go on the
branchd SDK (`branchd.run/branchd/sdk`, MIT licensed, from the public
repository [`branchd/sdk`](https://branchd.run/branchd/sdk)), the same SDK
branchd's own steps are written on. Start from
[`branchd/template-step`](https://branchd.run/branchd/template-step): one
step, its tests and the CI that publishes it.

### Write it

Its directory holds the Go main package and a manifest version 2
`step.yaml`:

```yaml title="steps/hello/step.yaml"
# steps/hello/step.yaml
# A typed step (manifest v2): one program, built from the Go main package in
# this directory, run in its own image. Its registry name comes from the
# repository and directory, so the file names none.
manifest: 2
name: Hello
description: Greets someone and says how long the greeting was.
runs:
  main: .
  platforms: [linux/amd64, linux/arm64]
source: true
inputs:
  who:
    type: string
    default: world
    description: Who to greet.
outputs:
  length:
    type: int
    description: The greeting's length in bytes.
secrets:
  GREETING_TOKEN:
    optional: true
    description: Not needed to greet; shown here to declare a secret.
```

[Step and package files](https://docs.branchd.run/branchd/pipelines/step-files#stepyaml-a-typed-step)
lists every key. The file names no registry package: branchd names it from
your repository and the directory.

```go title="steps/hello/main.go"
package main

import (
	"context"

	"branchd.run/branchd/sdk/step"
)

type inputs struct {
	Name string `step:"name,required"`
}

func main() { step.Run(hello) }

func hello(ctx context.Context, s *step.Step, in inputs) error {
	s.Logf("hello %s", in.Name)
	return s.Output("greeting", "hello "+in.Name)
}
```

`go get branchd.run/branchd/sdk@latest` adds the SDK. A default is written in
`step.yaml` only: the build links it into the binary.

### Test it

- **In process**: `steptest.NewAttempt(t, inputs)` and `steptest.Run(a, fn)`
  run the step's function against fixtures (inputs, staged files, tokens)
  and return its outputs, log and error.
- **Every manifest**: `steptest.CheckManifests(t, ".", false)` loads each
  `step.yaml` as the release does, so a manifest the publish would refuse
  fails a pull request.
- **The built binary**: `branchd ci step run steps/hello --with name=Ada`
  builds it for your machine and runs it once in an environment like the
  sandbox's, with only the secrets you give (`--secret-env NAME`).

### Release it

1. **List** the directory in `.branchd/packages.yaml` on the default branch.
2. **Protect** the default branch and `v*` tags.
3. **Ask for a step grant** if it needs `network`, `privileged` or
   `host_runtime`, or takes secrets it does not declare
   (`accepts_any_secret`). An administrator of your repository's company
   grants `network` and `any_secret`; `privileged` and `host_runtime` are an
   instance administrator's. Publishing refuses a step that declares more
   than its repository's grant.
4. **Add a release pipeline** that runs on `v*` tags, with
   `permissions: {packages: write}`:

   ```yaml title=".branchd/pipelines/release.yaml"
   name: Release
   on:
     push:
       tags: ['v*']
   permissions: {packages: write}
   jobs:
     publish:
       image: golang:1.26.4
       steps:
         - run: go run branchd.run/branchd/sdk/cmd/publish step steps/hello
   ```

   For each directory, branchd reads `step.yaml` itself, at the tag, holds
   it to the grant, and gives a push login on the package registry; the
   step is built for its platforms and its image pushed, and branchd records
   the version with your repository and commit as its source. Only a
   protected `vX.Y.Z` tag at the run's commit publishes, once per version.
5. **Tag** `vX.Y.Z`, and use it from another repository as
   `<team>/<repo>/<dir>@v1`: `@v1` takes your newest published `v1.x.y`.

A step's grant and its secrets are its caller's to see: a job that lists
`secrets:` narrows what the step gets, and a required secret it leaves out
is refused at compile, naming it.

## Getting it listed

A published step is listed in [Steps & presets](https://docs.branchd.run/branchd/pipelines/steps-and-presets)
for whoever can read your repository's code, with no submission. Give it a
human `name:`, a `README.md` beside `step.yaml` with a `uses:` example, and a
description on every input and secret. `branchd ci step lint` shows what it
lacks.

Source: https://docs.branchd.run/branchd/pipelines/writing-a-step
