Skip to main content

Pipeline Values You Can Use in Context Expression Restrictions

Context expression restriction examples are illustrations, not a whitelist. You can restrict a context by branch, repository name, owner, SSH, and other pipeline values, and you can combine them.

Overview

A context expression restriction can use almost any pipeline value, not only the examples on the Using contexts page.

Those examples usually show pipeline.git.branch, job.ssh.enabled, and pipeline.config_source. They are illustrations. They are not the only options.

The only pipeline values you cannot use are pipeline.trigger_parameters.* and pipeline.parameters.*. job.ssh.enabled is also allowed.

If the expression does not match, or a named value is unset, the job that uses the context is unauthorized and does not start.

Ways to restrict a context

Add one expression at Organization Settings → Contexts → [context] → Add Expression Restriction. Organization administrator permission is required. Multiple expression rows on one context are combined with and. Replace a row unless you intend both to apply.

These are equivalent patterns. Pick or combine them.

By branch (the usual documentation example):

pipeline.git.branch == "main"

By repository name (name only, no owner prefix: my-app, not my-org/my-app):

pipeline.git.repo_name == "my-app"

By repository name prefix:

pipeline.git.repo_name starts-with "circleci-orb-"

By repository owner or organization:

pipeline.git.repo_owner == "my-org"

Combined (one repo on main, or any branch of matching prefixes):

(pipeline.git.repo_name == "my-app" and pipeline.git.branch == "main")
or (pipeline.git.repo_name starts-with "circleci-orb-")

Combined, plus owner:

pipeline.git.repo_owner == "my-org"
and (
  (pipeline.git.repo_name == "my-app" and pipeline.git.branch == "main")
  or (pipeline.git.repo_name starts-with "circleci-orb-")
)

Optional extras already shown in public docs: block SSH reruns, and block configuration whose source starts with api (including unversioned configuration). Group the or so the extras apply to the whole rule:

(
  (pipeline.git.repo_name == "my-app" and pipeline.git.branch == "main")
  or (pipeline.git.repo_name starts-with "circleci-orb-")
)
and not job.ssh.enabled
and not (pipeline.config_source starts-with "api")

Outcome

  • Expression matches: the job starts and receives the context.

  • Expression does not match, or a value is unset: the job is unauthorized and never starts. Other jobs in the workflow that do not use the context can still run.

  • With and not job.ssh.enabled, a normal matching run still succeeds. Rerun job with SSH is unauthorized.

Additional Notes

This is supported on CircleCI Cloud for GitHub App, GitHub OAuth, and Bitbucket Cloud. Some pipeline values, including pipeline.git.repo_name, are not Server pipeline values. An unset value fails closed (unauthorized).

This is not a security defect. The docs already allow pipeline values other than the two exclusions above.

Expression restrictions are not config policies (OPA/Rego) and not .circleci/config.yml job filters or workflow when. Those are different products. See the links below.

Additional Resources

Did this answer your question?