Posto
Posto

Reputation: 7550

gitlab ci bash script variable modification

In GitLab CI script I wanted to

  1. Remove any character other then numbers and dot (.) and
  2. remove all the text after 2nd dot [Ex 5.6.7.8 -> 5.6] if dot exists in text.

So for that I have tried to use sed , but it's not working in GitLab Script (working on bash shell locally)

export BRANCH_NAME={$CI_COMMIT_REF_NAME} | sed 's/\.[^.]*$//'  | sed 's/[^0-9.]//g'

any idea what is missing here ?

If sed is not going to work here then any other option to achieve the same?

Edit : As per @WiktorStribiżew - echo "$CI_COMMIT_REF_NAME" | sed 's/.[^.]$//' | sed 's/[^0-9.]//g' - export BRANCH_NAME=${CI_COMMIT_REF_NAME} | sed 's/.[^.]$//' | sed 's/[^0-9.]//g'

echo is working but export is not

Upvotes: 2

Views: 2008

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626870

First of all, to remove all text after and including second dot you need

sed 's/\([^.]*\.[^.]*\).*/\1/'

The sed 's/\.[^.]*$//' sed command removes the last dot and any text after it.

Next, you must have made a typo and you actually meant to write ${CI_COMMIT_REF_NAME} and not {$CI_COMMIT_REF_NAME}.

So, you might be better off with

export BRANCH_NAME=$(echo "$CI_COMMIT_REF_NAME" | sed 's/\([^.]*\.[^.]*\).*/\1/' | sed 's/[^0-9.]//g')

Upvotes: 0

sseLtaH
sseLtaH

Reputation: 11227

Using sed

$ export BRANCH_NAME=$(echo "$CI_COMMIT_REF_NAME" |sed 's/[A-Za-z]*//g;s/\([0-9]*\.[0-9]*\)\.[^ ]*/\1/g')

Upvotes: 1

Related Questions