简体   繁体   English

在 Azure DevOps Build Pipeline 中恢复 Libman JS 库

[英]Restore Libman JS Libraries in Azure DevOps Build Pipeline

I've set up a happy Azure DevOps build pipeline, which is triggered on a commit to a branch, then restores Nuget packages, builds my Mvc.Core web app and deploys it to an Azure App Service.我已经设置了一个愉快的 Azure DevOps 构建管道,它在提交到分支时触发,然后恢复 Nuget 包,构建我的 Mvc.Core Web 应用程序并将其部署到 Azure 应用程序服务。

However the JS libraries referenced in LibMan are not restored and deployed to Azure.但是,LibMan 中引用的 JS 库不会还原并部署到 Azure。 How do I get this to happen?我怎样才能做到这一点?

I've included the LibMan build package (which I read would carry out the task automatically ), but with no joy.我已经包含了 LibMan 构建包(我读到它会自动执行任务),但没有任何乐趣。

<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.0.96" />    

Do I really need to add a .net core custom command to the pipeline, to install the LibraryManager Cli tool , and then call it to restore the JS libraries ?我真的需要在管道中添加一个 .net core 自定义命令来安装 LibraryManager Cli 工具,然后调用它来恢复 JS 库吗? It seems more complex than I'd expect, and I've not found any examples of anyone doing this online.这似乎比我预期的要复杂,而且我还没有在网上找到任何人这样做的例子。

Is the other approach to include the JS libraries in source control?在源代码管理中包含 JS 库的另一种方法是什么? I did want to avoid this if possible, but if it is the standard approach, then I'm happy to do that I guess.如果可能的话,我确实想避免这种情况,但如果这是标准方法,那么我想我很乐意这样做。

Any advice, or YAML examples of ways to achieve this in Azure DevOps would be very much appreciated.非常感谢任何有关在 Azure DevOps 中实现此目的的建议或 YAML 示例。

The problem with restoring LibMan JS libraries in our case turned out to be a project file setting added by another developer months ago, when they were having problems with slow build times, as mentioned here .在我们的案例中,恢复 LibMan JS 库的问题原来是另一个开发人员几个月前添加的项目文件设置,当时他们遇到了构建时间缓慢的问题,如这里所述

 <PropertyGroup>
    <LibraryRestore>false</LibraryRestore>
  </PropertyGroup>

Once I found, and removed this setting, unsurprisingly the LibMan restore worked perfectly, and without a particularly slow build time either.一旦我发现并删除了这个设置,不出所料,LibMan 恢复工作得很好,而且构建时间也没有特别慢。

I guess the ideal situation would be not to attempt to do the LibMan restore on every build, but manually on developer machines, and then as a specific step in a DevOps build pipeline?我想理想的情况不是尝试在每次构建时都进行 LibMan 恢复,而是在开发人员机器上手动进行,然后作为 DevOps 构建管道中的特定步骤? But anyway, it's all working now.但无论如何,现在一切都在起作用。

Restore Libman JS Libraries in Azure DevOps Build Pipeline在 Azure DevOps Build Pipeline 中恢复 Libman JS 库

You do not need to add a .net core custom command to the pipeline to restore the JS libraries.您无需向管道添加.net core自定义命令即可恢复 JS 库。

I have created a sample to test LibMan build package, and it works fine on Azure devops.我创建了一个示例来测试LibMan构建包,它在 Azure devops 上运行良好。

To use the LibMan build package, first, you need set the correct the libman.json with JS Libraries in it in your project, like:要使用LibMan构建包,首先,您需要在项目中使用 JS 库在其中设置正确的libman.json ,例如:

{
  "version": "1.0",
  "defaultProvider": "cdnjs",
  "libraries": [
    {
      "provider": "cdnjs",
      "library": "jquery@3.2.1",
      "destination": "wwwroot/lib/jquery",
      "files": [
        "jquery.min.js",
        "jquery.js",
        "jquery.min.map"
      ]


    }
  ]
}

That because LibMan package will restore the JS Libraries based on the libman.json .那是因为LibMan包将基于libman.json恢复 JS 库。 You will find following target in the microsoft.web.librarymanager.build.targets in the LibMan build package:您将在LibMan构建包的microsoft.web.librarymanager.build.targets中找到以下目标:

<Target Name="LibraryManagerRestore" Condition="'$(LibraryRestore)' != 'False'">

    <Microsoft.Web.LibraryManager.Build.RestoreTask
        FileName="libman.json"
        ProjectDirectory="$(MSBuildProjectDirectory)"
        ProviderAssemblies="$(LibraryProviderAssemblies)">

        <Output TaskParameter="FilesWritten" ItemName="_FilesWritten"/>
    </Microsoft.Web.LibraryManager.Build.RestoreTask>

    <ItemGroup>
        <FilesForPackagingFromProject  Include="%(_FilesWritten.Identity)">
            <DestinationRelativePath>%(_FilesWritten.Identity)</DestinationRelativePath>
        </FilesForPackagingFromProject>
    </ItemGroup>
</Target>

This .targets will only be parsed by MSBuild during building,这个 .targets 只会在构建过程中被 MSBuild 解析,

So , second, those JS Libraries will be restored when you build your project instead of restoring the packages.因此,第二,当您构建项目而不是恢复包时,将恢复那些 JS 库。

As test, I remove the JS libraries from my repo:作为测试,我从我的仓库中删除了JS libraries

在此处输入图片说明

Then after dotnet build completed, I could get the JS libraries in the folder wwwroot/lib/jquery :然后在 dotnet build 完成后,我可以在文件夹wwwroot/lib/jquery获取JS libraries

在此处输入图片说明

If it still not resolve your issue, please make sure you can restore the JS Libraries when you build from Visual Studio without Azure Devops.如果仍然无法解决您的问题,请确保您可以在不使用 Azure Devops 的情况下从 Visual Studio 构建时恢复 JS 库。

BTW, you can also use the task Libman (Preview) instead of MSBuild to restore packages defines in libman.json .顺便说一句,您还可以使用任务 Libman (Preview) 而不是 MSBuild 来恢复libman.json包定义。

Hope this helps.希望这可以帮助。

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

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