String manipulation in .gitlab-ci variables

Viewed 1211

I'm trying to set up my ci file with some variables. I'm able to generate a variable like so;

...
variables:
    TARGET_PROJECT_DIR: "${CI_PROJECT_NAME}.git"

However, I don't seem to be able to do this;

...
variables:
    PROJECT_PROTOCOL_RELATIVE_URL: "${CI_PROJECT_URL//https:\/\/}.git"

If I run that in bash, I get the expected output which is gitlab.com/my/repo/url.git with the 'https://' removed and the '.git' appended.

My workaround has just been to export it in the 'script' section, but it feels a lot neater to add this to the variables section, since this is part of a template that is being inherited by the actual jobs. Is it possible?

1 Answers

After looking around for similar challenges I found your not answered question. Here are my suggestions:

    stages:          
      - todo

    todo-job:
      stage: todo
    only:
      - master
    script:
      #your question / example
      - echo ${CI_PROJECT_URL}
      - echo ${CI_PROJECT_URL:8:100}.git

      #Because you have the word manipulation in the title, I have some more examples:

      #Return substring between the two '_' 
      - INPUT="someletters_12345_moreleters.ext"
      - SUBSTRING=`expr match "$INPUT" '.*_\([[:digit:]]*\)_.*' `  
      - echo $SUBSTRING
    
      #Store a substring in a new variable and create an output
      - b=${INPUT:12:5}
      - echo $b
    
      #Substring using grep with regex (more readable)
      - your_number=$(echo "someletters_12345_moreleters.ext" | grep -E -o '[0-9]{5}')
      - echo $your_number

      #Substring using variable and 'grep' with regex (more readable)
      - your_number=$(echo "$INPUT" | grep -E -o '[0-9]{5}')
      - echo $your_number

      #split a string and return a part using 'cut'
      - your_id=$(echo "Release V14_TEST-42" | cut -d "_" -f2 )
      - echo $your_id

      #split the string of a variable and return a part using 'cut'
      - VAR="Release V14_TEST-42"
      - your_number=$(echo "$VAR" | cut -d "_" -f2 )
      - echo $your_number

Gitlab output looks like:

    $ echo ${CI_PROJECT_URL}
    https://gitlab.com/XXXXXXXXXX/gitlab_related_projects/test
    $ echo ${CI_PROJECT_URL:8:100}.git
    gitlab.com/XXXXXXXXXX/gitlab_related_projects/test.git
    $ INPUT="someletters_12345_moreleters.ext"
    $ SUBSTRING=`expr match "$INPUT" '.*_\([[:digit:]]*\)_.*' `
    $ echo $SUBSTRING
    12345
    $ b=${INPUT:12:5}
    $ echo $b
    12345
    $ your_number=$(echo "someletters_12345_moreleters.ext" | grep -E -o '[0-9]{5}')
    $ echo $your_number
    12345
    $ your_number=$(echo "$INPUT" | grep -E -o '[0-9]{5}')
    $ echo $your_number
    12345
    $ your_number=$(echo "Release V14_TEST-42" | cut -d "_" -f2 )
    $ echo $your_number
    TEST-42
    $ VAR="Release V14_TEST-42"
    $ your_number=$(echo "$VAR" | cut -d "_" -f2 )
    $ echo $your_number
    TEST-42
    Cleaning up project directory and file based variables
    00:01
    Job succeeded
Related