如何通过AKS上的自托管Azure DevOps代理访问本地网络驱动器?
AKS自托管代理对接本地服务器的CI/CD操作方案
前提说明
确保AKS集群与目标本地服务器网络连通(如同一VNet、VPN专线打通或端口开放),同时代理服务账号拥有本地服务器的操作权限(执行脚本、访问共享目录)。敏感凭证(如SSH密钥、账号密码)需存储在Azure DevOps变量组并标记为保密,避免明文暴露。
Linux版AKS自托管代理方案
1. 在本地服务器运行PowerShell脚本
Linux代理需预装PowerShell Core(可在自定义代理镜像中提前安装,或流水线任务中临时安装),通过SSH远程执行脚本:
- 步骤1:将本地服务器的SSH私钥上传至Azure DevOps安全文件,流水线中引用该文件
- 步骤2:添加Bash任务执行以下命令:
# 设置私钥权限 chmod 600 $(sshKeyPath) # 远程登录本地服务器执行PowerShell脚本 ssh -i $(sshKeyPath) user@local-server-ip "pwsh -File /path/to/target-script.ps1"
若本地是Windows服务器,需先开启SSH服务,或改用WinRM协议(需安装pywinrm等工具)
2. 复制构建工件到本地网络共享驱动器
Linux系统通过cifs-utils挂载Windows共享目录,完成文件复制:
- 步骤1:在代理镜像中预装
cifs-utils(或流水线中临时安装:apt-get update && apt-get install -y cifs-utils) - 步骤2:添加Bash任务挂载共享并复制文件:
# 创建挂载目录 mkdir -p /mnt/local-share # 挂载Windows共享(用户名、密码从Azure DevOps保密变量获取) mount -t cifs //local-server-ip/share-name /mnt/local-share -o username=$(share-username),password=$(share-password),domain=$(domain-name) # 复制构建工件到共享目录 cp -r $(Build.ArtifactStagingDirectory)/* /mnt/local-share/ # 卸载挂载(可选,容器销毁时自动清理) umount /mnt/local-share
Windows版AKS自托管代理方案
1. 在本地服务器运行PowerShell脚本
通过WinRM远程执行脚本,利用Azure DevOps保密变量存储凭证:
- 添加PowerShell任务执行以下脚本:
# 从变量组获取凭证 $securePassword = ConvertTo-SecureString $(local-server-password) -AsPlainText -Force $credential = New-Object System.Management.Automation.PSCredential ($(local-server-username), $securePassword) # 远程执行目标脚本 Invoke-Command -ComputerName local-server-ip -ScriptBlock { & "C:\path\to\task-script.ps1" } -Credential $credential
若本地服务器与代理在同一域,可省略凭证参数,直接使用代理账号权限
2. 复制构建工件到本地网络共享驱动器
直接通过PowerShell映射共享并复制文件:
- 添加PowerShell任务执行以下脚本:
# 创建凭证对象 $securePassword = ConvertTo-SecureString $(share-password) -AsPlainText -Force $credential = New-Object System.Management.Automation.PSCredential ($(share-username), $securePassword) # 映射共享驱动器 New-PSDrive -Name Z -PSProvider FileSystem -Root "\\local-server-ip\share-name" -Credential $credential # 复制构建工件 Copy-Item -Path "$(Build.ArtifactStagingDirectory)\*" -Destination "Z:\" -Recurse -Force # 卸载驱动器 Remove-PSDrive Z
内容的提问来源于stack exchange,提问作者Aakansha Verma
相关产品推荐
相关产品推荐

