GitLab GitLab RulesGitLab WorkflowGitLab YAMLPipeline Conditions

Article & Tutorial

GitLab CI/CD Rules, Workflow, Only and Except Explained

Control pipeline and job creation with workflow rules, job rules, conditions, changes, schedules, manual actions, and migration from legacy only and except syntax.

CI decision engine evaluating branches, schedules, changes, and approvals before jobs run
CI decision engine evaluating branches, schedules, changes, and approvals before jobs run
Contents

GitLab provides two major decision layers: workflow rules determine whether a pipeline is created, while job rules determine which jobs are added and how they behave. Older only and except syntax still appears in existing configurations, but new designs should prefer rules.

Decision layers

MechanismScopeTypical question
workflow: rulesEntire pipelineShould this pipeline exist for this event?
Job rulesOne jobShould this job be added, and with which behavior?
only / exceptLegacy job selectionWhich references or events include or exclude the job?

Rules are evaluated in order

The first matching rule determines the job’s inclusion and relevant attributes. Put specific conditions before broad fallbacks, and make the final behavior explicit. A permissive last rule can create jobs in pipeline sources you did not intend.

Common conditions

  • Branch or tag context.
  • Merge-request pipeline source.
  • Scheduled, triggered, or web-created pipeline.
  • File changes relevant to a component.
  • Existence of files in the repository.
  • Variable values for approved modes.
  • Manual action for controlled deployment.

Avoid duplicate pipelines

A branch push associated with a merge request can produce more than one pipeline if workflow and job rules are not coordinated. Decide which pipeline types the project needs, then encode that policy at workflow level before defining job-specific behavior.

Manual jobs and approvals

A manual job is a control point, not a complete authorization system. Protect environments and branches, restrict who can run deployment jobs, use least-privilege credentials, and record the deployed artifact and approver.

Migrate from only and except

  1. Document existing behavior for branches, tags, schedules, and merge requests.
  2. Create representative test cases for every pipeline source.
  3. Translate one job at a time to rules.
  4. Inspect the merged configuration and simulated outcomes.
  5. Run on non-production references.
  6. Remove old syntax only after behavior matches.

Review checklist

  • Every intended pipeline source is covered.
  • Untrusted pipelines cannot access protected secrets.
  • Change-based rules handle new and deleted files.
  • Schedule conditions are separate from push behavior.
  • A clear fallback prevents unexpected inclusion.
  • Production jobs have explicit authorization and environment protection.

GitLab job rules documentation

Example: merge-request pipelines and default-branch builds

This example creates a merge-request pipeline for review, avoids an additional branch pipeline when an open merge request exists, and permits a push pipeline on the default branch. It also demonstrates how a job can use rules independently of workflow:rules.

.gitlab-ci.yml
workflow:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: '$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS'
      when: never
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
    - when: never

stages:
  - test

unit_tests:
  stage: test
  image: alpine:3.20
  script:
    - echo 'Run project tests here'
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
    - when: never

This is a policy example, not a universal configuration: it intentionally suppresses feature-branch push pipelines without a merge request. If your project requires those pipelines, add a specific branch rule before the fallback. The script is a placeholder to replace with your real test command; no test execution is claimed.

How rules evaluate: first match wins

EventWorkflow decisionunit_tests
Merge request pipelineCreateInclude
Push to default branchCreateInclude
Push to branch with open merge requestSkip duplicate branch pipelineNot created
Scheduled pipelineSkip in this exampleNot created
Push to other feature branch without merge requestSkip in this exampleNot created

Example: run a manual deployment only on the default branch

.gitlab-ci.yml
deploy_production:
  stage: deploy
  script:
    - ./scripts/deploy.sh
  environment:
    name: production
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
      when: manual
      allow_failure: false
    - when: never

A manual job alone is not access control. Restrict production environment deployment permissions and tokens, and replace ./scripts/deploy.sh with your reviewed deployment process. Add the deploy stage to your stages list before using this standalone fragment.

Validate before modifying production pipelines

  1. Copy the proposal into GitLab CI Lint and inspect its simulation for merge requests, pushes and scheduled events.
  2. Run on a temporary test branch in your project; inspect which pipeline types appear and which jobs are included.
  3. Check that no untrusted merge-request job can access protected secrets or deployment credentials.
  4. Verify the actual job command; placeholder echo lines are not tests.
  5. After merging, review any duplicate pipeline warnings and compare expected versus actual pipeline results.

Further reading: https://docs.gitlab.com/ci/jobs/job_rules/ and https://docs.gitlab.com/ci/yaml/workflow/ . Melochip: https://melochip.com/posts/gitlab-artifacts-vs-cache-explained and https://melochip.com/posts/gitlab-ci-cd-troubleshooting-guide .

Melochip GitLab artifacts and cache

Did this help?

Yes No

Join the discussion

0 Comments

Ask questions, add context, or share what worked.

Sign in to join the discussion

No comments yet. Start the discussion.

More from Melochip Editorial

Related articles

Stay in the loop. Get new Melochip articles

Receive Melochip article updates when new articles are available. Explicit opt-in only.

No pre-checked boxes. Unsubscribe support is part of the subscriber foundation.