Skip to content

Split expanded_environment_name away from variable expansion

The following discussion from !202162 (merged) should be addressed:

  • @fabiopitino started a discussion:

    Idea for a follow-up:

    The problem with expanded_environment_name method is that it has more than 1 responsibility:

    1. It returns the name of the persisted environment.
    2. It expands the user-provided environment name using the job variables.

    I think that these 2 responsibilities should be separate. The latter should be a method on its own that can also be reused in https://gitlab.com/gitlab-org/gitlab/-/blob/26c2e6302994dfa99b8cb567704b94d29b416c9c/lib/gitlab/ci/build/context/build.rb#L42 as SSoT:

    module Ci::Deployable
      def expand_environment_name(environment)
        ExpandVariables.expand(environment, -> { simple_variables.sort_and_expand_all })
      end
    
      # At this stage the "expanded" prefix is irrelevant from a domain perspective
      def environment_name 
        return unless has_environment_keyword?
    
        if use_job_environment_for_expanded_environment_name? && job_environment&.expanded_environment_name.present?
          return job_environment.expanded_environment_name
        end
        
        metadata&.expanded_environment_name
      end
    end

    Then use explicitly expand_environment_name when we don't have job_environment persisted yet and use environment_name after a job is persisted.