Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set variables depending on Gitlab rules

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 ?

like image 550
Med Avatar asked Mar 11 '26 17:03

Med


1 Answers

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"
like image 104
James Hooper Avatar answered Mar 14 '26 18:03

James Hooper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!