# Coming from GitLab CI

> How each GitLab CI construct is written in a branchd pipeline file, with a worked conversion.
> **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).

## Mapping

| GitLab CI                                                 | Pipeline file                                                                                                                         |
| --------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| `.gitlab-ci.yml`                                          | `.branchd/pipelines/<name>.yaml`, one file per pipeline                                                                               |
| `stages:`, `stage:`                                       | None. Order jobs with `needs:`.                                                                                                       |
| `workflow: rules:`                                        | `on:`                                                                                                                                 |
| `rules:`, `only:`, `except:`                              | One `if:` per job; `changed()` for `rules: changes:`                                                                                  |
| `when: manual`                                            | `manual: true`                                                                                                                        |
| `when: always` (a job)                                    | `if: always()`                                                                                                                        |
| `script:`                                                 | `steps:`, each `- run: …`                                                                                                             |
| `before_script:`                                          | The first steps                                                                                                                       |
| `after_script:`                                           | A last step with `if: always()`                                                                                                       |
| `variables:`                                              | `env:`                                                                                                                                |
| `default:`                                                | `defaults:` (`image`, `runs-on`)                                                                                                      |
| `image:`                                                  | `image:`                                                                                                                              |
| `services: [{name, alias}]`                               | `services: {<alias>: {image, env}}`                                                                                                   |
| `tags:`                                                   | `runs-on:`                                                                                                                            |
| `cache: {key: {files, prefix}}`                           | `cache: {files, key}`                                                                                                                 |
| `artifacts: {expire_in}`                                  | `artifacts: {expire-in}`                                                                                                              |
| `dependencies:`, `needs: {artifacts: false}`              | `needs: [{job, artifacts: false}]`                                                                                                    |
| `parallel: matrix:`                                       | `matrix:`                                                                                                                             |
| `allow_failure:`                                          | `continue-on-error:`                                                                                                                  |
| `resource_group:`                                         | `concurrency:` (a lock name)                                                                                                          |
| `interruptible: true`                                     | `concurrency: {cancel-in-progress: true}`                                                                                             |
| `environment:`                                            | `environment:`                                                                                                                        |
| `retry:`                                                  | `retry:`                                                                                                                              |
| `timeout:`                                                | `timeout:`                                                                                                                            |
| `extends:`, hidden jobs, `!reference`                     | A [composite step](https://docs.branchd.run/branchd/pipelines/composite-steps), a [reusable pipeline](https://docs.branchd.run/branchd/pipelines/reusable-pipelines), or YAML anchors |
| `include:`                                                | `uses:`                                                                                                                               |
| `trigger:` (multi-project)                                | [`trigger:`](https://docs.branchd.run/branchd/pipelines/trigger-another-repository), and the target declares `on: trigger`                                    |
| Child pipelines                                           | Several files, or a reusable pipeline                                                                                                 |
| `$CI_*` variables                                         | Still set, apart from `CI_JOB_STAGE`                                                                                                  |
| `rules: if: $CI_COMMIT_BRANCH == "main"`                  | `if: branch == 'main'`                                                                                                                |
| `rules: if: $CI_PIPELINE_SOURCE == "merge_request_event"` | `if: event == 'pull_request'`                                                                                                         |
| Pipeline schedules in settings                            | `on: schedule` in the file                                                                                                            |
| `[skip ci]`, `[ci skip]`, `git push -o ci.skip`           | The same: [Skip a push's pipelines](https://docs.branchd.run/branchd/pipelines/skip-a-run)                                                                    |

## A worked conversion

Before:

```yaml title=".gitlab-ci.yml"
# .gitlab-ci.yml
# Before: a GitLab CI file with stages, rules, a service, a cache and a
# manual deploy.
stages: [test, build, deploy]

variables:
  GOFLAGS: -mod=readonly

default:
  image: golang:1.26

test:
  stage: test
  services:
    - name: mysql:8
      alias: mysql
      variables: {MYSQL_ROOT_PASSWORD: test}
  cache:
    key: {files: [go.sum], prefix: go-mod}
    paths: [.cache/mod]
  before_script:
    - export GOMODCACHE="$CI_PROJECT_DIR/.cache/mod"
  script:
    - go test ./...
  after_script:
    - ./scripts/collect-logs.sh

build:
  stage: build
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
  script:
    - go build -o dist/server ./cmd/server
  artifacts:
    paths: [dist/]
    expire_in: 1 week

deploy:
  stage: deploy
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      when: manual
  resource_group: production
  environment: {name: production, url: https://app.example.com}
  script:
    - ./scripts/deploy.sh production
```

After:

```yaml title=".branchd/pipelines/ci.yaml"
# .branchd/pipelines/ci.yaml
# After: the same pipeline. Stages become needs:, rules: become on: and
# if:, before_script and after_script become steps.
name: CI
on:
  push:
    branches: [main]
  pull_request:
env:
  GOFLAGS: -mod=readonly
defaults:
  image: golang:1.26
jobs:
  test:
    services:
      mysql:
        image: mysql:8
        env: {MYSQL_ROOT_PASSWORD: test}
    cache:
      key: go-mod
      files: [go.sum]
      paths: [.cache/mod]
    steps:
      - run: export GOMODCACHE="$PWD/.cache/mod"
      - run: go test ./...
      - if: always()
        run: ./scripts/collect-logs.sh
  build:
    needs: [test]
    if: branch == 'main' && event == 'push'
    steps:
      - run: go build -o dist/server ./cmd/server
    artifacts:
      paths: [dist]
      expire-in: 1 week
  deploy:
    needs: [build]
    if: branch == 'main' && event == 'push'
    manual: true
    concurrency: production
    environment: {name: production, url: 'https://app.example.com'}
    steps:
      - run: ./scripts/deploy.sh production
```

What changed:

- `stages` went; `build` needs `test` and `deploy` needs `build`.
- The two `rules:` became one `if:` each, and `on:` says the pipeline runs on
  pushes to `main` and on pull requests.
- `default: image` became `defaults: image`.
- The service's alias became its key.
- `before_script` became a first step. Steps share a shell, so its `export`
  reaches the next step. `after_script` became a step with `if: always()`.
- `when: manual` became `manual: true`, and `resource_group` became
  `concurrency`.
- `$CI_PROJECT_DIR` became `$PWD`: a step starts in the checkout.

`branchd ci lint` names any key you missed, with its replacement.

Source: https://docs.branchd.run/branchd/pipelines/from-gitlab-ci
