I'm trying to figure out the regex pattern usage in GitLab in order to
Prevent any branch that does not follow the naming convention from be pushed up
Specify for example only these name formats can be used and at least 1 must be used: ^(bug)?(release)?(feature)?/.*\n
Has anyone done this with GitLab and can assist?
Tried the 'Settings > Repository > Protected branches > Protect a branch wildcard' to try and do this but it does not appear to work. I get 0 matches
Protected branch rule patterns do not support regex. Wildcard (*) is the only supported metacharacter. If you want a similar effect to you have in your regex ^(bug|release|feature).*, you will need to make multiple protected branch rules.
For example, you could make three rules:
bug*
feature*
release*
I have a build step in Gitlab CI/CD that I would like to skip if the only changing file is the readme file. My understanding is each entry listed under rules:changes is additive.
Additionally, the repository is set up as a "monorepo", in that there are different sub repositories listed under the packages directory.
Is there a way to specify under the rules section that there must be at least one changed file within packages/foo/, besides a change to packages/foo/readme.md?
rules:changes: takes glob patterns. And yes, your understanding is correct: by nature, glob patterns are inclusive only, so you cannot use add exclusionary parameters to a glob pattern or "negate" specific items that would otherwise match. Adding additional items to the changes: is also additive.
You can, however use negative-matching to include non-matching files (e.g., !(foo|bar|baz)). This should work for your use case.
So, a rule like this should work how you want:
rules:
- changes:
- "packages/foo/**/!(readme.md)"
Edit:
However, ruby's fnmatch doesn't support the ! metacharacter, so instead you can use the pattern:
packages/foo/**/{[^r]*,r,r[^e]*,re,re[^a]*,rea,rea[^d]*,read,read[^m]*,readm,readm[^e]*,readme,readme[^.]*,readme.,readme.[^m]*,readme.m,readme.m[^d]*,readme.md?*}
reference
Which should have the same effect.
rules:
- changes:
# same as "packages/foo/**/!(readme.md)"
# SEE: https://stackoverflow.com/a/69906355/5747944
- 'packages/foo/**/{[^r]*,r,r[^e]*,re,re[^a]*,rea,rea[^d]*,read,read[^m]*,readm,readm[^e]*,readme,readme[^.]*,readme.,readme.[^m]*,readme.m,readme.m[^d]*,readme.md?*}'
Tested in irb:
irb(main):011:0> pattern = './packages/foo/**/{[^r]*,r,r[^e]*,re,re[^a]*,rea,rea[^d]*,read,read[^m]*,readm,readm[^e]*,readme,readme[^.]*,readme.,readme.[^m]*,readme.m,readme.m[^d]*,readme.md?*}'
irb(main):012:0> File.fnmatch(pattern, './packages/foo/readme.md', File::FNM_PATHNAME | File::FNM_DOTMATCH | File::FNM_EXTGLOB)
=> false
irb(main):013:0> File.fnmatch(pattern, './packages/foo/anything-else', File::FNM_PATHNAME | File::FNM_DOTMATCH | File::FNM_EXTGLOB)
=> true
in the answer of this (Trigger Gitlab-CI Pipeline only when there is a new tag ), I understand to set only as filter
But How can I trigger the pipeline only:
there is a new tag created, these tags are semantic versioning tags, how can I filter them? something like
only:
- /[0-9]+\.[0-9]+\.[0-9]+/
but the tags are not always as 1.0.2, sometimes, they have beta or fc, such as 0.2.3-rc.1 or 2.3.5-beta, will the filter to be /[0-9]+\.[0-9]+\.[0-9]+.*/ is fine?
second rule is, the trigger is only on master or main branch
Regarding the Semantic versioning regex, I found this gist
https://gist.github.com/jhorsman/62eeea161a13b80e39f5249281e17c39
the sample is too complex
^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$
Using only is deprecated in favor of rules.
rules replaces only/except and they can’t be used together in the same job.
https://docs.gitlab.com/ee/ci/yaml/#rules
Create a rule and check the predefined variable CI_COMMIT_TAG for your pattern.
job:
script: echo "Hello, Rules!"
rules:
- if: $CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+.*/
You can list multiple of these rules. So if you want to execute the job also on every push to the master branch, you'd add another rule:
rules:
- if: ...
- if: $CI_COMMIT_REF_NAME == "master"
The extended pattern with .* at the end would be fine, but you could also be a bit more specific like this (-[0-9a-zA-Z.]+)? if you want to enforce a specific syntax.
I'm wondering if you can use wildcard characters with tags to get all tagged scenarios/features that match a certain pattern.
For example, I've used 17 unique tags on many scenarios throughout many of my feature files. The pattern is "#jira=CIS-" followed by 4 numbers, like #jira=CIS-1234 and #jira=CIS-5678.
I'm hoping I can use a wildcard character or something that will find all of the matches for me.
I want to be able to exclude them from being run, when I run all of my features/scenarios.
I've tried the follow:
--tags ~#jira
--tags ~#jira*
--tags ~#jira=*
--tags ~#jira=
Unfortunately none have given my the results I wanted. I was only able to exclude them when I used the exact tag, ex. ~#jira=CIS-1234. It's not a good solution to have to add each single one (of the 17 different tags) to the command line. These tags can change frequently, with new ones being added and old ones being removed, plus it would make for one real long command.
Yes. First read this - there is this un-documented expression-language (based on JS) for advanced tag selction based on the #key=val1,val2 form: https://stackoverflow.com/a/67219165/143475
So you should be able to do this:
valuesFor('#jira').isPresent
And even (here s will be a string, on which you can even do JS regex if you know how):
valuesFor('#jira').isEach(s => s.startsWith('CIS-'))
Would be great to get your confirmation and then this thread itself can help others and we can add it to the docs at some point.
I have a gitlab repo and I am trying to create some tags on top of required commits. But my CICD library (included) is having a job (job1) in a stage which runs on every tag/branch/commit. I want to exclude it to run on particular formats of tags. (eg. pre_final-2.3.1).
I tried giving the tag pattern in my .gitlab-ci.yaml in the except section of the job as below.
Eg:
job1:
except:
- ^pre_final-\d+\.\d+\.\d+$
It is still adding the job job1 to the pipeline for the CICD builds running on this tag. I believe this pattern is checking with branch name. But is there a way to mention the ref we provide in the except section is branch or tag?
Ref: https://docs.gitlab.com/ee/ci/yaml/#onlyrefs--exceptrefs
You should rules instead as only/except is not developed further. There you can check for the the name of your tag.
job1:
rules:
- if: '$CI_COMMIT_TAG =~ /^pre_final-\d+\.\d+\.\d+$/'
when: never
- when: always
There is no separate classification as branch or tag while using them in only/except.
I realized that I am giving a wrong regex pattern and thus it is not working for the tag pattern I am trying for
^pre_final-\d+\.\d+\.\d+$ --> /^pre_final-\d+\.\d+\.\d+$/
So my .gitlab-ci.yaml contains as below
job1:
except:
# matches with branch or tags names
- /^pre_final-\d+\.\d+\.\d+$/
Since above usage matches both branch and tag names, if we want to match only branch name or match only tag name, then we can use the below format
job1:
except:
variables:
# to match only tag names
- $CI_COMMIT_TAG == /^pre_final-\d+\.\d+\.\d+$/
# to match only branch names
- $CI_COMMIT_BRANCH = /^pre_final-\d+\.\d+\.\d+$/