Search code examples
gitlabgitlab-cigitlab-ci-runner

Get the value from secret variables in GitLab when concatenated with other variable


I have variable name URL_DEV in Gitlab Variables (Settings > CI/CD > Variables). I want to get the value using ${CI_ENVIRONMENT_NAME}. Example : echo URL_${CI_ENVIRONMENT_NAME} should provide the value from Gitlab Variables, but it is giving output as URL_DEV but not printing the value.

#Tried below commands
$ echo $URL_${CI_ENVIRONMENT_NAME}
output: DEV

$APP="URL_${CI_ENVIRONMENT_NAME}"
echo $APP
output: URL_DEV

$APP="URL_${CI_ENVIRONMENT_NAME}"
echo $(echo $APP)
output: URL_DEV

Expected Output should be value from the variables

echo $URL_${CI_ENVIRONMENT_NAME}
expected output: https://www.example.com

Solution

  • You need to use bash indirect expension. This snippet works :

    URL_DEV="https://google.com"
    ENV="DEV"
    SITE="URL_"$ENV
    echo $SITE
    URL_DEV
    
    curl -k ${!SITE}
    
    <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
    <TITLE>301 Moved</TITLE></HEAD><BODY>
    <H1>301 Moved</H1>
    The document has moved
    <A HREF="https://www.google.com/">here</A>.
    </BODY></HTML>