Gitlab 아바타/첨부파일 상대 경로 설정

사내 사정으로 인해 Gitlab의 External_Url 을 실제 서버 주소와 다르게 설정해서 쓰고 있다.

물론 실제 nginx 에도 해당 도메인으로 접근할 수 있도록 설정해야 하는 게 맞다. 그렇지 않으면, 첨부파일과 아바타 이미지가 모두 깨져나온다. 하지만 그러기 전에 Gitlab 차원에서 상대 경로로 아바타를 출력할 수 있도록 수정하는 방법 ‘도’ 있으니 편법으로 수정했다. 아래는 그 편법 내용을 다룬다.

아바타 상대 경로 설정

/opt/gitlab/embedded/service/gitlab-rails/app/models/concerns/avatarable.rb 파일에서 절대 경로로 수정하는 코드가 있는데, url_base 에 덧붙이는 코드들을 전부 주석처리하면 된다.

module Avatarable
  extend ActiveSupport::Concern

  def avatar_path(only_path: true)
    return unless self[:avatar].present?

    asset_host = ActionController::Base.asset_host
    use_asset_host = asset_host.present?

    # Avatars for private and internal groups and projects require authentication to be viewed,
    # which means they can only be served by Rails, on the regular GitLab host.
    # If an asset host is configured, we need to return the fully qualified URL
    # instead of only the avatar path, so that Rails doesn't prefix it with the asset host.
    if use_asset_host && respond_to?(:public?) && !public?
      use_asset_host = false
      only_path = false
    end

    url_base = ""
    # if use_asset_host
    #   url_base << asset_host unless only_path
    # else
    #   url_base << gitlab_config.base_url unless only_path
    #   url_base << gitlab_config.relative_url_root
    # end

    url_base + avatar.url
  end
end

당연히 gitlab-ctl reconfigure && gitlab-ctl restart 를 해 줘야 한다.

참고 : https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/11001/diffs#note_28659864

첨부파일 상대 경로 설정

단서가 없어서 맨땅에 헤딩하듯이 뒤지고 다녔다. Markdown 언어에서 링크를 담당하는 라이브러리가 있는데, 이름이 ‘banzai’ 이다 (…) 무슨 생각으로 이름을 지은건지 도저히 모르겠지만 여기를 수정해야 한다.

filter/upload_link_filter.rb 를 보면, build_url() 이란 함수가 있다. 여기서 Geo node 여부에 따라 base_path 를 설정하는 코드가 있는데, 여기서 원하는 내부 주소로 변경하면 된다.

def build_url(uri)
        base_path = if Gitlab::Geo.secondary?
                      Gitlab::Geo.primary_node.url
                    else
                      # Gitlab.config.gitlab.url
                      "http://your.address.com/"
                    end

        if group
          urls = Gitlab::Routing.url_helpers
          # we need to get last 2 parts of the uri which are secret and filename
          uri_parts = uri.split(File::SEPARATOR)
          file_path = urls.show_group_uploads_path(group, uri_parts[-2], uri_parts[-1])
          File.join(base_path, file_path)
        else
          File.join(base_path, project.full_path, uri)
        end
      end

Gitlab 11 버전 으로 업그레이드 되면서부터 해당 파일은 삭제되고, absolute_link_filter.rbexternal_link_filter.rb , relative_link_filter.rb 가 남아있다. 여기 있는 Gitlab.config.gitlab.url 을 원하는 주소로 바꿔주면 가능하다. (relative.rb 의 경우 Gitlab.config.gitlab.base_url)


Hugo 기반 / JimmyStack 테마를 사용 중입니다.