如何根据创建的分支为具有不同环境变量的creat-react-app项目设置DevOps构建? [英] How to setup DevOps build for creat-react-app project with different environment vars depending on built branch?

查看:202
本文介绍了如何根据创建的分支为具有不同环境变量的creat-react-app项目设置DevOps构建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个create-react-app项目,该项目从环境中获取了一些构建参数(例如Auth0配置),这意味着这些环境变量会影响react构建.

We have a create-react-app project that gets some build parameters from the environment (e.g. Auth0 config), meaning that these environment variables affect the react build.

我们正在为该项目开发一个Azure DevOps构建管道,尽管在管道的变量页面上定义变量很容易,但是如何根据正在构建的分支来不同地设置变量并不明显.

We're working on an Azure DevOps build pipeline for this project, and while it's straighforward to define the variables on the pipeline's variables page, it's not obvious how to set them differently depending on which branch is being built.

例如,我们希望dev分支在Auth0中使用或开发租户构建,而release和master分支应在Auth0中使用或生产租户构建(相同变量,两个不同的值).

For example, we'd like the dev branch to build using or development tenant in Auth0, while release and master branches should build using or production tenant in Auth0 (same variable, two different values).

是否有记录/受支持的方法来解决此问题?还是一些最佳实践"?

Is there a documented/supported approach to this? Or some "best practice"?

我了解到您可以通过PowerShell脚本修改变量,例如就像在这篇文章中一样.那是最好的方法吗?脚本如何检查正在建立的分支,以选择正确的值集?

I've read that you can modify variables from PowerShell scripts, e.g. like in this post. Is that the best way to do it? How can the script inspect which branch is being built, to choose the right set of values?

我还发现DevOps具有变量组的概念.我还没有读过它,但是在每个分支(或分支模式)中定义一个变量组并让脚本选择要应用的组是一个好方法吗?怎么样?

I also found that DevOps has a concept of variable groups. I haven't read up on it yet, but would it be a good approach to define a variable group per branch (or branch pattern) and have the script select which group to apply? How?

推荐答案

如何根据创建的分支为具有不同环境变量的creat-react-app项目设置DevOps构建?

How to setup DevOps build for creat-react-app project with different environment vars depending on built branch?

此刻,恐怕在构建定义中使用Power-Shell脚本任务来获取源分支名称,然后根据分支名称设置环境变量是一个不错的选择.因为在构建管道中尚不支持嵌套变量.

At this moment, I am afraid using Power-Shell script task in your build definition to get the source branch name and then set environment variables based on the branch name is a good choose. Because nested variables are not yet supported in the build pipelines.

PowerShell脚本如下:

The PowerShell script like:

$branch = $Env:Build_SourceBranchName
Write-Host "Current branch is $branch"
if ($branch -eq "Dev")
{
  Write-Host ("##vso[task.setvariable variable=testvar]testvalue")
}
else (($branch -eq "master") -or (($branch -eq "release")))
{
  Write-Host ("##vso[task.setvariable variable=testvar2]testvalue2")
}

检查记录命令在构建过程中获取更多详细信息.

Check the Logging Command during the build for some more details.

此外,对于此线程了解更多信息.

Besides, for the variable groups, we could use VSTS Variable Groups per environment for release definition, but it not work for build definition. Check this thread for some details.

希望这会有所帮助.

这篇关于如何根据创建的分支为具有不同环境变量的creat-react-app项目设置DevOps构建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆