AzureDevops发布Artifact

参考资料:http://almvm.devopshub.cn/labs/azuredevops/packagemanagement/#

1.AzureDevops中,找到Artifact,新建一个源,自定义一个包名字,比如:TestUtilityShared

 2.点击连接源

3.选择NuGet.exe,复制 右边的Key和Value。一会用得到

4.点击获取工具,去下载一个最新的Nuget.exe

5.下载 Azure Artifacts Credential Provider,地址:https://github.com/microsoft/artifacts-credprovider#azure-artifacts-credential-provider

其实主要需要【installcredprovider.ps1】这个文件,也可以看链接里的方法手动安装,我这边演示powershell命令安装

ps文件内容:

# A PowerShell script that adds the latest version of the Azure Artifacts credential provider
# plugin for Dotnet and/or NuGet to ~/.nuget/plugins directory
# To install netcore, run installcredprovider.ps1
# To install netcore and netfx, run installcredprovider.ps1 -AddNetfx
# To overwrite existing plugin with the latest version, run installcredprovider.ps1 -Force
# To use a specific version of a credential provider, run installcredprovider.ps1 -Version "0.1.17" or installcredprovider.ps1 -Version "0.1.17" -Force
# More: https://github.com/Microsoft/artifacts-credprovider/blob/master/README.md

param(
    # whether or not to install netfx folder for nuget
    [switch]$AddNetfx,
    # override existing cred provider with the latest version
    [switch]$Force,
    # install the version specified
    [string]$Version
)

$script:ErrorActionPreference='Stop'

# Without this, System.Net.WebClient.DownloadFile will fail on a client with TLS 1.0/1.1 disabled
if ([Net.ServicePointManager]::SecurityProtocol.ToString().Split(',').Trim() -notcontains 'Tls12') {
    [Net.ServicePointManager]::SecurityProtocol += [Net.SecurityProtocolType]::Tls12
}

$profilePath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::UserProfile)
$tempPath = [System.IO.Path]::GetTempPath()

$pluginLocation = [System.IO.Path]::Combine($profilePath, ".nuget", "plugins");
$tempZipLocation = [System.IO.Path]::Combine($tempPath, "CredProviderZip");

$localNetcoreCredProviderPath = [System.IO.Path]::Combine("netcore", "CredentialProvider.Microsoft");
$localNetfxCredProviderPath = [System.IO.Path]::Combine("netfx", "CredentialProvider.Microsoft");

$fullNetfxCredProviderPath = [System.IO.Path]::Combine($pluginLocation, $localNetfxCredProviderPath)
$fullNetcoreCredProviderPath = [System.IO.Path]::Combine($pluginLocation, $localNetcoreCredProviderPath)

$netfxExists = Test-Path -Path ($fullNetfxCredProviderPath)
$netcoreExists = Test-Path -Path ($fullNetcoreCredProviderPath)

# Check if plugin already exists if -Force swich is not set
if (!$Force) {
    if ($AddNetfx -eq $True -and $netfxExists -eq $True -and $netcoreExists -eq $True) {
        Write-Host "The netcore and netfx Credential Providers are already in $pluginLocation"
        return
    }

    if ($AddNetfx -eq $False -and $netcoreExists -eq $True) {
        Write-Host "The netcore Credential Provider is already in $pluginLocation"
        return
    }
}

# Get the zip file from the GitHub release
$releaseUrlBase = "https://api.github.com/repos/Microsoft/artifacts-credprovider/releases"
$versionError = "Unable to find the release version $Version from $releaseUrlBase"
$releaseId = "latest"
if (![string]::IsNullOrEmpty($Version)) {
    try {
        $releases = Invoke-WebRequest -UseBasicParsing $releaseUrlBase
        $releaseJson = $releases | ConvertFrom-Json
        $correctReleaseVersion = $releaseJson | ? { $_.name -eq $Version }
        $releaseId = $correctReleaseVersion.id
    } catch {
        Write-Error $versionError
        return
    }
}

if (!$releaseId) {
    Write-Error $versionError
    return
}

$releaseUrl = [System.IO.Path]::Combine($releaseUrlBase, $releaseId)
$releaseUrl = $releaseUrl.Replace("","/")

$zipFile = "Microsoft.NetCore2.NuGet.CredentialProvider.zip"
if ($AddNetfx -eq $True) {
    $zipFile = "Microsoft.NuGet.CredentialProvider.zip"
}
Write-Verbose "Using $zipFile"

$zipErrorString = "Unable to resolve the Credential Provider zip file from $releaseUrl"
try {
    Write-Host "Fetching release $releaseUrl"
    $release = Invoke-WebRequest -UseBasicParsing $releaseUrl
    $releaseJson = $release.Content | ConvertFrom-Json
    $zipAsset = $releaseJson.assets | ? { $_.name -eq $zipFile }
    $packageSourceUrl = $zipAsset.browser_download_url
} catch {
    Write-Error $zipErrorString
    return
}

if (!$packageSourceUrl) {
    Write-Error $zipErrorString
    return
}

# Create temporary location for the zip file handling
Write-Verbose "Creating temp directory for the Credential Provider zip: $tempZipLocation"
if (Test-Path -Path $tempZipLocation) {
    Remove-Item $tempZipLocation -Force -Recurse
}
New-Item -ItemType Directory -Force -Path $tempZipLocation

# Download credential provider zip to the temp location
$pluginZip = ([System.IO.Path]::Combine($tempZipLocation, $zipFile))
Write-Host "Downloading $packageSourceUrl to $pluginZip"
try {
    $client = New-Object System.Net.WebClient
    $client.DownloadFile($packageSourceUrl, $pluginZip)
} catch {
    Write-Error "Unable to download $packageSourceUrl to the location $pluginZip"
}

# Extract zip to temp directory
Write-Host "Extracting zip to the Credential Provider temp directory $tempZipLocation"
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory($pluginZip, $tempZipLocation)

# Remove existing content and copy netcore (and netfx) directories to plugins directory
if ($netcoreExists) {
    Write-Verbose "Removing existing content from $fullNetcoreCredProviderPath"
    Remove-Item $fullNetcoreCredProviderPath -Force -Recurse
}
$tempNetcorePath = [System.IO.Path]::Combine($tempZipLocation, "plugins", $localNetcoreCredProviderPath)
Write-Verbose "Copying Credential Provider from $tempNetcorePath to $fullNetcoreCredProviderPath"
Copy-Item $tempNetcorePath -Destination $fullNetcoreCredProviderPath -Force -Recurse
if ($AddNetfx -eq $True) {
    if ($netfxExists) {
        Write-Verbose "Removing existing content from $fullNetfxCredProviderPath"
        Remove-Item $fullNetfxCredProviderPath -Force -Recurse
    }
    $tempNetfxPath = [System.IO.Path]::Combine($tempZipLocation, "plugins", $localNetfxCredProviderPath)
    Write-Verbose "Copying Credential Provider from $tempNetfxPath to $fullNetfxCredProviderPath"
    Copy-Item $tempNetfxPath -Destination $fullNetfxCredProviderPath -Force -Recurse
}

# Remove $tempZipLocation directory
Write-Verbose "Removing the Credential Provider temp directory $tempZipLocation"
Remove-Item $tempZipLocation -Force -Recurse

Write-Host "Credential Provider installed successfully"

  可以直接拷贝我上面的代码保存成ps1文件

6.执行命令在当前环境安装证书:

C:下载installcredprovider.ps1 -AddNetfx

 7.前往路径 

C:Users用户名.nugetplugins etfxCredentialProvider.Microsoft 

找到 CredentialProvider.Microsoft.exe 文件

 8.将刚刚第【4】步下载的nuget.exe和CredentialProvider.microsoft.exe 拷贝到 你需要发布成Artifact包的项目下的含有 [.csproj]文件的目录下

9.这时候需要确保已经安装了.net Framework Runtime,如果没安装的可以先去下载安装:https://dotnet.microsoft.com/download

10.进入需要打包的项目,添加一个测试文件TestHelper.cs,代码如下:

 保存,然后生成项目,这时候我们即将生成的包版本号是“1.0.0.0.0”,可以查看Properties-AssemblyInfo.cs,查看版本号(后续发布的包需要修改该版本号)

11.进入项目文件夹(含有csproj的文件夹), 选择文件-打开Windows PowerShell-以管理员身份打开Windows PowerShell ,然后执行命令

./nuget.exe pack NYPublicUtility.Shared.csproj

 

12.执行完命令后,我们可以看到目录下多了个文件:NYTestUtility.Shared.1.0.0.nupkg ,下面就是发布到Azure Devops Artifact上了,先在VS中选择

工具-NuGet包管理器-程序包管理器设置

 在程序包源中,点击绿色+号,添加一个名称和源是我们第3步中拷贝的值

点击更新,然后点击确定保存。

再回到powershell命令行,执行命令:

./nuget.exe push -source "TestUtilityShared" -ApiKey az NYTestUtility.Shared.1.0.0.nupkg

13.这时候回到AzureDevopsArtifact页面,刷新一下,就能看到发布的包了

 

14.在我们需要引用包的测试项目中,右键项目,选择管理NuGet程序包

 选择浏览,右上角的程序包源选择我们刚刚新建的源

 这时候我们就能看到我们新上传的包了

备注:我们自己上传的包的使用方法,和公共包的使用方式一样, 后续的版本更新只需要重新打包并且上传,然后更新包就行。

原文地址:https://www.cnblogs.com/King-JJ/p/14718942.html