Have .gitlab-ci.yml in another branch but trigger pipelines for all commits

I work on a project that is hosted upstream (and I have also stored on my local gitlab installation).
Now I want to define a CI/CD configuration only for my local gitlab, but that file should never be uploaded upstream or should be part if branches that I will later merge into main branch.
I will commit to several branches on my local gitlab and every push to a branch that is not main should trigger the CI/CD pipeline.

The idea is now to have the .gitlab-ci.yml in an extra branch (like ci-cd-config) that is never pushed upstream.

If I need to test commits, I would like to create new branches like fix/feature-1.
The push should trigger now the CI/CD pipeline, like it is defined in file .gitlab-ci.yml which only exists in branch ci-cd-config.

But I cannot define in the CI/CD settings inside the project from where the .gitlab-ci.yml should be used.
I only see the setting: Settings → CI/CD → General pipelines → CI/CD configuration file
But it seems that I cannot tell gitlab to use the file from a very specific branch.

Does anyone have a tip for me, how I can do this?

I use Gitlab 16.6.1.

Thanks a lot
Matthias

If I understand it correctly, you push your local Git changes into two remotes - upstream, and local network GitLab servers.

To avoid maintaining different branches and .gitlab-ci.yml files, I’d suggest looking into workflow:rules to control when pipelines and jobs should be running. GitLab CI/CD `workflow` keyword | GitLab

Add a rule that checks for a CI/CD variable value, and if matched, it runs the pipeline.


workflow:
  rules:
    - if: $MY_ENV == "local"

On your local network GitLab server, navigate into the project settings and define the CI/CD variable. Upstream server stays blank. GitLab CI/CD variables | GitLab

At first thanks a lot for your answer.

Let my phrase my question different and bring also a solution that is working for me.
In the repository there is a branch called main which is synced to upstream repository.

I have in my local repository (and on my hosted gitlab) several other branches.

The .gitlab-ci.yml most not exist in the main branch and therefor must also not exist in any other branch where changes are commited (because a merge of these branches to main would bring the .gitlab-ci.yml then to the main branch which is not wanted).

My first idea was to have a dedicated branch that holds the .gitlab-ci.yml, but I could not found a way to tell gitlab to use the .gitlab-ci.yml from that specific branch to build all commits on all branches.

I tested now successfully another approach.

I created now a completely new repository that only have the .gitlab-ci.yml file and configured gitlab to use this repository/file (there is a configuration option available to point to a remote repository for the pipeline definition).

It is working fine so far.
Only problem is now, that the pipeline is also executed for commits in branch main, which I do not want.
There I need to read a little bit more in the documentation to tell gitlab to run the pipeline not for changes on branch main.

Great idea. This also helps with compliance needs, i.e. that no-one in the project can edit the CI/CD pipeline, and all jobs are always being run.

Only problem is now, that the pipeline is also executed for commits in branch main, which I do not want.
There I need to read a little bit more in the documentation to tell gitlab to run the pipeline not for changes on branch main.

A similar approach to my suggestion above is possible with rules (on the job level) and workflow:rules (global). You’ll need to use a pre-defined CI/CD variable that provides the current Git branch name, and compare it to the main branch.

workflow:
  rules:
    - if: $CI_COMMIT_BRANCH != "main"

Since some repositories might use a different default branch, I’d suggest using the CI_DEFAULT_BRANCH variable to stay safe.

workflow:
  rules:
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH

Edit: Added missing hyphens for rules:if.

Thanks, I think the syntax of rules is that is expects an array.
There I found another problem in gitlab.
As the file (.gitlab-ci.yml) is now hosted on a different repository I expected if I click the Go to the pipeline editor:

That it jumps to the repository that is defined as the destination for the file:

But it tries to open the ci-editor in the current repository and not in the remote repository:

Maybe a suggestion to fix this in a future version?

For the pipeline script part.
Just tested and this works:

build-job:
  rules:
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH

Thanks a lot for your amazing answers!

1 Like

Whoops. I forgot the leading hyphen to make the rules an array, sorry. I wrote the rules from memory, did not test them. Updated my post above.