Home > Mobile >  gitlab ci bash script variable modification
gitlab ci bash script variable modification

Time:01-12

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

CodePudding user response:

Using sed

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

CodePudding user response:

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')
  •  Tags:  
  • Related