[go: up one dir, main page]

Skip to content

Backend: Make it possible to identify a new/empty branch with workflow:rules

Solution

After implementing #293645 (closed) it appears like we have a working solution using this config

workflow:
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: always
    - if: $CI_COMMIT_BRANCH
      changes:
        paths: ['**/*']
        compare_to: master
      when: always

test:
  script: exit 0

Problem to Solve

Whenever a user creates a new branch it will be picked up by GitLab and will be post-processed. In turn, a pipeline will be created.

The pipeline could be useless in cases where there is nothing different from the origin, even considering .gitlab-ci.yml conditions.

Proposed solution

test:
  script: ./run tests
  rules:
    - if: $CI_COMMIT_BRANCH
      changes:
        paths:
          - *
        compare:
          branch: master

old Proposal (not relevant)

Click to expand

By having a new variable CI_BRANCH_COMMIT_COUNT that users could add to the workflow:rules condition, the variable will be defined based on a result of using a Git command, any pipeline using can then determine whether the branch is the same as the default branch.

An example for capturing a branch commit count could look like this:

git rev-list --count $CI_COMMIT_BRANCH ^DEFAULT_BRANCH (for regular pipelines)
git rev-list --count $CI_COMMIT_BRANCH ^CI_MERGE_REQUEST_TARGET_BRANCH_NAME (for MR pipelines)

This way users could add that to the pipeline configuration as

workflow:
  rules:
    - if: $CI_BRANCH_COMMIT_COUNT == 0
      when: never

...

in case they would like to avoid a pipeline to run when a new branch is created

This page may contain information related to upcoming products, features and functionality. It is important to note that the information presented is for informational purposes only, so please do not rely on the information for purchasing or planning purposes. Just like with all projects, the items mentioned on the page are subject to change or delay, and the development, release, and timing of any products, features, or functionality remain at the sole discretion of GitLab Inc.

Edited by Manuel Grabowski