如何通过Azure DevOps Pipeline从VM复制文件至System.ArtifactsDirectory或GitHub仓库
从VM复制文件到Azure DevOps Pipeline工件目录的解决方案
因为CopyFilesOverSSH@0仅支持从Pipeline工件目录推送文件至VM,无法反向拉取,你可以通过以下直接的方式实现需求:
方案1:使用SSH@0任务执行scp拉取命令
通过SSH任务直接运行scp命令,从远程VM拉取文件到Pipeline代理的$(System.ArtifactsDirectory)目录。前提是你已经在Azure DevOps中配置好SSH服务连接(包含VM地址、认证信息)。
YAML示例:
- task: SSH@0 displayName: 从远程VM拉取文件至工件目录 inputs: sshEndpoint: '你的SSH服务连接名称' # 替换为你在Azure DevOps中配置的SSH连接名 runOptions: 'commands' commands: | # 拉取单个文件示例 scp 你的VM用户名@你的VMIP:/remote/path/your-file.txt $(System.ArtifactsDirectory)/ # 拉取整个目录示例(加-r参数) # scp -r 你的VM用户名@你的VMIP:/remote/path/* $(System.ArtifactsDirectory)/ failOnStdErr: false # 可选:若scp输出非致命错误不想中断Pipeline,设为false
方案2:使用SSH@0任务执行远程打包+拉取(适合大文件/多文件)
如果要拉取的文件较多,可先在VM上打包压缩,再拉取压缩包到Pipeline,最后解压到工件目录:
YAML示例:
- task: SSH@0 displayName: 在VM上打包目标文件并拉取 inputs: sshEndpoint: '你的SSH服务连接名称' runOptions: 'commands' commands: | # 在VM上打包目标目录 tar -czf /tmp/backup.tar.gz /remote/target/path/ # 拉取压缩包到Pipeline工件目录 scp /tmp/backup.tar.gz $(System.ArtifactsDirectory)/ # 可选:删除VM上的临时压缩包 rm /tmp/backup.tar.gz failOnStdErr: false # 添加解压任务(若使用打包方案) - task: ExtractFiles@1 displayName: 解压拉取的压缩包到工件目录 inputs: archiveFilePatterns: '$(System.ArtifactsDirectory)/backup.tar.gz' destinationFolder: '$(System.ArtifactsDirectory)' cleanDestinationFolder: false
后续同步到GitHub仓库
文件拉取到$(System.ArtifactsDirectory)后,可通过Git任务将内容同步至GitHub:
- task: Git@2 displayName: 同步工件目录至GitHub仓库 inputs: repositoryUrl: 'https://github.com/你的用户名/你的仓库名.git' command: 'push' branch: 'main' # 替换为目标分支 pushSubmodule: true tags: true # 需提前在Azure DevOps变量组中配置GitHub PAT作为认证,或使用服务连接
内容的提问来源于stack exchange,提问作者Mudit Balooja
相关产品推荐
相关产品推荐

