nuget包也要自动化部署了,想想确实挺好,在实施过程中我们要解决的问题有版本自动控制,nuget自动打包,nuget自动上传到服务端等。
一 参数化构建
二 环境变量的k/v参数,存储类库的初始版本,当根目录version.txt生成后,这个k/v就不需要了
三 这个构建跳转到哪台节点服务器
四 使用ps插件,完成version.txt的建立和更新
$initVersion=[Environment]::GetEnvironmentVariable("${env:projectName}")
#版本文件目录
$VersionFileDirectory="${env:WORKSPACE}/NugetServices/${env:projectName}"
#版本文件名字
$VersionFileName="version.txt"
#版本文件路径
$VersionFilePath="$VersionFileDirectory\$VersionFileName"
#初始版本变量值 1.0.0.0
$InitVersionValue="100";
#版本长度1.0.0.0 =4
$VersionLength=3
Function UpdateVersion($vvalue,$vlength,$vfilepath)
{
$content=$(Get-Content -Path $vfilepath)
if([string]::IsNullOrEmpty($content))
{
Write-Host "version file don't exist ,creating version file......"
SetVersion $vvalue $vlength $vfilepath
}
else
{
$versionvalue=$([string]$content)
Write-Host "old version: $versionvalue"
$versionvalues=$([int]([string]$versionvalue).Replace(".",""))
$versionvalues=$(($versionvalues+1).ToString())
SetVersion $versionvalues $vlength $vfilepath
}
}
#设置版本值,版本名,版本值,版本长度,版本文件路径
Function SetVersion($vvalue,$vlength,$vfilepath)
{
if(-Not (Test-Path -Path $vfilepath))
{
$null=New-Item -Path $vfilepath -ItemType File -Force
}
$value=GetVersion $vvalue $vlength
Set-Content -Path $vfilepath -Value "$value"
}
Function GetVersion($value,$versionlength)
{
$value=[string]$value
$versionlength=[int]$versionlength
$versionvalue="";
$num=$value.Length-$versionlength+1
for($i=0;$i -lt $versionlength;$i++)
{
if($i -eq 0)
{
$versionvalue= $value.Substring(0,$num)+"."
}
else
{
$index=$i+$num-1
$versionvalue=$versionvalue+$value[$index]+"."
}
}
$result=$versionvalue.Trim(".");
Write-Host "new version: $result"
return $result;
}
if(-Not(Test-Path -Path $VersionFilePath))
{
SetVersion $initVersion $VersionLength $VersionFilePath
}
else {
UpdateVersion $InitVersionValue $VersionLength $VersionFilePath
}
View Code
五 使用cmd,完成.net core项目的发布和打包,注意如果是frameworks项目,需要使用nuget.exec 完成这个功能。
NGUET方法:(nuget pack NugetServices/Pilipa.Utility -version 2.1.3)
path "C:\Program Files\dotnet"
cd "NugetServices/%projectName%"
set /p version=<version.txt
dotnet restore --configfile ../../NuGet.Config
dotnet build
dotnet pack -o nugets /p:version=%version%
dotnet nuget push nugets/%projectName%.%version%.nupkg -k abc123 -s https://nugetserver.i-counting.cn/
好了,以上就是我在nuget打包实现自动化部署的过程!
感谢阅读!
作者:仓储大叔,张占岭,
荣誉:微软MVP