# Other CI tools: GitLab CI, CircleCI and Buildkite

The same shop-api pipeline in GitLab CI, CircleCI and Buildkite: how each names jobs, order, caches, artifacts and approvals, and where each runs your jobs.

Course: [CI/CD Pipelines](https://devops.rajeev.pro/courses/cicd), lesson 9. About 35 minutes, intermediate. Source: https://devops.rajeev.pro/courses/cicd/09-other-ci-tools

## What you will learn

- Map the ideas you know from GitHub Actions onto any CI tool
- Read and write a GitLab CI pipeline with stages, needs, caching, artifacts and rules
- Read and write a CircleCI config with executors, reusable commands and workflows
- Read and write a Buildkite pipeline with steps, dependencies and a manual approval
- Explain how each tool decides where jobs run, and what that means for security and cost
- Check a pipeline file against the tool's schema before pushing it

## One pipeline, four spellings

Every CI tool runs the same kind of pipeline: jobs in some order, on some machine, with caches,
artifacts, secrets and approvals. Once you can map the words, you can read any of them:

> **Figure.** A translation table between four CI tools for the same concepts. The pipeline file: .github/workflows/ci.yml for GitHub Actions, .gitlab-ci.yml for GitLab CI, .circleci/config.yml for CircleCI, .buildkite/pipeline.yml for Buildkite. A unit of work: a job in Actions, GitLab and CircleCI; a step in Buildkite. Ordering: needs in Actions and GitLab, which also has stages; requires in CircleCI workflows; depends_on and wait in Buildkite. Where it runs: runs-on runners; image plus runners; executors with resource classes; agents you host. Manual approval: environments with reviewers; when: manual; an approval job type; a block step.
>
> Same ideas, different words. Learn the mapping and any pipeline file becomes readable.

This lesson writes shop-api's pipeline (lint, unit, integration) in each tool. All three files below were
checked against the tool's published JSON schema.

The same pipeline still needs clear failure signals, controlled credentials and a tested artifact. Mapping each
tool's terms to these jobs makes the workflow easier to review. A GitLab stage, CircleCI workflow and Buildkite
dependency all describe ordering, but their execution and ownership models differ. Compare where runners live,
who patches them, and how secrets are scoped before choosing a tool.

## GitLab CI

**When you'll meet it:** at companies that host their code on GitLab, often self-hosted GitLab
inside their own network, where it's the natural choice because code, issues, CI and the container
registry all live in one product.

GitLab CI is built into GitLab. The pipeline is `.gitlab-ci.yml` at the top of the repository, and jobs run
on **runners**: GitLab's shared ones, or your own.

```yaml
# .gitlab-ci.yml
stages: [lint, test, build]

default:
  image: node:22-alpine              # every job runs in this container
  cache:
    key:
      files: [package-lock.json]     # new cache when the lockfile changes
    paths: [.npm/]
  before_script:
    - npm ci --cache .npm --prefer-offline

lint:
  stage: lint
  script:
    - npm run lint

unit:
  stage: test
  script:
    - npm run test:coverage

integration:
  stage: test
  needs: [lint]                      # start as soon as lint passes
  script:
    - npm run test:integration

package:
  stage: build
  script:
    - mkdir -p dist && cp -r src package.json dist/
    - tar -czf shop-api-$CI_COMMIT_SHORT_SHA.tgz -C dist .
  artifacts:
    paths: [shop-api-*.tgz]
    expire_in: 7 days
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
```

| GitLab                                             | GitHub Actions equivalent                              |
| -------------------------------------------------- | ------------------------------------------------------ |
| `stages:`                                          | job order through `needs` (Actions has no stages)      |
| `image:`                                           | `container:` on a job, or a Docker step                |
| `script:`                                          | `run:` steps                                           |
| `needs:`                                           | `needs:`                                               |
| `rules: - if:`                                     | `if:`                                                  |
| `artifacts:`                                       | `upload-artifact`                                      |
| `$CI_COMMIT_SHORT_SHA`, `$CI_DEFAULT_BRANCH`       | `github.sha`, `github.event.repository.default_branch` |
| _Settings → CI/CD → Variables_ (masked, protected) | secrets and variables                                  |
| `environment:` + `when: manual`                    | environments with required reviewers                   |

**Stages** run in order, each waiting for the whole previous stage. `needs` overrides that with a graph:
`integration` starts the moment `lint` passes, without waiting for `unit`. GitLab validates the file as you
type in _Build → Pipeline editor_.

> **Video.** [GitLab for Everyone: Your First CI/CD Pipeline Explained](https://www.youtube.com/watch?v=IV5MQUEUx44&t=136s) (GitLab)

## CircleCI

**When you'll meet it:** at teams that wanted fast, hosted CI before GitHub Actions existed, and
stayed for its caching, test splitting and wide range of machine types (including macOS for mobile
apps).

CircleCI is a hosted CI service that connects to GitHub or Bitbucket. The config is `.circleci/config.yml`:

```yaml
# .circleci/config.yml
version: 2.1

executors:
  node:
    docker:
      - image: cimg/node:22.23        # CircleCI's convenience image
    resource_class: small             # the machine size you pay for

commands:
  install:                            # reusable steps, like a composite action
    steps:
      - checkout
      - restore_cache:
          keys:
            - npm-v1-{{ checksum "package-lock.json" }}
      - run: npm ci
      - save_cache:
          key: npm-v1-{{ checksum "package-lock.json" }}
          paths: [~/.npm]

jobs:
  lint:
    executor: node
    steps:
      - install
      - run: npm run lint
  unit:
    executor: node
    steps:
      - install
      - run: npm run test:coverage
  integration:
    executor: node
    steps:
      - install
      - run: npm run test:integration

workflows:
  ci:
    jobs:
      - lint
      - unit
      - integration:
          requires: [lint, unit]
```

| CircleCI         | Means                                                                      |
| ---------------- | -------------------------------------------------------------------------- |
| `executors`      | where a job runs: Docker image, `machine` VM, macOS; plus `resource_class` |
| `commands`       | reusable step sequences                                                    |
| `jobs`           | the units of work                                                          |
| `workflows`      | the order: `requires:` is `needs:`                                         |
| `orbs`           | shared, versioned packages of jobs and commands (like actions)             |
| `type: approval` | a job that waits for a person to click Approve                             |

Caching is explicit: you choose the key and the paths, and `{{ checksum "package-lock.json" }}` plays the role
of `hashFiles()`. Secrets live in project settings or **contexts**, which can be restricted to security
groups. `circleci config validate` (CircleCI CLI) checks the file locally.

> **Video.** [CircleCI Tutorial — Tests, Cache, and Artifacts](https://www.youtube.com/watch?v=lqmmbk_ekMY) (Test Systems)

## Buildkite

Buildkite splits CI in two: Buildkite runs the **control plane** (the web UI, scheduling, logs), and you run
the **agents** that execute jobs, on your own machines, cloud or Kubernetes. Your code and secrets never leave
your infrastructure, which is why large companies with strict security or special hardware choose it.

```yaml
# .buildkite/pipeline.yml
steps:
  - label: ":eslint: lint"
    key: lint
    command: npm ci && npm run lint
    plugins:
      - docker#v5.14.0:
          image: node:22-alpine

  - label: ":test_tube: unit"
    key: unit
    command: npm ci && npm run test:coverage
    plugins:
      - docker#v5.14.0:
          image: node:22-alpine

  - label: ":globe_with_meridians: integration"
    depends_on: [lint, unit]
    command: npm ci && npm run test:integration
    plugins:
      - docker#v5.14.0:
          image: node:22-alpine

  - wait                                  # everything above must pass

  - block: ":rocket: Deploy to production?"
    branches: main

  - label: ":rocket: deploy"
    command: ./scripts/deploy.sh production
    branches: main
```

| Buildkite                  | Means                                                          |
| -------------------------- | -------------------------------------------------------------- |
| a **step** with `command:` | a job                                                          |
| `key` + `depends_on`       | `needs`                                                        |
| `wait`                     | a barrier: all previous steps must pass                        |
| `block`                    | pauses the build until someone unblocks it in the UI           |
| `plugins`                  | reusable behaviour, such as running the step in a Docker image |
| agents and **queues**      | where steps run: your machines, matched by tags                |

Pipelines can also be **dynamic**: a step runs a script that generates more steps with `buildkite-agent
pipeline upload`, which is how monorepos test only the parts that changed.

> **Video.** [Buildkite Tutorial — Run CI on Your Own Agent](https://www.youtube.com/watch?v=fkg_LMBeHdU) (Test Systems)

## Choosing a tool

|              | GitHub Actions                                   | GitLab CI                             | CircleCI                                 | Buildkite                          |
| ------------ | ------------------------------------------------ | ------------------------------------- | ---------------------------------------- | ---------------------------------- |
| Lives with   | GitHub                                           | GitLab                                | GitHub, Bitbucket                        | any Git host                       |
| Runs jobs on | GitHub's runners or yours                        | GitLab's runners or yours             | CircleCI's machines or yours             | **your** agents only               |
| Strength     | marketplace of actions, tight GitHub integration | one product for code, CI and registry | fast, flexible caching and machine sizes | scale, security, your own hardware |

Most teams use what comes with their Git host. The skills transfer: order checks by cost, cache dependencies,
build once, gate production behind approval, keep secrets out of the repository and pin what you depend on.

Use the comparison as a starting point, not a universal ranking. A tool that is easy for one team may be costly
for another if it needs hardware or administration they do not already have. Test a small pipeline with the same
jobs and permissions before moving a production workflow. Check current pricing, runner availability and the
configuration schema at that time; those details can change independently of the pipeline concepts.

> **Validate before you push.** Each tool publishes a JSON schema for its file. Editors with the YAML extension use them to underline mistakes as you type, and you can check from the terminal. For example, a GitLab job with `when: sometimes` fails with `'sometimes' is not one of ['on_success', 'on_failure', 'always', 'never', 'manual', 'delayed']`.

## Mission: Run shop-api's pipeline on GitLab

Mirror shop-api to GitLab, add the GitLab CI pipeline from this lesson, and get lint, unit, integration and package passing. Then change integration to wait for both lint and unit, and compare the pipeline graph.

- You need: A free GitLab.com account and the shop-api repository
- You'll have: A green GitLab pipeline with a package artifact

```sh
$ git remote add gitlab git@gitlab.com:<your-user>/shop-api.git
$ nano .gitlab-ci.yml                         # the pipeline from this lesson
$ git add .gitlab-ci.yml && git commit -m "ci: add GitLab CI pipeline"
$ git push gitlab main
```

In GitLab, open _Build → Pipelines_ and the latest pipeline: **integration** starts as soon as **lint**
passes, even while **unit** is running. Download `shop-api-<sha>.tgz` from the **package** job's artifacts.
Then change `needs: [lint]` to `needs: [lint, unit]`, push, and watch the graph draw both lines into
integration. New GitLab.com accounts may need to verify an identity before shared runners pick up jobs.

## Try it

- Import shop-api into GitLab (free account), add the .gitlab-ci.yml from this lesson, and watch the pipeline graph.
- In GitLab, open *Build → Pipeline editor* and use its validation on a file with a typo.
- Run the CircleCI config through `circleci config validate` after installing the CircleCI CLI.
- Change GitLab's integration job to `needs: [lint, unit]` and see how the graph changes.
- Add a block step to a Buildkite pipeline and read how it pauses the build.
- For each tool, find where you would store a deploy secret.

## Quiz

1. In GitLab CI, what does needs do that stages alone don't?
   - Nothing
   - **Starts a job as soon as the jobs it needs finish, even if earlier stages are still running** (answer)
   - Runs the job twice
   - Skips the stage
   Why: Stages run in order, each waiting for the whole previous stage. needs builds a graph instead, which is usually faster.

2. What is a CircleCI executor?
   - A person who approves deploys
   - **The environment a job runs in: a Docker image, a machine VM or macOS, with a resource class** (answer)
   - A test runner
   - A secret store
   Why: Like runs-on in GitHub Actions, plus the size of the machine. Define it once and reuse it in every job.

3. Where do Buildkite jobs run?
   - Always on Buildkite's own servers
   - **On agents you run on your own machines or cloud; Buildkite runs only the control plane** (answer)
   - Only on Kubernetes
   - On GitHub's runners
   Why: Your code and secrets never leave your infrastructure, and you choose the hardware. You also run and secure the agents.

4. Which GitLab CI keyword controls whether a job runs, for example only on the default branch?
   - stage
   - **rules** (answer)
   - needs
   - image
   Why: rules with if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH is the modern replacement for only and except.

5. Moving from GitHub Actions to another tool, what changes least?
   - The YAML keywords
   - **The pipeline design: stages ordered by cost, caching, build once, gated deploys** (answer)
   - Where secrets are stored
   - How runners are chosen
   Why: Every tool spells it differently, but the shape of a good pipeline is the same everywhere.

## Resources

- [GitLab CI/CD YAML syntax reference](https://docs.gitlab.com/ci/yaml/) (GitLab Docs)
- [GitLab: migrating from GitHub Actions](https://docs.gitlab.com/ci/migration/github_actions/) (GitLab Docs)
- [CircleCI configuration reference](https://circleci.com/docs/reference/configuration-reference/) (CircleCI Docs)
- [Buildkite: defining your pipeline steps](https://buildkite.com/docs/pipelines/configure/defining-steps) (Buildkite Docs)
- [Buildkite Docker plugin](https://github.com/buildkite-plugins/docker-buildkite-plugin) (Buildkite)
