有没有办法应用类似于VSTS File Transform的东西在 VSTS 构建过程中连接到 Azure Function?
我有一个 Azure Function App,每个部署环境(开发、测试、生产)都有一个函数。除了需要根据每个特定部署更新以下值之外,这些功能几乎相同:
- 函数名称
- 集成服务总线队列名称
- 用于日志记录的 TraceWriter 前缀
这些值的代码大纲,请注意“Dev”前缀:
public static class DevFunction {
[FunctionName("DevFunction")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(...)] HttpRequestMessage request,
[ServiceBus("devQueueName", ...] ICollector<string> outputBus,
TraceWriter log)
{
log.Info("Starting DevFunction");
... // Do work
}
}
我想将这些函数合并为一个函数,并使用类似于 VSTS 文件转换的功能在给定部署的构建过程中更新上面列出的信息。我目前有三个独立的 VS 项目,每个项目都有自己的功能,可以部署到每个单独的环境中。这里有更好的选择吗?
最佳答案
目前,Azure Functions 不支持标准配置/设置转换。但是,有多种解决方法可以根据部署环境转换功能:
0。 future 的解决方案
理想情况下,Azure 将添加对函数的标准 web.config 等效项的支持。从这里GitHub thread :
[Jun 19, 2017] ...there is no good equivalent of a web.config file today for functions. We plan on addressing this as part of our porting work to .NET core.
1。预处理器指令
对于我的场景,使用 preprocessor directives最终成为最简单的解决方案。每个构建配置对应一个部署。例如:
#if RELEASE
[FunctionName("ReleaseFunction")]
#elif AZURETEST
[FunctionName("TestFunction")]
#else
[FunctionName("DevFunction")]
#endif
构建配置是在我的每个 VSTS 构建的“构建解决方案”步骤中指定的。显然,这段代码相当丑陋,并且可能难以维护,具体取决于所需转换的数量。
2。读取自定义设置文件
同样来自同一个GitHub thread ,可以在函数应用的根目录创建自定义设置文件,然后根据需要读取:
var settingsPath = Path.Combine(executionContext.FunctionAppDirectory, "settings.json");
dynamic settings = JsonConvert.DeserializeObject(File.ReadAllText(settingsPath));
var myConfigValue = (string)settings.Values.MyConfigValue;
请注意,这不适用于转换 FunctionName
并且 runtime bindings可能需要转换输入/输出绑定(bind)(例如原始问题中的 ServiceBus 绑定(bind))。
3。 PowerShell 文本替换
编写一个 PowerShell 脚本,对函数文件执行原始文本转换。添加 VSTS 构建步骤,在构建解决方案之前运行脚本。转换所需的任何值都可以在 Function App 的全局应用程序设置中定义。
4。 ARM 模板
再次来自GitHub thread ,也许可以使用 ARM 模板来完成此任务。
[Aug 10, 2017] My CI/CD setups use ARM templates to deploy the function apps with the appropriate app settings. These ARM templates are in source control and have CD hooked up so when I need to add a new app setting I just commit the changes to the ARM template.
关于Azure Function 在 VSTS 构建期间进行转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50934568/