简体   繁体   English

Azure DevOps - React App - 在发布管道中设置环境变量

[英]Azure DevOps - React App - Set Environment Variables in Release Pipeline

We have a React app in AzureDevOps.我们在 AzureDevOps 中有一个 React 应用程序。 We build it using npm install/npm run build and then upload the zip file.我们使用npm install/npm run build 构建它,然后上传 zip 文件。 From there we'll do a release to multiple stages/environments.从那里我们将发布到多个阶段/环境。 Due to SOX compliance we're trying to maintain a single build/artifact no matter what the environment.由于 SOX 合规性,无论环境如何,我们都试图维护一个单一的构建/工件。

What I'm trying to do is be able to set the environment variables during the release pipeline .我想要做的是能够在发布管道期间设置环境变量。 For instance, be able to substitute the value of something like process.env.REACT_APP_CONFIG_VALUE例如,能够替换process.env.REACT_APP_CONFIG_VALUE之类的值

I've tried setting that in the Pipeline variables during the release but it does not seem to work.我试过在发布期间在管道变量中设置它,但它似乎不起作用。 Is this possible or do I have to use a json config of some sort instead of using process.env?这是可能的还是我必须使用某种 json 配置而不是使用 process.env?

Thanks谢谢

You could not achieve this by setting pipeline variables during the release.您无法通过在发布期间设置管道变量来实现此目的。

Suggest you could use RegEx Match & Replace extension task to achieve this.建议您可以使用RegEx Match & Replace扩展任务来实现这一点。 Use this site to convert the regular expression: Regex Generator使用此站点转换正则表达式: Regex Generator

Here is an example:这是一个例子:

this._baseUrl = process.env.REACT_APP_CONFIG_VALUE;

This extension task will use regular expressions to match fields in the file.此扩展任务将使用正则表达式来匹配文件中的字段。

在此处输入图像描述

在此处输入图像描述

Checking from published js.从已发布的 js 检查。

在此处输入图像描述

This is how I did it -我就是这样做的-

Step1: Add all those files (.env.env.development.env.production) to azure devops library as secure files.第一步:将所有这些文件(.env.env.development.env.production)作为安全文件添加到 azure devops 库中。 We can download these secure files in the build machine using a DownloadSecureFile@1 pipeline task (yml).我们可以使用 DownloadSecureFile@1 管道任务 (yml) 在构建机器中下载这些安全文件。 This way we are making sure the correct.env file is provided in the build machine before the task yarn build --mode development in the pipeline.通过这种方式,我们确保在管道中的任务 yarn build --mode development 之前在构建机器中提供了正确的 env 文件。

在此处输入图像描述

Step2: Add the following task in your azure yml pipeline in appropriate place.第 2 步在您的 azure yml 管道中适当的位置添加以下任务。 I have created a github repohttps://github.com/mail4hafij/react-yarn-azure-pipeline if you want to see a complete example.如果您想查看完整示例,我已经创建了一个 github 存储库https://github.com/mail4hafij/react-yarn-azure-pipeline

  # Download secure file from azure library
  - task: DownloadSecureFile@1
    inputs:
      secureFile: '.env.development'

  # Copy the .env file
  - task: CopyFiles@2
    inputs:
      sourceFolder: '$(Agent.TempDirectory)'
      contents: '**/*.env.development'
      targetFolder: '$(YOUR_DEFINED_PROJECT_ROOT_FOLDER_VARIABLE)'
      cleanTargetFolder: false

Keep note, secure files can't be edited but you can always re-upload.请注意,无法编辑安全文件,但您可以随时重新上传。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM