# fragments + inject (/docs/macros/fragments)



<Callout type="warn" title="Deprecated: prefer _anchors: / templates:">
  `fragments` still compiles and expands exactly as shown below, but native YAML
  anchors now cover the same-file, no-param case: a `- *alias` whose anchor is a step
  list is flattened in place at compile time. Compiling a file that uses `fragments:`
  emits a `fragment-deprecated` warning. Migrate:

  * **Same-file, no params:** move the list under [`_anchors:`](/docs/macros/anchors)
    with a `&name` anchor and call it with `- *name` (see below).
  * **Params or cross-file:** use [`templates:`](/docs/macros/templates) +
    `inject ... with`, or cross-file `inject: ./lib#name`.
</Callout>

## Migrating to `_anchors:` [#migrating-to-_anchors]

Same-file, no-param reuse maps one-to-one onto native anchors:

<CodeCompare>
  ```yaml title="before (fragments)"
  fragments:
    setup:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
  jobs:
    test:
      runs-on: ubuntu-latest
      steps:
        - inject: setup
        - run: npm test
  ```

  ```yaml title="after (_anchors)"
  _anchors:
    setup: &setup
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
  jobs:
    test:
      runs-on: ubuntu-latest
      steps:
        - *setup
        - run: npm test
  ```
</CodeCompare>

Both compile to the identical workflow. If you need typed params or cross-file reuse,
use [`templates:`](/docs/macros/templates) instead.

## Reference [#reference]

Define reusable step blocks at the top of the file; splice them in with
`- inject: <name>`.

`fragments` is stripped, `inject` expanded in place:

<CodeCompare>
  ```yaml title=".actio.yml"
  name: Fragments
  on: [push]
  fragments:
    setup:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
  jobs:
    test:
      runs-on: ubuntu-latest
      steps:
        - inject: setup
        - run: npm test
  ```

  ```yaml title="generated .yml"
  jobs:
    test:
      runs-on: ubuntu-latest
      steps:
        - uses: actions/checkout@v4
        - uses: actions/setup-node@v4
          with:
            node-version: 20
        - run: npm test
  ```
</CodeCompare>


## Sitemap

Browse the full documentation: [Markdown sitemap](https://austenstone.github.io/actio/sitemap.md) · [XML sitemap](https://austenstone.github.io/actio/sitemap.xml)