如何从 Azure DevOps 构建 .NET Core 辅助角色服务并将其部署到本地环境
How can I build and deploy a .NET Core worker service to on-premise environment from Azure DevOps
我有一个工作人员服务应用程序,我通过复制其二进制文件并使用 PowerShell 通过 "New-Service" 命令在开发服务器上手动安装它。
我们正在研究 CI/CD 以自动构建和部署其工件。我无法弄清楚如何将 "built" 文件从 Azure 获取到本地服务器,我查看了部署组之类的东西,但这些似乎不是用于复制的发布工具中的选项。我查看了 "copy" 工具和构建工具,但我被卡住了。
有些人似乎提到这个使用 "classic",我想我使用的是 YAML,虽然不是经典的。
谁能给我指出正确的方向?
经典的UI对我来说更直接一些(最后也是YAML)
您需要的是:
- 将 Azure 部署代理安装到您的计算机上,以便它们 "become members" 部署池。您可以通过部署池菜单生成安装脚本,您应该以本地管理员身份在计算机上 运行。
- (我假设您有一个项目)在链接到该部署池的项目级别创建一个部署组
- 如果您还没有生成二进制文件的构建,请创建一个
- 创建新版本
- 在该版本中创建一个阶段
- 打开舞台,在舞台名称旁边的顶部菜单上,您可以看到三个点。单击它,然后 "Add deployment group job" -> 这些是 运行 通过使用前面提到的部署代理在本地机器上的东西。
- 编写部署任务或从市场中选择一些。通常看起来像复制文件 -> 提取 -> 从变量中替换一些标记 -> 运行 一些脚本或使用专用任务来安装应用程序
安装windows服务的一些帮助(你可以做一个任务,但类似的已经存在)
$serviceName = "$(ServiceName)"
$serviceDisplayName = "$(ServiceDisplayName)"
$serviceDescription = "$(ServiceDescription)"
$exePath = "$(ServiceExeFullPath)"
$username = "NT AUTHORITY\NETWORK SERVICE"
$password = convertto-securestring -String "dummy" -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
Write-Host "====================================="
Write-Host $serviceName
Write-Host $serviceDisplayName
Write-Host $serviceDescription
Write-Host $exePath
Write-Host "====================================="
$existingService = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'"
if ($existingService)
{
"'$serviceName' exists already. Stopping."
Stop-Service $serviceName
"Waiting 5 seconds to allow existing service to stop."
Start-Sleep -s 5
"Seting new binpath for the service '$serviceName'"
sc.exe config $serviceName binpath= $exePath
"Waiting 5 seconds to allow service to be re-configured."
Start-Sleep -s 5
}
else
{
"Installing the service '$serviceName'"
New-Service -BinaryPathName $exePath -Name $serviceName -Credential $cred -DisplayName $serviceDisplayName -Description $serviceDescription -StartupType Automatic
"Service installed"
"Waiting 5 seconds to allow service to be installed."
Start-Sleep -s 5
}
"Starting the service."
Start-Service $serviceName
"Completed."
像“$(ServiceName)”这样的变量是从 AzureDevops 发布变量中替换的。您可以阅读有关变量用法的更多信息 HERE
设置常用服务选项可能也很有用。我通常通过单独的 powershell 任务来完成:
$serviceName = "$(ServiceName)"
$failureDelay = [int] $(ServiceFailureDelayMs)
$failureAction = "restart"
$reset = [int] $(ServiceResetSeconds)
$service = Get-Service $serviceName -ErrorAction SilentlyContinue
if(!$service)
{
Write-Host "##vso[task.LogIssue type=warning;]Directory Windows Service '$serviceName' not found, skip."
return
}
"Updating '$serviceName' service recovery options."
sc.exe failure $service.Name actions= $failureAction/$failureDelay/$failureAction/$failureDelay/$failureAction/$failureDelay reset= $reset
"Done."
我有一个工作人员服务应用程序,我通过复制其二进制文件并使用 PowerShell 通过 "New-Service" 命令在开发服务器上手动安装它。
我们正在研究 CI/CD 以自动构建和部署其工件。我无法弄清楚如何将 "built" 文件从 Azure 获取到本地服务器,我查看了部署组之类的东西,但这些似乎不是用于复制的发布工具中的选项。我查看了 "copy" 工具和构建工具,但我被卡住了。
有些人似乎提到这个使用 "classic",我想我使用的是 YAML,虽然不是经典的。
谁能给我指出正确的方向?
经典的UI对我来说更直接一些(最后也是YAML)
您需要的是:
- 将 Azure 部署代理安装到您的计算机上,以便它们 "become members" 部署池。您可以通过部署池菜单生成安装脚本,您应该以本地管理员身份在计算机上 运行。
- (我假设您有一个项目)在链接到该部署池的项目级别创建一个部署组
- 如果您还没有生成二进制文件的构建,请创建一个
- 创建新版本
- 在该版本中创建一个阶段
- 打开舞台,在舞台名称旁边的顶部菜单上,您可以看到三个点。单击它,然后 "Add deployment group job" -> 这些是 运行 通过使用前面提到的部署代理在本地机器上的东西。
- 编写部署任务或从市场中选择一些。通常看起来像复制文件 -> 提取 -> 从变量中替换一些标记 -> 运行 一些脚本或使用专用任务来安装应用程序
安装windows服务的一些帮助(你可以做一个任务,但类似的已经存在)
$serviceName = "$(ServiceName)"
$serviceDisplayName = "$(ServiceDisplayName)"
$serviceDescription = "$(ServiceDescription)"
$exePath = "$(ServiceExeFullPath)"
$username = "NT AUTHORITY\NETWORK SERVICE"
$password = convertto-securestring -String "dummy" -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
Write-Host "====================================="
Write-Host $serviceName
Write-Host $serviceDisplayName
Write-Host $serviceDescription
Write-Host $exePath
Write-Host "====================================="
$existingService = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'"
if ($existingService)
{
"'$serviceName' exists already. Stopping."
Stop-Service $serviceName
"Waiting 5 seconds to allow existing service to stop."
Start-Sleep -s 5
"Seting new binpath for the service '$serviceName'"
sc.exe config $serviceName binpath= $exePath
"Waiting 5 seconds to allow service to be re-configured."
Start-Sleep -s 5
}
else
{
"Installing the service '$serviceName'"
New-Service -BinaryPathName $exePath -Name $serviceName -Credential $cred -DisplayName $serviceDisplayName -Description $serviceDescription -StartupType Automatic
"Service installed"
"Waiting 5 seconds to allow service to be installed."
Start-Sleep -s 5
}
"Starting the service."
Start-Service $serviceName
"Completed."
像“$(ServiceName)”这样的变量是从 AzureDevops 发布变量中替换的。您可以阅读有关变量用法的更多信息 HERE
设置常用服务选项可能也很有用。我通常通过单独的 powershell 任务来完成:
$serviceName = "$(ServiceName)"
$failureDelay = [int] $(ServiceFailureDelayMs)
$failureAction = "restart"
$reset = [int] $(ServiceResetSeconds)
$service = Get-Service $serviceName -ErrorAction SilentlyContinue
if(!$service)
{
Write-Host "##vso[task.LogIssue type=warning;]Directory Windows Service '$serviceName' not found, skip."
return
}
"Updating '$serviceName' service recovery options."
sc.exe failure $service.Name actions= $failureAction/$failureDelay/$failureAction/$failureDelay/$failureAction/$failureDelay reset= $reset
"Done."