# Call a pipeline (reusable pipelines)

> Write jobs once in a pipeline file with on: call, and use them as one job of another pipeline.
> **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 reusable pipeline:

```yaml title=".branchd/pipelines/deploy.yaml"
# .branchd/pipelines/deploy.yaml
# A reusable pipeline: on: call only, so it never runs on its own. A job
# that uses it gets its two jobs, named "<calling job> / plan" and
# "<calling job> / apply".
on:
  call:
    inputs:
      version: {type: string, required: true}
      environment: {type: choice, options: [stage, production], default: stage}
    secrets:
      DEPLOY_TOKEN: {required: true}
    outputs:
      revision: ${{ jobs.apply.outputs.revision }}
env:
  TARGET: ${{ inputs.environment }}
jobs:
  plan:
    image: alpine:3
    steps:
      - run: ./scripts/plan.sh "${{ inputs.version }}" "$TARGET"
  apply:
    needs: [plan]
    image: alpine:3
    secrets: [DEPLOY_TOKEN]
    outputs:
      revision: ${{ steps.apply.outputs.revision }}
    steps:
      - id: apply
        run: ./scripts/apply.sh "${{ inputs.version }}" "$TARGET" >> "$BRANCHD_OUTPUT"
```

The pipeline that calls it, twice:

```yaml title=".branchd/pipelines/ci.yaml"
# .branchd/pipelines/ci.yaml
# Calls deploy.yaml twice, once per environment. The run's jobs are build,
# stage / plan, stage / apply, production / plan, production / apply and
# announce.
name: CI
on:
  push:
    branches: [main]
jobs:
  build:
    image: alpine:3
    outputs:
      version: ${{ steps.v.outputs.version }}
    steps:
      - id: v
        run: echo "version=1.0.$CI_PIPELINE_IID" >> "$BRANCHD_OUTPUT"
  stage:
    needs: [build]
    uses: ./.branchd/pipelines/deploy.yaml
    with:
      version: ${{ needs.build.outputs.version }}
      environment: stage
    secrets: [DEPLOY_TOKEN]
  production:
    needs: [build, stage]
    uses: ./.branchd/pipelines/deploy.yaml
    with:
      version: ${{ needs.build.outputs.version }}
      environment: production
    secrets: [DEPLOY_TOKEN]
    environment: production
    manual: true
  announce:
    needs: [production]
    image: alpine:3
    steps:
      - run: echo "deployed ${{ needs.production.outputs.revision }}"
```

- **`on: call`** makes a file callable. It declares `inputs` (typed as for
  [manual runs](https://docs.branchd.run/branchd/pipelines/manual-runs)), `secrets`
  (`{required, description}`) and `outputs`, each output one job's output,
  `${{ jobs.<id>.outputs.<name> }}`. A file that declares only `on: call`
  never runs on its own.
- **A calling job** has `uses:` instead of `steps:`: `./<path>.yaml` from the
  repository's root, at this commit, or another repository's pipeline,
  `<group>/<repo>/<path>.yaml@v1` (see
  [Other repositories](https://docs.branchd.run/branchd/pipelines/other-repositories)). Its called jobs join this run, named
  `<calling job> / <job>`, with checks such as `CI / stage / apply`.
- **The calling job takes** `uses`, `with`, `needs`, `if`, `secrets`, `allow`,
  `runs-on`, `environment`, `concurrency`, `manual` and `matrix`. It runs
  nothing itself.
- **`matrix:`** on the calling job calls the pipeline once per cell, as GitHub
  calls a reusable workflow from a matrix: the cell's jobs are
  `<calling job> (<values>) / <job>`, its `with:` and `if:` read
  `${{ matrix.<key> }}`, and a job that needs the calling job waits for every
  cell's jobs. A matrix call has no outputs. With two or more cells, a failed called
  job cancels unfinished jobs across the call unless its matrix sets
  `fail-fast: false`.
- **`with:`** gives the inputs. A whole `${{ needs.<job>.outputs.<name> }}`
  of a job it needs passes that output to a `string` or `choice` input.
- **`secrets:`** lists what it passes. Each must be declared under
  `on.call.secrets`; a required one must be passed. A called job gets, of the
  secrets in its own `secrets:`, those the caller passed.
- **`needs:`** on the calling job is what the called pipeline's first jobs
  wait for. A job that needs the calling job needs all its jobs, and reads the
  declared outputs as `${{ needs.<calling job>.outputs.<name> }}`.
- **`environment:`, `concurrency:` and `manual:`** on the calling job are set
  on every called job. `manual: true` above makes `production / plan` and
  `production / apply` each wait to be released. A called job that sets its
  own `environment` or `concurrency` as well is refused.
- **What a called pipeline reads:** the run's facts, its inputs, its own
  `env:` and its own jobs. The repository's settings variables (`vars`) are
  refused there: pass them as inputs.
- Its jobs run as the calling repository, with its job token (at most the
  calling job's `permissions:`), runners and the secrets passed. A secret
  `on.call.secrets` declares `required: false` may be left out. A pipeline has at most 500 jobs, called jobs and matrix
  cells included.

Source: https://docs.branchd.run/branchd/pipelines/reusable-pipelines
