10

We have 2 environments (development, production). Per default we deploy to production but if the branch name or the commit message start with dev we deploy to development environment.

I create variables (app_url and app_url_dev) via gitlab GUI : (Project => Settings => CI/CD => Variables)

I use gitlab rule to change the app_url variable of deployement. The script deploy.py (and many other scripts) are using app_url variable.

The gitlab-ci code is:

Tags:
  stage: Deploy code
  rules:
  #    - if: $CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_MESSAGE =~ /^dev/
      - if: $CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_BRANCH =~ /^dev/
        variables:
          app_url: $app_url_dev
  script:
  - Python deploy.py 

The problem is that i never get the value of app_url_dev in app_url. I tried other combinaison but i always get the production url and not the dev one:

app_url: ${app_url_dev}
app_url: app_url_dev

But i get always the app_url and not the app_url_dev. Anyone can help please ?

2 Answers 2

10

As Brennan said in a comment above, something might have changed as variable expansion works in the rules:variables block now.

Here's what's working for me:

variables:
  DEPLOY_KEY: "$DEPLOY_KEY_DEV" # global fallback

deploy:
  stage: deploy
  script:
    - bash ./deploy.sh
  rules:
    - if: $DEPLOY_ENV == "production" # can use trigger variables for evaluation
      variables:
        DEPLOY_KEY: "$DEPLOY_KEY_PROD" # can expand project variables into other variables
    - if: $DEPLOY_ENV == "staging"
      variables:
        DEPLOY_KEY: "$DEPLOY_KEY_STAGING"
3
  • What GitLab release & tier are you guys using? I'm attempting to do something very similar to the OP, out of sheer coincidence, and I'm not seeing variable expansion in rules:variables. I have a 'variables' block in one of my rules, with a PARAMETER_A variable whose value I set as $SOME_ENV_PARAMETER_A, and rather than getting the expected value of the $SOME_ENV_PARAMETER_A variable when the pipeline is assembled, I instead get the string literal '$SOME_ENV_PARAMETER_A' value, which needless to say foils my plans. I'm using GitLab 15.11.3-ce. Thank you for any help! Commented Jun 10, 2023 at 21:29
  • I'm using 16.1.0 and Enterprise, but I had this implemented already on 15.x.x. It's hard to say what the issue is without seeing code, but this section on GitLab shows that with Bash (and similar) shells, you access with double-quotes; in PowerShell, you shouldn't use any quotes. If that's not the case, this section mentions how you can disable variable expansion for specific variables - maybe that's the issue? Good luck! Commented Jun 12, 2023 at 11:00
  • Maybe the confusion here is that you cannot use variables declared in the job itself? Commented Jul 18, 2023 at 2:50
2

There is no variable expansion made in rules:variables: section. You can only set a simple text value for a variable.

For an app_url, you may consider using the environment:url: section, which is where variable expansion works. But you can't use it inside of rules:.

Or have some conditional logic in the script: section.

1
  • 6
    Not sure if something has changed since this answer, but this is no longer true. I have tested this and you can definitely use another variable inside a rules:variable var
    – Brennan
    Commented Feb 14, 2023 at 23:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.