Skip to main content

Trunk-based workflow component

The component requires merge requests to squash their commits when merging into the trunk.

Configure GITLAB_TOKEN with a group service account before allowing the component to update project settings.

Usage​

  • Ensure your projects have a description
  • Set GITLAB_TOKEN as a masked, protected and hidden variable for your group
  • Protect your main branch
  • Create GitLab pipeline file .gitlab-ci.yml:

Set branch-name-rule to a GitLab RE2 regular expression to reject new branch names that do not match your convention. An empty value leaves an existing branch-name expression unchanged. The configuration job also enables three push rule checks: the committer email must be verified, the commit author name must match the GitLab account, and the author must be a GitLab user. Project push rules require GitLab Premium or Ultimate and a token with permission to manage them.

Automatic configuration always adds the Git notes workaround to an existing non-empty required commit-message expression. There is no opt-in parameter. An absent expression stays absent, and the negative rule is unchanged.

When the push-rules API is unavailable, the job warns and skips default push-rule checks after confirming project access. An explicit branch-name-rule requires push-rule support. Authentication, permission and other API failures remain errors. See the project push rules API for tier requirements.

Use branch-rules to configure protected branches, access levels, and regular approval rules. Each entry requires branch, allowed-to-merge, and allowed-to-push. Add approvers to select GitLab usernames, group paths that start with /, or the role tokens @@guest, @@reporter, @@developer, @@maintainer, and @@owner. An omitted or empty approvers array selects all project Maintainers and Owners. A wildcard branch such as renovate/* creates an approval rule named renovate.

Set required to a non-negative integer to choose the number of approvals for that branch. When required is omitted, a non-empty approvers list requires one approval; an omitted or empty list selects all Maintainers and Owners but requires zero approvals. Set required: 2 to require two approvals from the selected users or groups.

If reading the approval-rules API returns 403 Forbidden or 404 Not Found, the job warns and skips regular approval-rule configuration, then continues with the remaining workflow settings. Existing approval rules stay unchanged; requested approvers and approval counts are not applied, even when explicitly configured. Check the GitLab tier and token access to enable this configuration. Authentication failures (401), other API errors, and failed rule writes still fail the job.

Use GitLab access level 0 for no access, 30 for Developer, or 40 for Maintainer. Level 60 is available to Administrators on self-managed GitLab. Force pushes are disabled for every configured rule.

When a configured branch contains CODEOWNERS, the component enables Code Owner approval and keeps the regular approval rule. A merge request must satisfy both policies. The component also enables automatic Code Owner reviewer assignment when the GitLab API supports it.

If GitLab exposes a differently named single approval rule, the component reuses it only after verifying that multiple rules are unavailable and the existing rule already provides the same project-wide approval policy. Unverifiable capabilities, API permission failures, or a non-equivalent policy fail configuration without replacing that rule. This verification uses the licensedFeatureAvailability GraphQL field available in GitLab 18.11 or later.

include:
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/codeowners@stable
inputs:
codeowners: |
[Default]
* @@maintainer @@owner
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/workflow-trunkbased@stable
inputs:
branch-name-rule: '^(main|master|develop|(feature|feat|bugfix|fix|hotfix|release|chore|ai|copilot|cursor|claude|codex)/[a-z0-9]+(\.[a-z0-9]+)*(-[a-z0-9]+(\.[a-z0-9]+)*)*)$'
branch-rules:
- branch: main
allowed-to-merge: 40
allowed-to-push: 0
approvers:
- alice
- /root-group/reviewers
- "@@owner"
required: 2
- branch: release/*
allowed-to-merge: 40
allowed-to-push: 0
approvers:
- "@@maintainer"
- "@@owner"

The example follows the Conventional Branch formal grammar.

Merge request targets​

With GitLab Premium or Ultimate, the configuration job adds target-branch workflows through the native GraphQL API. Use a Maintainer or Owner token. This requires the target-branch API and licensed-feature capability query (GitLab 18.11 or later).

Branches matching feat/*, fix/*, chore/*, and hotfix/* target the project default branch, including when its name is not main.

The job reads every page before changing policy. Existing rules and their relative order are preserved, and repeated runs do not create duplicates. The API exposes no rule priority or reorder operation, so connection order is not used as proof of effective first-match precedence.

Automatic reconciliation supports disjoint patterns only. A rule that could overlap another pattern, or an existing mapping with a different target, causes configuration to fail before changing policy. No existing rule is deleted or recreated. Align the conflicting rules in Settings > Merge requests > Merge request branch workflow, then rerun configuration. Wildcard-overlap checks are conservative; ambiguous patterns require explicit alignment.

An unavailable licensed feature or target-routing API error produces a warning and treats this optional configuration as unavailable in that GitLab version, tier or API context. No further routing writes are attempted and successful routing is not claimed. Earlier confirmed additions, if any, remain in place. Incomplete successful responses, conflicting mappings and ambiguous patterns still fail. Source admission remains enforced independently in CI. Branches without a matching workflow continue to target the default branch. Existing merge requests keep their targets. See GitLab target-branch workflows.

Restrict merge request sources​

Trunk-based defaults allow every source branch into main. Add source-branches to an entry in branch-rules to restrict sources for that target. An omitted or empty list remains unrestricted. validate:trunkbased checks every merge request pipeline even with automatic configuration disabled.

include:
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/workflow-trunkbased@stable
inputs:
branch-rules:
- branch: main
allowed-to-merge: 40
allowed-to-push: 0
source-branches:
- feat/*
- fix/*
- renovate/*

Use a catalog release containing source-branches. renovate/dependency passes this example; other/change targeting main fails. Names use case-sensitive Bash globs matching the whole source, not regular expressions or shell code. Every matching target rule applies. An unmatched target is unrestricted and invalid lists fail. This CI check does not replace branch protection, squash policy, approvals or merge-request target selection. Unlike GitFlow, no intermediate development branch is required.

Customize release rules​

Use release-rules to pass an array of commit analyzer rules to semantic-release. The default [] preserves existing analyzer rules and normal Conventional Commits behavior: features release minor, fixes patch and breaking changes major. For squash merges, check the final commit message and its body.

For example, this consumer can release breaking changes as minor:

include:
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/workflow-trunkbased@stable
inputs:
release-rules:
- breaking: true
release: minor

Non-empty input replaces releaseRules on the configured commit analyzer, including in an existing .releaserc.json, while preserving its other options and plugins. Commits not matched by a custom rule still use the analyzer's default rules. Other projects retain their defaults unless they opt in. This does not make a breaking API compatible or restrict manually created tags.

Branching​

Roles​

The following roles exist in the scenarios:

  • Developer
    • Creates a short-lived feature, fix, chore, or hot fix branch
    • Implements the change and keeps it small enough to merge quickly
    • Creates a merge request to the default branch after successful local testing
  • Maintainer
    • Reviews merge requests
    • Merges into the default branch after the pipeline succeeds and discussions are resolved

Settings enabled by default​

  • Protect main; Maintainers can merge and direct pushes are disabled
  • Disable force pushes on main
  • Branch rules
  • fast forward merge
  • Pipelines must succeed before merge
  • Merged results pipelines
  • Skipped pipelines are considered successful
  • All discussions must be resolved
  • Allow merge request authors and committers to approve when at most five active human Maintainers and Owners can approve. Prevent both approval types for larger teams.
  • Remove all approvals when commits are added
  • Prevent editing approval rules in merge requests
  • Create a regular approval rule for each configured branch and assign its configured approvers, or all Maintainers and Owners when none are listed. Require the configured required count, defaulting to one for explicit approvers and zero otherwise.
  • With a CODEOWNERS file, also require Code Owner approval and automatically assign matching Code Owners when the GitLab API supports it.
  • Configure merge request targets when the licensed native API is available.

Inputs​

NameDescriptionDefaultType
------------
autoconfigure-enabledEnable or disable the automatic configuration of the pipeline.trueboolean
branch-name-ruleRE2 regular expression that every new branch name must match. An empty value leaves the project push rule unchanged.string
branch-rulesProtected branch, approval, and source-branches rules. An omitted or empty source-branches list allows every source branch. Each rule can assign approvers by username, /group path, or @@role. Access levels are 0 (no access), 30 (Developer), 40 (Maintainer), or 60 (Administrator on self-managed GitLab).[{"branch":"main","allowed-to-merge":40,"allowed-to-push":0}]array
pattern-branch-interruptibleRegex pattern to mark the pipeline as interruptible based on the branch name. Use https://github.com/google/re2/wiki/Syntax/^(feat|fix|hotfix)\//string
pattern-branch-skipRegex pattern to skip the pipeline based on the branch name. Use https://github.com/google/re2/wiki/Syntaxstring
release-rulesCustom commit-analyzer release rules. An empty array preserves the configured analyzer rules and defaults.[]array
rulesThe rules that control when the project configuration job runs.[{"if":"$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH","when":"always"},{"when":"never"}]array