如何通过DevOps Pipelines将Flask Python制品文件复制到Windows服务器
如何将Flask项目制品复制到仅支持IP+端口访问的远程Windows服务器
我有一个分为构建测试、部署两个阶段的Python Flask项目流水线,部署阶段需要把制品文件复制到只能通过IP加端口访问的远程Windows服务器,该怎么操作?
已尝试的两种方法及问题
方法1:使用Window Machine File Copy
配置代码如下:
inputs: SourcePath: '$(Build.ArtifactStagingDirectory)/flask-files.zip' MachineNames: 'IP:Port' # 替换为你的Windows服务器名称或IP地址 AdminUserName: 'username' # 替换为访问Windows服务器的管理员用户名 AdminPassword: 'password' # 替换为访问Windows服务器的管理员密码 TargetPath: 'C:\inetpub\wwwroot\sampletransfer' displayName: 'Copy Artifact to Windows Server'
执行后提示无法解析URL。
方法2:使用PowerShell@2
流水线配置代码:
displayName: 'Copy artifact to remote server' inputs: targetType: 'filePath' filePath: 'copy-artifact.ps1' arguments: '-ArtifactStagingDirectory "$(Build.ArtifactStagingDirectory)"'
对应的copy-artifact.ps1脚本:
[Parameter(Mandatory=$true)] [string]$ArtifactStagingDirectory $sourcePath = Join-Path -Path $ArtifactStagingDirectory -ChildPath "flask-files.zip" $targetPath = "\\myip:port\c$\inetpub\wwwroot\sampletransfer" $adminUsername = "myuser" $adminPassword = ConvertTo-SecureString "password" -AsPlainText -Force $credentials = New-Object System.Management.Automation.PSCredential($adminUsername, $adminPassword) Copy-Item -Path $sourcePath -Destination $targetPath -ToSession (New-PSSession -ComputerName myip -Port myport -Credential $credentials)
执行后出现错误:
WinRM客户端无法处理请求。如果认证方案不是Kerberos,或者客户端计算机未加入域,则必须使用HTTPS传输,或者将目标机器添加到TrustedHosts配置中。使用winrm.cmd配置TrustedHosts。请注意,TrustedHosts列表中的计算机可能不会被认证。可以运行以下命令获取更多信息:winrm help config。
解决方法
修复方法1的URL解析问题
Window Machine File Copy的MachineNames参数仅需填写IP或主机名,端口需通过Protocol和Port参数单独配置:
inputs: SourcePath: '$(Build.ArtifactStagingDirectory)/flask-files.zip' MachineNames: '你的服务器IP' AdminUserName: 'username' AdminPassword: 'password' TargetPath: 'C:\inetpub\wwwroot\sampletransfer' Protocol: 'HTTPS' # 根据你的WinRM配置选择HTTP或HTTPS Port: '你的端口号' # 例如5986(HTTPS默认)或5985(HTTP默认) displayName: 'Copy Artifact to Windows Server'
注意:若使用HTTP,需确保目标服务器允许WinRM通过HTTP,且客户端已将目标IP加入TrustedHosts。
修复方法2的WinRM错误
方案1:将目标IP添加到客户端TrustedHosts
在执行PowerShell脚本的代理机器上,以管理员权限运行:
# 添加单个IP winrm set winrm/config/client '@{TrustedHosts="你的服务器IP"}' # 添加多个IP(逗号分隔) winrm set winrm/config/client '@{TrustedHosts="IP1,IP2"}'
方案2:改用HTTPS连接WinRM
确保目标服务器已配置WinRM的HTTPS端点,然后修改PowerShell脚本,添加-UseSSL参数,同时修正目标路径写法:
[Parameter(Mandatory=$true)] [string]$ArtifactStagingDirectory $sourcePath = Join-Path -Path $ArtifactStagingDirectory -ChildPath "flask-files.zip" $targetPath = "C:\inetpub\wwwroot\sampletransfer\flask-files.zip" $adminUsername = "myuser" $adminPassword = ConvertTo-SecureString "password" -AsPlainText -Force $credentials = New-Object System.Management.Automation.PSCredential($adminUsername, $adminPassword) # 建立HTTPS会话 $session = New-PSSession -ComputerName myip -Port myport -Credential $credentials -UseSSL Copy-Item -Path $sourcePath -Destination $targetPath -ToSession $session Remove-PSSession $session # 用完关闭会话
方案3:检查目标服务器WinRM配置
在目标服务器上运行以下命令,确保WinRM已启用且端口正确监听:
# 快速配置WinRM winrm quickconfig # 查看WinRM监听端口 winrm enumerate winrm/config/listener
内容的提问来源于stack exchange,提问作者Usman Rafiq
相关产品推荐
相关产品推荐

