GitLab Functions: Reusable CI Logic Beyond Scripts and Components
GitLab Functions are experimental, but they deserve attention because they target a real CI problem: teams keep copying the same shell logic into dozens of pipelines and then pretending it is standardization.
That is why this post is intentionally practical. It does not try to turn GitLab Functions into a product brochure. It treats the announcement, release, or vulnerability as an operating decision: what should a cloud team change, what can wait, what has to be measured, and which guardrails keep the fix from becoming a new source of downtime.
If you are connecting this to the existing BitsLovers library, start with GitLab CI monorepo patterns, GitLab CI parallel matrix jobs, GitLab runner tag strategy, GitLab CI services for test databases, Jenkins to GitLab CI migration, GitLab review apps and environments. Those articles cover the adjacent platform patterns; this one focuses on reusable CI logic and where GitLab Functions fit beside includes, components, anchors, and scripts.

The workflow above is the recommended operating model. It keeps the discussion out of the abstract. You start with the signal, scope the blast radius, implement the smallest useful control, verify the result, and then turn the work into a repeatable runbook. That order matters. A lot of teams jump straight from announcement to tooling. That feels fast, but it usually skips ownership, rollback, and the boring evidence an auditor or incident reviewer will ask for later.
What Changed
GitLab now documents CI/CD Functions as a way to define reusable pipeline logic packaged and referenced through function calls. That puts them beside a crowded set of GitLab reuse tools: YAML anchors, includes, components, templates, and plain scripts stored in repositories.
The date matters here because engineering teams already have plenty of stale guidance in their wikis. Treat this as a May 2026 operating note. If a vendor updates the documentation later, update the runbook and leave a revision note in the post. That is not editorial polish; it is how you keep technical content from becoming another unsafe copy-paste source.
The mechanism is packaging a reusable unit of CI behavior and calling it from pipeline configuration. The value is not fewer lines of YAML by itself. The value is a better ownership boundary. A platform team can publish a function for a common task, version it, and let application teams consume it without copying the implementation.
Why Platform Teams Should Care
GitLab-heavy organizations usually hit reuse pain in three places: Terraform jobs, container builds, and security scans. The first team writes the good version. The next nine teams copy it. Six months later, every project has a slightly different retry rule, secret handling pattern, and artifact path. Reuse primitives are governance tools, not formatting tricks.
This is also where cost and reliability get mixed together. A feature that looks like a security improvement can increase build time, data scanned, node churn, or operational review effort. A reliability feature can quietly move risk from the service team to the platform team. A new AI workflow can shorten analysis time and still create a governance problem if the identity model is weak. Good engineering writing should name that tradeoff.
For GitLab Functions, the practical question is not “is this useful?” It is useful. The better question is where the control should live. If it belongs in a one-off project, document it there. If it belongs in the platform baseline, put it in CI, admission control, IAM, observability, or a shared runbook. Most teams get into trouble when they make that boundary implicit.
Operating Baseline
The baseline is to pick the weakest abstraction that solves the problem. Anchors are fine inside one file. Includes are fine for shared snippets. Components are better for reusable CI modules. Functions become interesting when the behavior is command-like, versioned, and maintained by a platform owner.
| GitLab reuse option | Use it for | Avoid it when |
|---|---|---|
| YAML anchors | Small local repetition | Multiple repositories need the same logic |
| include | Shared templates and central YAML | You need a versioned command-like unit |
| CI/CD components | Reusable pipeline modules | The logic is more like a function call |
| Functions | Packaged reusable execution logic | The feature state is too new for a critical gate |
The table is deliberately opinionated. It gives you a default answer before the exception shows up. Exceptions are fine; hidden exceptions are not. If someone wants to bypass the default, require a reason, an owner, and an expiration date. That one small rule prevents a lot of permanent “temporary” infrastructure.
Implementation Pattern
A migration should start with one noisy copy-paste block, not the entire pipeline estate.
stages: [validate, build]
terraform-plan:
stage: validate
script:
- gitlab-fn run registry.example.com/platform/terraform-plan:v1 \
--root infra \
--backend prod \
--out plan.json
artifacts:
paths:
- plan.json
expire_in: 7 days
The snippet is not meant to be pasted blindly. Use it as the shape of the implementation, then adapt names, account boundaries, tags, and approval gates to your environment. The useful part is the sequence: inspect, constrain, verify, and record evidence. If your process cannot produce evidence, it is not mature enough for production.
Controls, Metrics, And Evidence
Reusable CI is only useful if it reduces drift. Track drift directly.
| Metric | Signal | Target |
|---|---|---|
| Duplicate shell blocks | How much unmanaged logic remains | Falls each sprint |
| Pinned function versions | Upgrade discipline | 100 percent for production projects |
| Failure rate by function | Quality of the shared primitive | Visible to platform owner |
| Mean upgrade lag | How long projects stay on old versions | Defined per criticality tier |
Notice that the table separates a control from the evidence. A control without evidence is a hope. Evidence without an owner is a screenshot in a ticket that nobody trusts three months later. Tie each signal to a system that already has retention, access control, and review habits.
Rollout Plan
Do not announce a function platform. Replace one painful pattern and let the value prove itself.
- Inventory repeated jobs across the top ten active repositories.
- Pick one pattern with clear ownership, such as Terraform plan, container build, or SAST export.
- Create a versioned function and keep the old pipeline path available for one release.
- Require pinned versions. Floating tags make debugging impossible.
- Publish migration examples and a rollback path before asking teams to adopt it.
This is where teams often overbuild. Start with the smallest production slice that proves the behavior. One non-critical cluster, one runner group, one application namespace, one account, or one data domain is enough. Then widen the blast radius only after you have a rollback path and a metric that proves the change did not make the system worse.
Gotchas
The dangerous part is treating every reuse tool as the same thing.
- A function that hides too much becomes a black box. Keep inputs and outputs obvious.
- Do not put secrets into function parameters unless the handling is documented and logged safely.
- Experimental features need a fallback. Critical deploy gates should not depend on a feature your team cannot debug.
- Versioning matters more than syntax. A shared function without release notes is just centralized copy-paste.
- Some logic belongs in a normal script inside the repo. Reuse is not always worth the indirection.
The uncomfortable lesson is simple: new platform features usually fail at the handoff points. The vendor feature works. The identity mapping is incomplete. The backup restores but not the secret. The scanner finds an issue but nobody owns the fix. The autoscaler drains a zone correctly but the application has a bad disruption budget. These are not edge cases. They are where production work lives.
Security, Reliability, And Cost Tradeoffs
The maintainability win is less duplicated YAML and fewer one-off scripts. The cost is a new dependency path. If the platform team breaks a popular function, many repositories break at once. That is why release discipline matters more than clever syntax.
Use a scorecard before rolling the pattern to every team:
| Question | Good answer | Weak answer |
|---|---|---|
| Is ownership clear? | Function has owner, changelog, and support path | Nobody knows who can change it |
| Is rollback simple? | Projects can pin or revert to prior version | Only latest exists |
| Is behavior observable? | Logs and artifacts show function inputs and result | Failure says function failed |
The weak answers are not moral failures. They are just not production answers yet. If your current state is weak, write the gap down, choose the next smallest fix, and keep the change contained until the evidence improves.
First 48 Hours In Practice
The first two days decide whether GitLab Functions becomes a controlled platform improvement or another half-finished note in a chat thread. I would split the work into three windows: the first hour, the first business day, and the first week. The first hour is about scope. Do not change production yet unless the exposure is obvious. Name the owner, capture the source link, list affected systems, and decide whether this is emergency work or scheduled platform work.
By the end of the first business day, the team should have one working example. That could be one patched runner pool, one restored namespace, one repository review, one governed data domain, one EKS node group, or one shared VPC deployment. The exact target depends on the topic. The point is to choose a small production-shaped slice, not a toy. A lab that has no secrets, no real users, no deployment pressure, and no monitoring will hide the problems that matter.
The first-week goal is repeatability. If the change worked once because a senior engineer babysat it, you have a useful experiment, not a platform pattern. Turn the successful path into a runbook with commands, screenshots, expected output, rollback steps, and escalation rules. Then test it with someone who did not write the first version. That review will expose missing assumptions faster than another hour of polishing.
For reusable CI logic and where GitLab Functions fit beside includes, components, anchors, and scripts, the review meeting should be short and concrete. Ask what changed, which systems are in scope, which systems are intentionally out of scope, what evidence proves the control works, and what would make the team roll back. If the group cannot answer those five questions, the change is not ready to become a default.
| Owner | Decision to make | Evidence they should demand |
|---|---|---|
| Service owner | Confirms scope and business impact | Accepts or rejects the default action for YAML anchors |
| Platform owner | Turns the pattern into a shared control | Publishes the runbook, dashboard, and rollback path for GitLab Functions |
| Security owner | Reviews risk and exception handling | Checks that Duplicate shell blocks has usable evidence |
| FinOps or operations owner | Checks cost and toil | Watches whether Pinned function versions creates recurring work |
One practical habit helps a lot: write the rollback criteria before the rollout starts. For GitLab Functions, a rollback may mean re-enabling an old runner path, restoring a prior IAM policy, pausing an agent workflow, undoing an autoscaling setting, or reverting to a previous storage ownership model. Whatever the answer is, write it down. Operators make better decisions during incidents when the stop condition is already named.
Runbook Artifacts To Keep
A trustworthy runbook is not a wall of prose. It is a small set of artifacts that prove the system can be operated by more than one person. Keep the procedure, the evidence, and the exception list separate. Procedures change often. Evidence grows during exercises and incidents. Exceptions need owners and expiration dates because otherwise they become the real architecture.
| Artifact | What good looks like | Maintenance rule |
|---|---|---|
| Runbook page | One current procedure with commands, owners, and rollback | Update after every exercise or incident |
| Evidence folder | Screenshots, command output, logs, ticket IDs, and query results | Keep according to audit and incident policy |
| Exception register | Every skipped service, account, cluster, repo, or dataset | Owner plus expiration date required |
| Dashboard link | The live view operators use during rollout | Must show the metric in the control table |
The evidence should be boring enough to survive an audit and specific enough to help an engineer at 2 a.m. A command transcript showing how much unmanaged logic remains is useful. A dashboard screenshot with no time range is not. A ticket that says “verified” is weak. A ticket with the exact source, system, output, owner, and next review date is much stronger.
This also keeps trust resources honest. A blog post can point to AWS, Kubernetes, GitLab, or project documentation, but the local runbook has to say how your team interpreted that source. If the official document changes, the local procedure needs a review. If the source disappears, the team needs a replacement. That is why the trusted resources section at the end of this post is not decorative; it is part of the operating model.
Example Review Questions
Use these questions before making GitLab Functions a default pattern:
- What is the smallest system where we proved this works with production-like constraints?
- Which team owns the control after the initial rollout is finished?
- Which metric tells us the change helped instead of simply adding process?
- What is the first rollback action if a function that hides too much becomes a black box. keep inputs and outputs obvious.?
- What exception would we approve, and how long may that exception live?
- Which trusted source would force us to revisit the design if it changed?
Two questions deserve blunt answers. First, does the pattern reduce risk, or does it only move risk to another team? Second, can a new engineer follow the runbook without private context? If the answer to either question is no, keep the rollout narrow.
A Concrete Failure Scenario
Imagine the team accepts the default action for yaml anchors but ignores include. At first, the rollout looks successful. The dashboard turns green. The announcement is written. Then the first exception arrives. A service owner cannot meet the deadline, a cluster has an unusual constraint, or a repository breaks in a way the shared workflow did not predict. Without an exception register, the team handles that case in a side conversation. Two weeks later nobody remembers whether the exception was temporary.
That is the failure mode this article is trying to avoid. The technology can be good and the rollout can still decay. The fix is not more meetings. The fix is a small operating loop: define the default, record the exception, attach an owner, set an expiration date, and review the evidence. This is simple, but it is not optional for production work.
Do not put secrets into function parameters unless the handling is documented and logged safely. That gotcha should shape the rollout. Put it in the runbook as a check, not as a footnote. If a future operator has to rediscover it during an outage or audit review, the article failed to become operational knowledge.
When To Use This
Use this pattern when several projects need the same CI behavior and you want versioned, centrally maintained execution logic.
Do not use it when the logic is project-specific, only a few lines long, or depends heavily on local repository conventions. That boundary is important because the wrong abstraction can make a simple system harder to operate. Sometimes the best platform decision is to leave a feature out of the shared baseline and document a local exception instead.
Trusted Resources
These are the sources I would keep next to the runbook:
- GitLab CI/CD Functions
- GitLab CI/CD components
- GitLab include keyword
- GitLab CI/CD YAML reference
- GitLab job artifacts
- Open Container Initiative image spec
I am intentionally marking one uncertainty: GitLab Functions are documented as experimental, so syntax and operational behavior can change. Treat the article as an operating guide, not as a replacement for the vendor documentation. The source links above are the authority when a limit, feature state, or mitigation changes.
The Practical Takeaway
GitLab Functions are not a reason to rewrite every pipeline. They are a reason to stop copy-pasting the jobs your platform team already owns.
Comments