如何在不同的构建环境中使用Contentful Middleman预览API?

时间:2017-07-05 13:50:27

标签: ruby middleman contentful

以下是我原则上要实现的目标,由于需要在构建之前检索内容数据,因此显然无法正常工作:

# Build command
bundle exec middleman contentful && bundle exec middleman build -e <ENV>
# config.rb

configure :prod do
  set :build_dir, 'build/prod'
  set :ENV, 'prod'

  activate :contentful do |f|
    f.space         = { hs: 'SPACE' }
    f.access_token  = 'TOKEN'
    f.cda_query     = { include: 2 }
    f.all_entries   = true
    f.content_types = { #... }
  end
end

configure :testSite do
  set :build_dir, 'build/test-site'
  set :ENV, 'test'

  activate :contentful do |f|
    use_preview_api = true
    #...
  end
end

1 个答案:

答案 0 :(得分:3)

您可以使用环境变量来启用它。

# Build command
CONTENTFUL_ENV=<env> bundle exec middleman contentful && bundle exec middleman build -e <env>


# config.rb
access_token = nil
use_preview = false
case ENV['CONTENTFUL_ENV']
when 'prod'
  access_token = ENV['CONTENTFUL_PROD_TOKEN']
when 'testSite'
  access_token = ENV['CONTENTFUL_TEST_TOKEN']
  use_preview = true
end

activate :contentful do |f|
  f.space = {space_alias: ENV['CONTENTFUL_SPACE']}
  f.use_preview_api = use_preview
  f.access_token = access_token
  # ...
end

通过这种方式,您可以将多个环境协同工作