TExcelTAD
Home › CI/CD Pipelines & Pipeline Security › Continuous Integration in Practice

Continuous Integration in Practice

Continuous integration is a team habit before it is a server. A CI server that runs tests on branches nobody merges for three weeks is "continuous building", not integration. The practice has a few rules:

  1. Merge to the main branch at least daily. Short-lived branches (hours to a day or two) or direct trunk commits behind feature flags. The Git course compares Gitflow with trunk-based development; CI works best with the latter.
  2. Every merge runs the full pipeline on the merge result, not just on the branch.
  3. A red main branch is the team's top priority. Fix it or revert within minutes; never pile new commits on a broken build.
  4. The pipeline is fast. About 10 minutes from push to verdict is the usual target for the main feedback loop. Slower than that, and people stop waiting for it and batch their changes.
  5. Everyone can see the status, and everyone can run the same checks locally.

Why small batches win: a model

Why "integrate often"? Because the cost of merging grows with the time two lines of work spend apart. The model gives eight developers a stream of small changes, each touching a few files of a 60-file codebase (seeded, so it is the same on every run). It counts how many files are changed on both sides when each branch is merged: those are the potential conflicts somebody has to resolve by hand.

python · try it yourself
import random

FILES = 60
DEVS = 8
DAYS = 20

def simulate(branch_days, seed=7):
    rng = random.Random(seed)
    main_changes = []          # (day, file) committed to main
    conflicts = 0
    merges = 0
    for dev in range(DEVS):
        day = 0
        while day < DAYS:
            touched = set()
            for d in range(branch_days):          # one change per day on the branch
                touched |= set(rng.sample(range(FILES), 2))
            merge_day = day + branch_days
            # files changed on main by others while this branch was open
            others = {f for (d, f) in main_changes if day <= d < merge_day}
            conflicts += len(touched & others)
            merges += 1
            main_changes += [(merge_day, f) for f in touched]
            day = merge_day
    return merges, conflicts

for branch_days in (1, 2, 5, 10):
    merges, conflicts = simulate(branch_days)
    print(f"branches live {branch_days:>2} day(s): {merges:>3} merges, {conflicts:>3} overlapping files, "
          f"{conflicts / merges:.1f} per merge")
branches live  1 day(s): 160 merges,  41 overlapping files, 0.3 per merge
branches live  2 day(s):  80 merges,  71 overlapping files, 0.9 per merge
branches live  5 day(s):  32 merges, 123 overlapping files, 3.8 per merge
branches live 10 day(s):  16 merges, 105 overlapping files, 6.6 per merge

The total amount of work is the same in every row. Long-lived branches don't create more changes, they let changes collide in larger batches, so each merge is bigger, riskier and harder to review. That is the core argument of CI, and the reason the DORA research links small batch size with both speed and stability (DORA metrics).

What runs on every change

Check Purpose Typical time
Lint and format style, obvious bugs, dangerous patterns seconds
Unit tests logic in isolation seconds to a few minutes
Build the code compiles and packages; the artifact exists minutes
Integration tests components with real dependencies (a database in a container) minutes
Security checks SAST, secrets, dependencies, image scan (Pipeline Security) seconds to minutes
End-to-end tests the deployed system through its UI or API minutes to tens of minutes, often after merge

The next three lessons go through lint, tests and build verification in turn.

Branch protection makes the habit stick

Both GitHub and GitLab can require a passing pipeline before a merge:

  • GitHub: a branch ruleset (or branch protection rule) on main with "Require status checks to pass" listing the job names, plus "Require a pull request before merging".
  • GitLab: "Pipelines must succeed" in the project's merge request settings, plus protected branches.

"Require branches to be up to date" (GitHub) or merge trains (GitLab) and the merge queue (GitHub) go one step further: they test the change as it will be after the merge, against the latest main, so two changes that pass separately but break together can't both land.

Test yourself

In the model, why do 10-day branches cause more overlap per merge than 1-day branches, even though the total work is the same?

  1. Developers write more code
  2. Each branch collects more changes while main also moves further, so the two sides collide in bigger batches
  3. Git merges slower
  4. Random chance

Main is red after a merge. What does CI practice say to do?

  1. Keep merging; the next fix will cover it
  2. Fix it or revert within minutes, before anything else is merged
  3. Disable the failing test
  4. Wait for the nightly build

What does a merge queue (or merge train) check that a normal PR pipeline doesn't?

  1. Code style
  2. The change combined with everything ahead of it on the latest main
  3. Commit messages
  4. Nothing extra