Gitlab only and except not working simultaneously - gitlab

I have written a Git pipeline gitlab.yaml, where I am having both except and only rules.
I have a design/ folder which I am ignoring for most of the jobs and, except this folder, all my pipeline should get executed.
only:
refs:
- master
except:
changes:
- design/*
But: when I make changes to design folder file, and other files, then all the jobs are not getting executed.

Note: depending on your GitLab version (or with gitlab.com), you need to rewrite your exclusion rules using rules:
See "Transitioning your only/except syntax to rules".
The question "Gitlab CI how to ignore directory using rules syntax?".
However, as illustrated with gitlab-org/gitlab issue 301070, this new syntax does not solve your issue.
Currently it seems not to be possible to use rules:changes: to trigger a job when certain globs match, ignoring other sets of globs.
So for now, this is still being discussed/fixed.
You can try this glob pattern:
job:
rules:
- if: $CI_COMMIT_BRANCH == "master"
changes:
- '[!design]*/**'

Related

GitLab Using $CI_ENVIRONMENT_NAME with rules:changes fails

$CI_ENVIRONMENT_NAME doesn't work with rules:changes. Is it expected behaviour?
rules:
- changes: # Include the job and set to when:manual if any of the follow paths match a modified file.
- authurl/config/$CI_ENVIRONMENT_NAME/*
when: manual
allow_failure: false
As soon as the variable replaced with a static value the rule stars working.
The docs mention that $CI_ENVIRONMENT_NAME is available only if environment:name is set.
For expected behavior, you might have to add the environment value to the job.

How to prevent a job from running when the branch is different from release/

I am working with gitlab ci and I need the job to run only when the branch is release/ and if it is any other branch the job will not run.
I have tried to do it in many ways but none of them works, I would appreciate if you could help me, because I don't know what else to try.
Have a look at the rules block and common if clauses for rules.
For your specific question adding the following should work:
rules:
- if: $CI_COMMIT_BRANCH =~ /^release/

Split GitLab CI Jobs into multiple files

I've been trying to get more familiar with GitLab's CI functionality and find the idea of splitting up a CI pipeline into multiple separate jobs interesting. This would allow me to maintain one project of "known jobs" and include them in other projects.
So far, I have something like this:
$ ls
jobA.yaml jobB.yaml jobC.yaml jobD.yaml
Those 4 are all identical (for now), and have the following:
job-name:
stage: my-stage # Might be needed to differentiate later on
tags: runner-tag # used to figure out where/how the job should be done: directly on a server, in a container, etc
script:
- echo "beep beep"
In the actual .gitlab-ci.yaml I want to use, I would then (I think) put something like this. In this case, I would use the jobs defined in the project for itself:
include:
project: '$CI_PROJECT_PATH'
file: "*.yaml"
stages:
- my-stage
That gives me back a linter error though. Perhaps I'm misreading the documentation, but I think that should be possible somehow....
This should be a comment, but can't put formatted code in there..
We use a main yml, which just include all the others. It is not wildcards like you have.
Have you tried changing "file" to "local"? with the leading "- "?
include:
- template: Code-Quality.gitlab-ci.yml
- local: '/.gitlab/py.yml'
- local: '/.gitlab/static.yml'
- local: '/.gitlab/lint.yml'
- local: '/.gitlab/docs.yml'
- local: '/.gitlab/publish.yml'
According to the docs, wildcard includes are only possible with local. Furthermore you need to move your jobA.yaml to a directory as otherwise you will include your .gitlab-ci.yml as well with a wildcard on the top level.
So the following works with JobA.yaml in config:
include:
- local: 'config/*.yaml'
stages:
- my-stage

gitlab-ci.yml cannot resolve predefined environment variables

In my .gitlab-ci.yml i'm trying to deploy on merge request.
My pipeline works, script is executed , everything is ok and running , but i'm not able to read any predefined environment variables. My files looks like :
executeAutomationTests:
stage: check
only:
refs:
- merge_requests
script:
- echo $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
But $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME is not resolved. I need to know source branch for the merge_request in order to pull the code and make a deployment. I have tried other variables like : $CI_COMMIT_REF_NAME, $CI_JOB_STAGE , but non of them are resolved.
My GitLab version : GitLab Community Edition 13.4.2
The problem was that in the pipeline job monitor the variable wasn't resolved and that's confused me. It was looking like :
So, for anyone who is fighting with such things, keep that in mind and also, keep in mind differences in syntax between powershell , bash and so on ..

How to run CI job only if two files have been changed? [not one or another]

I have task to create simple GitLab CI file to run a job only if two files have been changed. In official GitLab docs I have not found nothing about this. I need to run a job only if file A changed and also file B changed.
In this case, you don't have to change by yourself, every time if you change/commit your File A and File B, GitLab CI will allow your files to automatically run as a job
Option A - Only/Except https://docs.gitlab.com/ee/ci/yaml/#onlyexcept-advanced
This is the correct way to do it
your-step:
script:
- your-script.sh
only:
changes:
- "file*.extension" # I don't know the pattern of your files
Option B - Using shell script
You can do anything with shell script (or any other language)
in this link https://gitlab.com/gitlab-org/gitlab-foss/-/issues/19813 you will see a lot of options to do it. Just try if something fits to your purpose
Option C - Using gitlab rules https://docs.gitlab.com/ee/ci/yaml/#rules
In the example below, the job will be triggered if any of the conditions match
your-step:
script:
- your-script.sh
rules:
- changes:
- fileA
- file B
- dir-with-both-files/*

Resources