Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for dynamic build_url (and maybe other @config settings) #1542

Closed
tammersaleh opened this issue Mar 9, 2021 · 2 comments
Closed

Allow for dynamic build_url (and maybe other @config settings) #1542

tammersaleh opened this issue Mar 9, 2021 · 2 comments

Comments

@tammersaleh
Copy link

tammersaleh commented Mar 9, 2021

Problem

We need to ensure some links are fully qualified URLs with scheme, host, etc.

This works when deploying to a small number of known locations by hardcoding build_url in each environment inside nanoc.yaml. However, we deploy PRs to dynamic environments that have generated hostnames via Netlify's build preview feature.

Suggestions

Some solutions that come to mind:

  • Allow some targeted configuration values to be defined as environment variables, such as BUILD_URL
  • Parse nanoc.yaml as ERB before consuming, so we can build_url: <%= ENV["BUILD_URL"] %>
  • Reconsider implementing the original request of adding a --config flag to the nanoc CLI --config option on commandline #676

If this is already possible somehow, please let me know.

Attempted solution

I tried a hack of including this snippet at the top of Rules:

@config[:base_url] = ENV["BUILD_URL"]

This works a charm for nanoc compile, but fails with frozen errors on nanoc check:

Captain! We’ve been hit!

FrozenError: can't modify frozen Hash
You attempted to modify immutable data. Some data cannot be modified once compilation has started. Such data includes content and attributes of items and layouts, and fil
nts.

  0. /usr/local/bundle/gems/nanoc-core-4.11.14/lib/nanoc/core/configuration.rb:119:in `[]='
  1. /app/Rules:3:in `parse'
  2. /usr/local/bundle/gems/nanoc-4.11.14/lib/nanoc/rule_dsl/rules_loader.rb:26:in `instance_eval'
  3. /usr/local/bundle/gems/nanoc-4.11.14/lib/nanoc/rule_dsl/rules_loader.rb:26:in `parse'
  4. /usr/local/bundle/gems/nanoc-4.11.14/lib/nanoc/rule_dsl/rules_loader.rb:15:in `load'
  5. /usr/local/bundle/gems/nanoc-4.11.14/lib/nanoc/rule_dsl/action_provider.rb:20:in `for'
  6. /usr/local/bundle/gems/nanoc-core-4.11.14/lib/nanoc/core/compiler_loader.rb:18:in `load'
  7. /usr/local/bundle/gems/nanoc-core-4.11.14/lib/nanoc/core/compiler.rb:31:in `new_for'
  8. /usr/local/bundle/gems/nanoc-4.11.14/lib/nanoc/checking/check.rb:35:in `create'
  9. /usr/local/bundle/gems/nanoc-4.11.14/lib/nanoc/checking/runner.rb:101:in `block in run_checks'
  ... 22 lines omitted (see crash.log for details)

A detailed crash log has been written to ./crash.log.
@denisdefreyne
Copy link
Member

Hey Tammer,

I believe that you’d be able to achieve this by sticking the @config[:base_url] = ENV["BUILD_URL"] line into the preprocess block. For example:

preprocess do
  @config[:base_url] = ENV["BUILD_URL"]
end

… and then you can use @config[:base_url] as usual.

Would this work for your use case?

@tammersaleh
Copy link
Author

Thanks, @ddfreyne, that sure seems to have done the trick! I guess check calls @config.freeze after the preprocessing has happened? Do you think this is something we should add to the docs in some way?

@tammersaleh tammersaleh changed the title Allow for dynamic build_url (and maybe other env settings) Allow for dynamic build_url (and maybe other @config settings) Mar 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants