使用 Rake 进行环境建设的首选方法? (来自 Msbuild/Nant)
我编写并维护了很多 Nant/Msbuild 文件,并且在当前项目中遇到了一些问题,似乎 rake 会让我的生活变得更轻松。然而,我在第一个障碍上有点难住,但这可能是因为我是通过南特的眼睛看问题的。
这里给出一些背景信息,这是我通常期望构建的布局方式(忘记实际的物理结构,只需看看它的组件是什么):
- Root
| - build-scripts
| - default.build
| - *.build
| - build-properties
| - dev.properties
| - live.properties
| - *.properties
| - tools
| - Nant
| - Nant.exe
| - *.*
| - Nunit
| - Nunit.exe
| - *.*
| - **/*.*
现在,正如您在上面看到的,构建的主要组件是构建脚本,其中包含实际的构建指令、构建属性,这些文件纯粹包含每个环境的属性。即
dev.properties 可能包含 web.service.url = "http://some.dev.address"
live.properties 可能包含 web.service.url = "http://some.live.address"
然后是构建脚本使用的外部可执行文件的工具,例如 Nant、Nunit、 JsTestDriver 等。
现在关注我的default.build 文件,它往往包含许多前期属性,例如使用的目录(即库、输出、包、项目、测试),因此它们都可以预先评估。然后其他 *.build 文件包含相关的构建脚本,例如tests.build将处理Nunit并运行单元/集成/验收测试等。
对我来说,这里的主要问题是我有这些环境属性,它们为每个配置配置构建环境,然后是许多预定义的属性,当从命令行调用时,这些属性会被覆盖,即,如果您正在构建实时环境,则您将传递environment=live,或者如果将其留空,它将默认环境为dev ,并且在 CI 服务器上它会将环境设置为 ci。
我能找到的所有示例似乎都没有告诉我如何使用 Rake 的命令行覆盖默认属性/变量,我知道您可以设置属性并通过 Env[] 机制获取它们,但是要使用这个我需要像平常一样设置一个全局变量,然后有一个步骤检查传入的任何环境变量并用这些属性覆盖全局变量,也是我看到允许外部配置文件的唯一方法是在其中使用全局变量并包含它,我不知道介意做,但我希望在这个领域有一些我可以学习的最佳实践。
另外,我想到的最后一件事是,实际上唯一要传入的变量是环境(dev、ci、live 等),因此我可以使其默认构建任务需要传入一个参数,即支持,但不确定这是否是最好的,因为如果没有设置任何内容,我希望它作为“dev”运行,这意味着您总是需要设置一个(不是世界末日)。
正如您所看到的,在这个领域存在很多问题,采用我现有的方法并尝试使其与 Rake 一起使用。任何建议或信息都会很棒!
I have written and maintained a lot of Nant/Msbuild files, and have got a few problems in a current project where it seems that rake would make my life a lot easier. However I am a bit stumped on the first hurdle, but this may be because I am looking at the problem through Nant eyes.
To give some context here is how I normally expect my builds to be laid out (forgetting the actual physical structure, just look at what the components of it are):
- Root
| - build-scripts
| - default.build
| - *.build
| - build-properties
| - dev.properties
| - live.properties
| - *.properties
| - tools
| - Nant
| - Nant.exe
| - *.*
| - Nunit
| - Nunit.exe
| - *.*
| - **/*.*
Now as you can see above the main components of a build are the build scripts, which contain the actual build instructions, the build properties, which are files containing purely the properties per environment. i.e
dev.properties may contain web.service.url = "http://some.dev.address"
live.properties may contain web.service.url = "http://some.live.address"
Then there are the tools which are external executables used by the build scripts, such as Nant, Nunit, JsTestDriver etc.
Now focusing on my default.build file, this tends to contain a lot of upfront properties, such as directories used (i.e libs, output, package, project, tests) so they can all be evaluated up front. Then the other *.build files contain relevant build scripts, such as tests.build would deal with Nunit and run Unit/Integration/Acceptance tests etc.
The main problem for me here is that I have these environmental properties which configure the build for each environment, and then a lot of pre-defined properties which are overridden when invoked from the command line, i.e you would pass environment=live if you were building for a live environment, or if you left it blank it would default the environment to dev, and on the CI server it would set the environment to ci.
None of the examples I can find seem to tell me how to have default properties/variables that are overridden by the command line with Rake, I know you can set properties and get them out via the Env[] mechanism, but to use this I would need to set a global variable as normal then have a step which checks any env vars passed in and overwrite the global variables with those properties, also the only way I can see to allow external configuration files is to use global variables within them and include it, which I don't mind doing but I was hoping there was some best practises in this area I could learn from.
Also the final thing I was thinking about was that really the only variable to be passed in would be the environment (dev,ci,live etc) so I could make it so the default build task required an argument to be passed in, which is supported, but not sure if this is best as I would want it to run as "dev" if nothing was set, and this means you would always need to set one (not end of the world).
As you can see there are quite a few questions in this area, taking my existing approach and trying to adapt it to work with Rake. Any advice or information would be great!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是正确的答案,但我基本上保持了与上面相同的结构,并为所有内容使用了全局变量。所以我有一个属性文件,其中包含以下内容:
它并不是非常理想,但它允许我根据环境注入不同的属性,它还使许多构建文件的可重用性降低,因为它们都使用这些全局变量。使用基于参数的任务可能会使其更具可重用性,但该领域没有大量文档和工作示例,因此我将坚持使用以下系统,直到发现主要问题。
This isn't the proper answer, but I have basically kept the same soft of structure as above and used global variables for everything. So I have a properties file which contains something like:
It is not really ideal, but it allows me to inject different properties based on the environment, it also makes alot of the build files less re-usable as they all use these global variables. Using parameter based tasks may make it more re-usable but there isn't a huge deal of documentation in this area and working examples, so I am just going to stick with the following system until I find a major problem.