title: How to define global variables based on rules on gitlabci?
body: <p>How to define global variables based on rules on gitlabci?</p>
  <p>I have a syntax error with this script and I wonder how to fix it. My goal is to use different environment variables based on my branch.</p>
  <p>Is it possible to define once a list of environment variables for the two stages (build and deploy)?</p>
  <p>If not how can I reduce and simplify my script to avoid redundancy? I tested anchors but the possibility to define global environment variables for all my script based on my branch would be really awesome!</p>
  <pre><code>rules:
    - if: $CI_COMMIT_BRANCH == /develop/
      variables:
        BUILD_ENV: &quot;build:staging&quot;
        CLUSTER_NAME: &quot;STAGING&quot;
        SERVICE_NAME: &quot;WebappSTAGING&quot;
        TASK_DEFINITION_NAME: &quot;FlaskSTAGING&quot;
        S3_BUCKET_URL: &quot;&quot;
    - if: $CI_COMMIT_BRANCH == /master/
      variables:
        BUILD_ENV: &quot;build&quot;
        CLUSTER_NAME: &quot;&quot;
        SERVICE_NAME: &quot;&quot;
        TASK_DEFINITION_NAME: &quot;&quot;
        S3_BUCKET_URL: &quot;&quot;
  
  
  image: docker:19.03.10
  #image: registry.gitlab.com/gitlab-org/cloud-deploy/aws-ecs:latest
  services:
    - docker:dind
  
  before_script:
    - IMAGE_TAG=$CI_COMMIT_BRANCH
    - apk add --no-cache curl jq python py-pip
    - pip install awscli
    - pip install ecs-deploy
    - aws ecr get-login-password --region eu-west-3 | docker login --username AWS --password-stdin $REPOSITORY_URL
  
  stages:
    - build
    - deploy
  
  build_container:
    stage: build
    script:
      - echo &quot;Building Flask image...&quot;
      - docker build -t $REPOSITORY_URL/flask:$IMAGE_TAG ./Flask_server
      - echo &quot;Tagging Flask image...&quot;
      - docker tag $REPOSITORY_URL/flask:$IMAGE_TAG $REPOSITORY_URL/flask:$IMAGE_TAG
      - echo &quot;Pushing Flask image...&quot;
      - docker push $REPOSITORY_URL/flask:$IMAGE_TAG
    only:
      - develop
      - master
  
  build_staticfiles:
    stage: build
    image: node:14
    before_script:
      - nodejs -v
      - npm -v
    script:
      - cd client
      - npm install
      - npm run $BUILD_ENV
    artifacts:
      paths:
      - client/dist/
      expire_in: 30 minutes
    only:
      - develop
      - master
  
  
  deploy_container:
    stage: deploy
    script:
      - echo $REPOSITORY_URL/flask:$IMAGE_TAG
      - ecs deploy $CLUSTER_NAME $SERVICE_NAME --timeout -1
    only:
      - develop
      - master
  
  deploy_static_files:
    stage: deploy
    script:
      - aws s3 cp client/dist/ S3_BUCKET_URL --recursive
    only:
      - develop
      - master
  </code></pre>