# Reuse a step (composite steps)

> Keep a list of steps with inputs in a file of its own, and use it from any job of the repository.
> **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).

The step, in `.branchd/steps/setup-buf/step.yaml`:

```yaml title=".branchd/steps/setup-buf/step.yaml"
# .branchd/steps/setup-buf/step.yaml
# A composite step: steps with inputs, expanded into the job that uses it.
name: Set up buf
description: Installs buf, and signs in to the Buf Schema Registry when asked.
inputs:
  version: {type: string, default: '1.56.0'}
  login: {type: bool, default: 'false'}
secrets:
  BUF_TOKEN: {required: false, description: 'A Buf Schema Registry token, for login.'}
outputs:
  path: ${{ steps.install.outputs.path }}
steps:
  - id: install
    name: 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"
  - name: Sign in
    if: inputs.login
    run: echo "$BUF_TOKEN" | buf registry login buf.build --token-stdin
```

The pipeline that uses it:

```yaml title=".branchd/pipelines/ci.yaml"
# .branchd/pipelines/ci.yaml
# Uses the composite step in .branchd/steps/setup-buf/ twice: once with its
# defaults, once with a version and a sign-in.
name: CI
on: [push, pull_request]
jobs:
  lint:
    image: golang:1.26
    steps:
      - uses: ./setup-buf
      - run: buf lint
  push:
    if: branch == 'main' && event == 'push'
    image: golang:1.26
    secrets: [BUF_TOKEN]
    steps:
      - id: buf
        uses: ./setup-buf
        with:
          version: '1.57.0'
          login: true
      - run: ${{ steps.buf.outputs.path }} push
```

- **`uses: ./<name>`** names `.branchd/steps/<name>/step.yaml` of this
  repository, at the run's commit. `<name>` may have several segments
  (`./go/setup` is `.branchd/steps/go/setup/step.yaml`).
- **Expanded when the run is compiled.** Its steps become the job's own, in
  the job's shell: a tool it installs or a variable it exports is there for
  the steps after it. The log labels them `<use's name>: <step's name>`
  (`Set up buf: Install`).
- **`step.yaml`** has `name`, `description`, `inputs`, `secrets`, `outputs` and
  `steps` (required). `steps` are `run:` steps, other composite steps and,
  marked `isolated: true`, typed steps.
- **Inputs** are `{type, default, required, description}`, with `type`
  `string` (the default), `bool` or `int`. An input nobody gives reads its
  default, else `''`, `false` or `0`. `with:` on the use gives them; an input
  the step does not declare is refused.
- **Secrets.** The step reads a secret from the environment (`$BUF_TOKEN`),
  so the job that uses it grants it in its own `secrets:`. A `required: true`
  secret the job does not grant is refused; one with `required: false` is
  optional, and the step must cope without it.
- **Outputs.** `outputs:` maps names to values, usually
  `${{ steps.<id>.outputs.<name> }}` of one of its steps. The job reads
  them through the use's `id`: `${{ steps.buf.outputs.path }}`.
- **The use takes** `id`, `name`, `uses`, `with`, `if`, `env`, `isolated`
  and `workspace` (see [Isolate a step](https://docs.branchd.run/branchd/pipelines/isolated-steps)). `if:` applies
  to all its steps; `env:` is given to each of them, and then each runs in a
  subshell, so its exports no longer reach the steps after it.
- **What its expressions can read:** the run's facts (`branch`, `event`, …),
  its own `inputs` and its own steps' outputs. The calling job's `matrix`,
  `needs`, `env` and `vars` are refused there: pass them in `with:`.
- **To share it** with other repositories, see
  [Steps and pipelines of another repository](https://docs.branchd.run/branchd/pipelines/other-repositories).
- **It is not isolated.** A composite step runs in the job's shell with
  everything the job has: its secrets, its job token and its workspace.
- **Limits.** `uses:` nests at most 4 deep; a cycle is refused; at most 64 uses
  are expanded in one file; a job has at most 256 steps, its composite steps'
  included.

Source: https://docs.branchd.run/branchd/pipelines/composite-steps
