51

GitLab 实现动态 Environment URL

 5 years ago
source link: https://kemchenj.github.io/2018-12-27/?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

之前 fir 上传接口返回的数据里增加了一个 release_id,让我们可以直接跳转到对应 Build 的下载页面,但 GitLab 并不支持 动态 Environment URL

为此 GitLab 提供了一个环境变量 CI_ENVIRONMENT_SLUG ,可以作为合法的子域名使用,但这种方式并不利于我们接入第三方服务,例如 iOS 常用的测试分发平台 fir。

解决方法也很简单,使用一个中转服务器去存储 CI_ENVIRONMENT_SLUG 与下载地址的映射,然后通过 CI_ENVIRONMENT_SLUG 访问时查询对应的下载链接,重定向过去即可。

我顺手用 Sinatra + SQLite 实现了一套,首先是定义资源 download_url ,添加 put方法新增下载链接:

put '/download_url' do
  request.body.rewind
  json = JSON.parse request.body.read

  slug = json['ci_environment_slug']
  release_id = json['fir_release_id']
  download_url = json['fir_download_url']

  pass unless !slug.nil? || !slug.empty?
  pass unless !release_id.nil? || !release_id.empty?
  pass unless !download_url.nil? || !download_url.empty?
  
  guru_club_releases.insert(ci_environment_slug: slug, 
                            fir_release_id: release_id,
                            fir_download_url: download_url)

  'Success'
end

接着是 get,通过 ci_environment_slug 获取资源:

get '/download_url' do
  slug = params['ci_environment_slug']

  row = guru_club_releases.where(ci_environment_slug: slug).first

  if row
    download_url = row[:fir_download_url]
    release_id = row[:fir_release_id]
    redirect "#{download_url}?release_id=#{release_id}"
  else
    halt 404, 'Could not find corespond release'
  end
end

这里有完整的 Demo


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK