如何从 Azure DevOps 构建 .NET Core 工作线程服务并将其部署到本地环境

Hea*_*vyg 4 continuous-deployment azure-devops

我有一个工作服务应用程序,我通过复制其二进制文件并使用 PowerShell 通过“New-Service”命令进行安装,在开发服务器上手动安装该应用程序。

我们正在寻找 CI/CD 来自动化构建和部署其工件。我不知道如何将“构建”文件从 Azure 获取到本地服务器,我已经查看了诸如部署组之类的内容,但这些似乎不是用于复制的发布工具中的选项。我已经查看了“复制”工具和构建工具,但我被困住了。

有些人似乎使用“经典”提到这一点,我想我使用的是 YAML,但不是经典。

有人能指出我正确的方向吗?

Kri*_*óth 6

使用经典 UI 对我来说更简单(最后也是 YAML)

你需要的是:

  1. 将 Azure 部署代理安装到您的计算机上,以便它们“成为部署池的成员”。您可以通过部署池菜单生成安装脚本,您应该以本地管理员身份在计算机上运行该脚本。
  2. (我假设您有一个项目)在链接到该部署池的项目级别创建一个部署组
  3. 如果您还没有生成二进制文件的构建,请创建一个
  4. 创建新版本
  5. 在该版本中创建一个阶段
  6. 打开舞台,在舞台名称旁边的顶部菜单中,您可以看到三个点。单击它,然后“添加部署组作业”-> 这些旨在通过利用前面提到的部署代理在本地计算机上运行内容。
  7. 编写部署任务或从市场中选择一些。通常看起来像复制文件 -> 提取 -> 替换变量中的一些标记 -> 运行一些脚本或使用专用任务来安装应用程序

以及安装 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."
Run Code Online (Sandbox Code Playgroud)

像“$(ServiceName)”这样的变量是从 AzureDevops 发布变量中替换的。您可以在此处阅读有关变量用法的更多信息

设置常用服务选项也可能很有用。我通常通过单独的 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."
Run Code Online (Sandbox Code Playgroud)