Context Parameter Inheritance
Hi,
Is there a way I can read the root project's Context Parameters, from a sub project? Or to let the sub project inherit the root project's Context Parameters?
I've tried using DslContext.getParameter(name = "myParameter") to read a parameter defined at root level, but all it returns is <placeholder-1>.
My use case is as follows:
- I would like to disable some (or even all) scheduled triggers on our test environment, but not on production.
- I'm thinking of using Context Parameters for this, and I would like to define the parameter at the root level (so it will be either tst or prd). I would then use something like enabled = DslContext.getParameter(name = "environment") == "prd" within a trigger in a sub project to make sure that that trigger is only enabled in production.
- I want to define the parameter at root level because the root project is distinct for our production and test environment. Most of the subprojects, however, are the same on production and test, so I can't make the distinction there.
If there's a better way to disable scheduled triggers than using Context Parameters, please let me know.
Thanks!
Guno
Please sign in to leave a comment.
Hi Guno,
Unfortunately, Context Parameters (defined using DslContext.getParameter) are limited in scope to the current DSL execution and do not automatically propagate from a root project to subprojects. This is why your subproject only sees <placeholder-1> for the parameter.
To address this, I recommend using build parameters instead. Build parameters defined at the root level are automatically inherited by subprojects unless explicitly overridden.
At the root project:
In the subproject:
The env parameter is automatically available in all subprojects, which simplifies the configuration process.
Best Regards,
Tom
Hi Tom,
Thanks for your response.
I considered using regular build parameters, but I decided against it, as I thought that there was no way that that could work (also saw in my test that it didn't work); the evaluation of the expression "%env%" == "prd" into a boolean would happen way before TeamCity would have any chance to substitute the env param.
Would you agree? And if so, what would be a better approach?
Thanks.
You are right. In TeamCity DSL, build parameters such as %env% are resolved at runtime, not during the DSL script's evaluation.
In this case, if you prefer a more explicit solution, you could create two separate templates: one for triggered builds and another for non-triggered builds. Then, depending on the environment (e.g., production or test), you can decide which template to use for each build configuration.
Best Regards,
Tom
Hi Tom,
I'm not sure what that should look like, exactly.
Do you mean I should use a build param (like %env%, in your example) in the template() calls in my sub projects in some way, to conditionally apply either the one or the other template?
Apologies for any confusion. What I mean is that, if possible, you may need to use different templates for the production and test environments. For production, use a template with triggers enabled, and for testing, use a template with triggers disabled.
Best Regards,
Tom
OK, understood.
Should I raise a Youtrack issue to support propagation/inheritance for context parameters?
That would solve my problem is a much more elegant way.
Hi Guno,
Feel free to share any feature requests/bugs at Youtrack(https://youtrack.jetbrains.com/issues/TW).
Best Regards,
Tom
Raised https://youtrack.jetbrains.com/issue/TW-91739/Access-Context-Parameters-of-root-project.