regular expression for [STRING] in gitlab-ci.yml file - regex

I am tyring to set rule for deployment stage in gitlab-ci.yml file where if the git commit message is having a particular [STRING] in this format then it should deploy to that particular environment where this rule is written.
# Deploy to QAT environment
deploy-qat:
stage: deploy
extends: .helm_deploy
environment:
name: qat
tags:
- exe-prd
rules:
- if: $CI_COMMIT_MESSAGE =~ "/[QAT]$/|/[qat]$/" #&& $CI_COMMIT_REF_NAME == "example/qat"
when: always
I have wrote above rule however it is not working. I have tried below combinations of regular expressions however none of them are working.
"/\[QAT\]/|/\[qat\]/"
"/[QAT]/|/[qat]/"
"*\[QAT\]*|*\[qat\]*"
"\[\(QAT\|qat\)\]"
"\[\(QAT\|qat\)]"
"/\[(qat|QAT)\]/"
I tried following website for regular expression here which validates my requirement but it is not working inside gitlab-ci.yml file.

You can use
# Deploy to QAT environment
deploy-qat:
stage: deploy
extends: .helm_deploy
environment:
name: qat
tags:
- exe-prd
rules:
- if: $CI_COMMIT_MESSAGE =~ /\[(QAT|qat)]/
when: always
See more about how to format regex matching conditions at the rules:variables reference page.
NOTES:
/\[(QAT|qat)]/ should not be put inside quotes
You need to use /.../ regex literal syntax (the backslashes are regex delimiters)
\[(QAT|qat)] is a regex that matches [, then either QAT or qat, and then a ] char
=~ is a regex matching operator.

Try this block in your yml:
deploy-qat:
only:
message:
- /\[(qat|QAT)\]/

Related

Pattern Matching for rules parameter Gitlab CI

I am trying to make use of the rules: parameter to make a job only appear in a pipeline if specified users did the push. I dont want to define the list of users for each job, so I have the following global variable:
variables:
USER_LIST: "user1 user2 user3"
and in the job, I have the following:
rules:
- if '$USER_LIST =~ /$GITLAB_USER_LOGIN/'
when: on_success
- when: never
This does not appear to be working, as I suspect the regex pattern being used is not being replaced by the variable, and using $GITLAB_USER_LOGIN as the search string. If I use an explicit search:
rules:
- if '$USER_LIST =~ /user1/'
when: on_success
- when: never
then the pattern matches just fine.
NOTE: I am aware that GITLAB_USER_LOGIN is a protected variable. I get the same problem with GITLAB_USER_EMAIL too.
So the question is, how can I put a GITLAB predifined variable into a string that will be used for pattern matching?
You are using a variable and not a regular expression in your rule. So you don't need the slashes around the $GITLAB_USER_LOGIN variable. Try something like this:
rules:
- if '$USER_LIST =~ $GITLAB_USER_LOGIN'
I have more or less the same problem setup. The issues mentioned in the comments were addressed in 15.1, see https://docs.gitlab.com/ee/ci/jobs/job_control.html#store-the-regex-pattern-in-a-variable.
Nevertheless '$USER_LIST =~ /$GITLAB_USER_LOGIN/' still doesn't work. Also, if you define a variable like pattern: /$GITLAB_USER_LOGIN/ and use that in the way described in the documentation, it doesn't work.
'$USER_LIST =~ $pattern'
What works, but isn't the solution to your or my problem is defining pattern with a fixed pattern, like pattern: /user1/. Then it works if used like this:
'$USER_LIST =~ $pattern'
But obviously, I want to use the actual user, that tries to create a pipeline.
What works on my instance (15.4) is this:
'$GITLAB_USER_LOGIN =~ $USER_LIST'
I have no idea why, cos in my opinion this is no behaviour suggested by the documentation. I looked around in several places and found no documented examples of this use, but it works.
This job gets created with a delayed start:
variables:
pattern: "user"
string: "this contains user and other stuff"
show-env:
stage: env
tags:
- ops
rules:
- if: $pattern =~ $string
when: delayed
start_in: 15m
- when: always
script:
- env
And if you remove user from string and push it again, it gets executed right away.
And it also works when I define USERLIST in the project ci/cd variables and test that against $GITLAB_USER_LOGIN.
The documentation shows that the content matching regex should look like this:
$VARIABLE =~ /^content.*/
for your code you need to add ^ and .*:
rules:
- if '$USER_LIST =~ /^user1.*/'
when: on_success
- when: never
My workaround is to build an dynamic-child-pipeline where the variable within pattern already evaluated and inserted as constant string.

Using regular expressions in Prometheus relabel_configs source_labels

I'm trying to save the meta labels retrieved from EC2 Service Discovery as target labels. I'm mostly concerned about the tags - every instance contains a lot of them and I would love to gather them all using one simple configuration entry with the usage of regular expression.
The perfect solution seems to be something like this:
relabel_configs:
- source_labels:
- '__meta_ec2_tag_(.*)'
target_label: '### use extracted regex group here'
Unfortunately, I get the following error:
\"__meta_ec2_tag_(.*)\" is not a valid label name"
Does that mean that I can't use regular expressions to describe source labels and that I have to specify each source label separately like in the sample below?
- source_labels:
- '__meta_ec2_tag_Name'
target_label: 'instance_name'
- source_labels:
- '__meta_ec2_tag_environment'
target_label: 'environment'
- source_labels:
- '__meta_ec2_tag_project'
target_label: 'project'
Try this:
relabel_configs:
- regex: '__meta_ec2_tag_(.*)'
replacement: $1
I just encountered the same problem but the previous answer didn't work for me with this error :
relabel configuration for replace action requires 'target_label' value
I found out that Prometheus now has a labelmap option for relabel_configs that does this : https://grafana.com/blog/2022/03/21/how-relabeling-in-prometheus-works/#labelmap
TLDR;
relabel_configs:
- action: labelmap
regex: "__meta_ec2_tag_(.*)"
replacement: "$1"

Using a new line character in the ansible find module

Trying to add a valid python regex as a parameter for the patterns option in the find module, but it's not working.
tasks:
- name: Obtain a list of auto* files in /etc
find:
path: /etc
patterns: "^auto(_|\\.)([a-zA-Z]+\n)"
use_regex: yes
register: etc_auto_files
The problem appears to be with the "\n", I have tried multiple backslashes, single quotes, and square brackets, but haven't been able to get it working.
I am running ansible 2.3.2.0.
Try (not tested)
patterns: "^auto(_|\\.)([a-zA-Z]+{{'\n'}})"
Solved this. The pattern was meant to match filenames that are as follows:
/etc/auto_(one or more letters, end of filename)
/etc/auto.(one or more letters, end of filename)
The following filenames are examples that would match the pattern:
/etc/auto.master or /etc/auto_master
The following filenames are examples that would not match the pattern:
/etc/old.auto.master
/etc/old.auto_master
/etc/auto.master.20180101
/etc/auto_master.20180101
The ansible pattern that ended up working:
"^auto(_|\\.)([a-zA-Z]+)$"
Here is how the pattern was successfully used in a find task:
- name: Obtain a list of auto* files in /etc
find:
path: /etc
patterns:
- "^auto(_|\\.)([a-zA-Z]+)$"
- "^fstab$"
- "^vfstab$"
use_regex: yes
register: etc_auto_files
Thanks to those who commented and attempted to answer this question.

regex for Ansible replace module?

I'm trying to disable my default /etc/yum.repos.d/*.repo files by setting the line
enabled=1
to
enabled=0
Easy enough with Ansible's replace module. However, some *.repo files have
enabled=1
while some have
enabled = 1
that is, some have no space on each side of the = sign, while others have. What should the regex value be in this task to handle both?
- name: Disable the existing CentOS repos in /etc/yum.repos.d
replace:
dest: /etc/yum.repos.d/{{ item }}
regexp: "enabled = 1" ####### What should this be?? ########
replace: "enabled=0"
with_items:
- CentOS-Base.repo
- CentOS-fasttrack.repo
- CentOS-Vault.repo
- CentOS-CR.repo
The regexp parameter in the replace module uses Python regular expressions. All you need to do is add zero or more qualifiers (*) for whitespace (\s) between the equals sign.
- name: Disable the existing CentOS repos in /etc/yum.repos.d
replace:
dest: /etc/yum.repos.d/{{ item }}
regexp: 'enabled(\s)*=(\s)*1'
replace: "enabled=0"
with_items:
- CentOS-Base.repo
- CentOS-fasttrack.repo
- CentOS-Vault.repo
- CentOS-CR.repo

How to specify case insensitive mode in ansible lineinfile regexp?

Simple question.
I'm trying to match "UseDns", "usedns" and other variations.
- name: Disable DNS checking on login (huge speedup)
sudo: true
lineinfile:
dest: "/etc/ssh/sshd_config"
regexp: "^[# \t]*[Uu][Ss][Ee][Dd][Nn][Ss] "
# how does one specify case insensitive regexp in lineinfile?
line: "UseDNS no"
state: "present"
create: true
insertafter: EOF
notify:
- sshd restart
Ansible uses Python re module. You can use inline modifiers, such as (?ism) in your pattern. Use the i for case-insensitive matching:
regexp: "(?i)^[# \t]*usedns "
Inline modifiers apply to the part of the regular experssion to the right of the modifier, and can be disabled with a - e.g. (?-i). This can be applied to implement case-insensitivity to only a part of a regular expression.
For example, the regex (?i)use(?-i)DNS should match useDNS and UseDNS, but not useDns or USEdns.