【问题标题】:Azure DevOps feed get download url for a latest version of the nuget package or symply download latest packageAzure DevOps 源获取最新版本的 nuget 包的下载 url 或 symply 下载最新包
【发布时间】:2021-03-30 17:52:04
【问题描述】:

我有构建 nuget 包并将其部署到 Azure DevOps 源的 Azure DevOps 管道。在门户中,我可以找到特定版本包的下载链接。 如何在提要中找到 url 以下载最新版本的 nuget 包?

或者,我如何下载最新版本的软件包? 理想情况下,只需通过 curl、dotnet 或任何在 dev windows 机器上重新发送的东西,通常是 docker sdk 图像。 我倾向于走很长的路

  • dotnet 新控制台
  • 添加包
  • 恢复 找到文件的位置。但我真的不喜欢这种方法。有更好的吗?

【问题讨论】:

    标签: azure-devops feed artifact


    【解决方案1】:

    如何在提要中找到 url 以下载最新版本的 nuget 包?

    请按照以下步骤查找此网址。

    1. 使用此 Rest API:Feed Management - Get Feeds 获取提要 身份证。
    2. 使用此 API:Artifact Details - Get Packages 获取有关提要中目标包的详细信息。网址看起来像:https://feeds.dev.azure.com/{organization}/{project}/_apis/packaging/Feeds/{feedId}/packages?packageNameQuery={packageName}&api-version=6.0-preview.1。从响应中可以找到它的versions数组,最新版本用"isLatest":true标记,所以你可以在这个feed中得到这个包的最新版本。
    3. 这个 Rest API:NuGet - Download Package 提供了在 feed 中下载最新版本 nuget 包的 URL。

    顺便说一句,请注意,如果提要是在项目中创建的,则必须提供上述 API 中的 project 参数。如果提要未与任何项目关联,请在请求中省略项目参数。

    【讨论】:

    • 嗨,Alex,我的回答有效吗?请检查它,并请让我知道结果。谢谢。
    • 嗨,是的,它有效。抱歉耽搁了,因为我实际上以其他方式解决了我的问题。
    • 感谢您的回复。如果你能在这里分享你的解决方案会更好,这样其他人将来遇到同样的问题时可以有多种选择。
    • 我的问题是我想通过从提要下载 nuget 来安装 dotnet new -i nuget.nupkg。我找到了同样指定源提要的方法。这实际上帮助我解决了我的任务,结果我根本不需要从提要下载 nupkg。所以我的解决方案在这里没有帮助
    【解决方案2】:

    如果您在导入 NuGet 包之前构建最新版本并将项目设置为获取最新版本,则可以自动执行此操作。这些都不是真正的管道的一部分,除了您正在订购步骤。

    但是,我质疑 NuGet 一个项目,然后将其包含在管道中,特别是如果这是一个更大的解决方案中的一个项目(具有依赖项)。如果您打算部署到 NuGet,您应该更加有意识地将有问题的项目移动到另一个解决方案并将解决方案产品化,而不是将其留在主解决方案中。然后,您将为新产品创建一个单独的管道,并像使用任何其他 NuGet 包一样使用它。现在,您可能会想“但我在各种解决方案中都使用它”,所以这更容易,但实际上,这是一个更有说服力的理由,将其分离出来并有意放置在 NuGet 中,而不是自动获取最新的(即,如果您从另一家公司购买此程序集,并且治理要求您在自动部署解决方案之前测试新版本,请采取行动)。

    如果您这样做是因为有问题的项目仍在不断变化,那么我不会将消费者设置为自动获取最新信息并使其有意。即使您目前只有一个开发组,您也最好将去往 NuGet 的部分商品化。否则,真的没有必要构建 NuGet 包和使用(除非我错过了一些令人信服的理由不生产和继续这种每次编译和版本控制的复杂方式)。

    可能是 TL;DR?

    【讨论】:

      【解决方案3】:

      对于任何发现@edward-han-msft 的答案有用的人(就像我所做的那样),这里有一个 python 3.6+ 脚本,用于从 Azure DevOps 工件(原文如此)提要下载所有软件包的所有版本。在我的示例中,我正在迁移到另一个 NPM 提要,因此此脚本还将下载的包发布到您在 .npmrc 中配置的任何 npm 注册表。根据您的需要进行调整。

      import requests
      from requests.auth import HTTPBasicAuth
      import re
      import json
      import os
      from os.path import exists
      import subprocess
      
      organization = '<Your organisation>'
      project = '<Your project name>'
      feed_name = '<The name of the artifact feed>'
      feed_id = '<feedId - this can be found by examining the json response of the package feed>'
      
      # Packages feed url
      url_packages = f"https://feeds.dev.azure.com/{organization}/{project}/_apis/packaging/feeds/{feed_name}/packages?api-version=5.1-preview.1"
      
      # ADO PAT
      basic = HTTPBasicAuth("<I think this can be anything>", "<a DevOps PAT with Packaging Read scope>")
      
      # fetch the packages feed and save locally
      r1 = requests.get(url_packages, auth = basic)
      open('packages.json', 'wb').write(r1.content) # for debug
      
      # parse the json
      packages = json.loads(r1.content)
      
      for package in packages['value']:
          package_name = package['normalizedName']
          short_name = package_name.split('/')[1]
          versions_url = package['_links']['versions']['href']
          print(f'Package: {package_name} ({short_name})')
      
          # create a folder for each package
          package_folder = './'+short_name
          if not exists(package_folder):
              os.mkdir(package_folder)
      
          # fetch versions json
          r2 = requests.get(versions_url, auth = basic)
          versions = json.loads(r2.content)
          open(f'{package_folder}/versions.json', 'wb').write(r2.content) # for debug
      
          # This block iterates though the versions and discards ones that fall outside of semver e.g. x.x.x-canary or similar
          version_numbers = {}
          for package_version in versions['value']:
              # is it a release version? (semver compliant)
              version_number = package_version['normalizedVersion']
              match = re.search('^\d+.\d+.\d+$', version_number)
              if match:
                  split = version_number.split('.')
                  sortable_version = '%02d.%02d.%02d' % (int(split[0]),int(split[1]),int(split[2]))
                  version_numbers[sortable_version] = version_number
      
          # the dictionary keys are a sortable format of the version e.g. 00.00.00
          version_numbers = sorted(version_numbers.items(), key = lambda kv: kv[0])
          print(version_numbers) # for debug
      
          for package_version in version_numbers:
              version_number = package_version[1] # package_version is a tuple
              package_filename = f'{package_folder}/{short_name}-{version_number}.tgz'
      
              # dowload package if not previously downloaded
              if not exists(package_filename):
                  print(f'Downloading : {short_name}-{version_number}')
              
                  package_url = f'https://pkgs.dev.azure.com/{organization}/{project}/_apis/packaging/feeds/{feed_id}/npm/packages/{package_name}/versions/{version_number}/content?api-version=6.0-preview.1'
                  r3 = requests.get(package_url, allow_redirects=True, auth = basic)
                  open(package_filename, 'wb').write(r3.content)
              
              # publish the package if not previsouly published
              if not exists(package_filename+'.published'):
                  npm_publish = subprocess.run(["npm", "publish", package_filename])
      
                  if npm_publish.returncode == 0:
                      # create a file matching the package with .published extension to indicate successful publish
                      subprocess.run(["touch", package_filename+'.published'])
                  else:
                      print(f'Error publishing {short_name}-{version_number}. Code: {npm_publish.returncode}')
          
          print('done.')
      

      该脚本是幂等的,因为它保留了下载包的副本,并且在成功调用 npm publish 后还会触及 &lt;package name&gt;.published 文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-27
        • 2014-02-21
        • 1970-01-01
        • 2012-11-13
        • 2019-08-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多