简体   繁体   English

Azure DevOps 自托管代理 - 如何复制云托管代理?

[英]Azure DevOps Self-Hosted Agent - How to replicate cloud-hosted agents?

I wonder if, for simplicity reasons, it is possible to create Azure DevOps self-hosted agents locally, reproducing all capabilities of the cloud-hosted ones.我想知道,为了简单起见,是否可以在本地创建 Azure DevOps 自托管代理,重现云托管代理的所有功能。 I need to use self-hosted agents, but do not want to create installation and upgrade scripts for each and every application on them.我需要使用自托管代理,但不想为它们上的每个应用程序创建安装和升级脚本。

I would imagine there is something like a VM image with all tools preinstalled;我想有一个像预装了所有工具的 VM 映像; possibly the same as in Azure DevOps.可能与 Azure DevOps 中的相同。 This could potentially have the benefit of 100% compatibility.这可能具有 100% 兼容性的好处。

What I have found so far:到目前为止我发现了什么:

How can I create "the perfect Azure DevOps agent"?如何创建“完美的 Azure DevOps 代理”?

How can I create "the perfect Azure DevOps agent"?如何创建“完美的 Azure DevOps 代理”?

I have had the same request as you before, I agree with your views 2 and 3.我之前和你有同样的要求,我同意你的观点2和3。

But because I am not very proficient in docker technology and need to maintain my docker environment frequently, I choose to use packer to build my image.但是因为我对docker技术不是很精通,需要经常维护我的docker环境,所以我选择使用packer来构建我的镜像。

You could check below great document for some more details:您可以查看以下很棒的文档以获取更多详细信息:

Build your own Hosted VSTS Agent Cloud: Part 1 - Build 构建您自己的托管 VSTS 代理云:第 1 部分 - 构建

Build your own Hosted VSTS Agent Cloud: Part 2 - Deploy 构建您自己的托管 VSTS 代理云:第 2 部分 - 部署

Looks like we are in the same rabbit-hole.看起来我们在同一个兔子洞里。 I started out with the same question as you posted, and it looks like you are on to the answer - setup a VM localy or on Azure and you are good to go.我从与您发布的相同问题开始,看起来您已经找到了答案 - 在本地或 Azure 上设置 VM,您就可以开始了。 The links in the answer from "Leo Liu" is probably a good place to start. “Leo Liu”回答中的链接可能是一个很好的起点。 However - VMs are not Docker and this doesn't answer your broader question.但是 - 虚拟机不是 Docker,这不能回答您更广泛的问题。

If the question is rephrased as "Why don't Microsoft provide an easy way to setup self-hosted agents in docker containers?"如果问题被改写为“为什么 Microsoft 不提供一种在 docker 容器中设置自托管代理的简单方法?” I believe the answer lies in their business model.我相信答案在于他们的商业模式。 Local VMs need Windows licenses and Azure VMs are billed by the hour...本地 VM 需要 Windows 许可证,Azure VM 按小时计费...

But, conspiracy theories aside, I don't think there is an easy way to setup a dockerised version of the cloud-hosted agents.但是,撇开阴谋论不谈,我认为没有一种简单的方法可以设置云托管代理的 dockerised 版本。 It's probably not a very good idea either.这可能也不是一个好主意。 Docker containers are meant to be small, and with all of the dependencies on those agents they are anything but small. Docker 容器本来就应该很小,并且由于对这些代理的所有依赖,它们绝非小事。 It's also never going to be "perfect", as you say, since the dockerized windows isn't identical to what's running in their cloud-hosted VMs.正如您所说,它也永远不会“完美”,因为 dockerized 窗口与其云托管 VM 中运行的窗口不同。

I have started tweeking something that is not perfect, but might work:我已经开始调整一些不完美但可能有效的东西:

  • setup a docker-agent according to the documentation here根据此处的文档设置 docker-agent
  • add the essence from the PS-scripts corresponding to the packages you need from here此处添加与您需要的软件包相对应的 PS 脚本中的精华
  • add commands to the Dockerfile to COPY and RUN the scripts将命令添加到 Dockerfile 以COPYRUN脚本
  • docker build as usual and you should have a container that's a bit more capable and an agent that reports its capabilities in a similar way to the cloud-agents docker build像往常一样,你应该有一个更强大的容器和一个以类似于云代理的方式报告其功能的代理

In an ideal world there would be a repository with all of the tweeked scripts, and a community that kept them updated.在理想的世界中,会有一个包含所有 tweeked 脚本的存储库,以及一个保持它们更新的社区。 In an even more ideal world that would be a Microsoft hosted repository, but like I said - that's probably unlikely.在一个更理想的世界中,它将是 Microsoft 托管的存储库,但就像我说的那样 - 这可能不太可能。

Here is some code to get you started.这是一些帮助您入门的代码。 Maybe I'll publish a more finished version somewhere in the future.也许我会在未来的某个地方发布一个更完整的版本。

init.ps1 with some lines borrowed from here : init.ps1 从这里借用了一些行:

Write-Host "Install chocolatey"
$chocoExePath = 'C:\ProgramData\Chocolatey\bin'

if ($($env:Path).ToLower().Contains($($chocoExePath).ToLower())) {
    Write-Host "Chocolatey found in PATH, skipping install..."
    Exit
}

$systemPath = [Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::Machine)
$systemPath += ';' + $chocoExePath
[Environment]::SetEnvironmentVariable("PATH", $systemPath, [System.EnvironmentVariableTarget]::Machine)

$userPath = [Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::User)
if ($userPath) {
    $env:Path = $systemPath + ";" + $userPath
}
else {
    $env:Path = $systemPath
}

Invoke-Expression ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))

choco feature enable -n allowGlobalConfirmation

Remove-Item -Path $env:ChocolateyInstall\bin\cpack.exe -Force

Import-Module "$env:ChocolateyInstall\helpers\chocolateyInstaller.psm1" -Force
Get-ToolsLocation

Modified Dockerfile from the Microsoft documentation, that also runs the script on build:从 Microsoft 文档修改 Dockerfile,它也在构建时运行脚本:

FROM mcr.microsoft.com/windows/servercore:ltsc2019

COPY init.ps1 /Windows/Temp/init.ps1

RUN powershell -executionpolicy bypass C:\Windows\Temp\init.ps1

WORKDIR /azp

COPY start.ps1 .

CMD powershell .\start.ps1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM