如何在 Azure DevOps 发布期间转换/修改 js 文件 [英] How to transform/modify a js file during a release in Azure DevOps

查看:29
本文介绍了如何在 Azure DevOps 发布期间转换/修改 js 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景/当前状态:我有一个基于 Azure DevOps 的 Angular 5 项目.我从同一个分支(主)推送到 Dev/Test 和 Prod.我为我的构建设置了 environment.ts 文件,所以当我从 master 构建我的项目时,它会根据我正在构建的环境选择 BaseAPIURL.我的 master 分支总是使用命令 using run-script build --环境=开发.

我想要实现的目标:我希望针对不同的环境有一个构建和多个发布"计划.默认情况下,在构建时,所有 angular 代码都会转换为我的dist"文件夹中的 .js 文件.环境变量存在于 main.bundle.js 中.如何在发布期间转换/修改该文件?

解决方案

我同意 Sergey 的看法,不推荐这样做,但我过去在一个 angular 项目中这样做过.

很简单,在您的 中安装

现在在每个发布环境(阶段)中添加替换令牌任务并将其配置为搜索模式__{variable}__:

对所有变量都这样做(只需将它们添加到变量选项卡中并将占位符放入文件中).

如果你想使用 APP_INITIALIZER 你可以阅读 这篇 博文解释了如何使用 Azure DevOps 来实现.

Scenario/Current state: I have an Angular 5 project which is on Azure DevOps. I push to Dev/Test and Prod from same branch (master). I have environment.ts file set up for my build so when I build my project from master it picks up the BaseAPIURL's depending on which environment I am building in. My master branch always using the command using run-script build --env=dev.

What I am trying to achieve: I want to have one Build and multiple "Release" plans for different environment. By default upon build all the angular code is converted to .js files in my "dist" folder. The environment variable is present in main.bundle.js. How can I transform/modify that file during a release ?

解决方案

I agree with Sergey, is not recommended to do it, but I did it in the past in an angular project.

It's simple, install Replace Tokens extension, in your environment.ts file put a placeholders for the values, for example:

baseUrl = __baseUrl__

In the release pipeline add a variable baseUrl for each environment with different value.

Now in each release environment (stage) add the replace tokens task and configure it to search the pattern __{variable}__:

So do it for all the variables (just add them in the variables tab and put the placeholders in the file).

If you want to use APP_INITIALIZER you can read this post that explain hot to do it with Azure DevOps.

这篇关于如何在 Azure DevOps 发布期间转换/修改 js 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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