Azure Devops 在管道中找不到要复制到远程计算机的文件

Posted

技术标签:

【中文标题】Azure Devops 在管道中找不到要复制到远程计算机的文件【英文标题】:Azure Devops can't find file(s) in pipeline to copy to remote machine 【发布时间】:2022-01-08 17:33:17 【问题描述】:

我正在为 Azure Devops 中的 yml 管道而苦苦挣扎。我创建了 stage.yml、build.yml 和 deploy.yml 文件。 stage.yml 非常前卫,它有两个阶段,可将您引导至 build.yml 或 deploy.yml。

deploy.yml 应该从 build.yml 中获取工件结果并使用 SSH 将其复制到我的远程环境。 Azure 将工件显示为第一阶段的结果,这样就可以了。但是,每次我运行作业时,它都会在运行输出中显示此消息:

Starting: Copy JAR to host deploy
==============================================================================
Task         : Copy files over SSH
Description  : Copy files or build artifacts to a remote machine over SSH
Version      : 0.189.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/deploy/copy-files-over-ssh
==============================================================================
Setting up SSH service connection to remote host xx.xx.xx.xx.
Found 0 files to copy to the remote machine.
Completed copying 0 files to the remote machine.
Finishing: Copy JAR to host deploy

我的配置中是否缺少某些内容,或者只是路径配置错误?如果有,应该如何配置?

我的 build.yml 看起来像这样:

parameters:
- name: incomingFeedName
  type: string
  default: 'project'

jobs:
- job: build_maven
  displayName: Build
  pool:
    vmImage: ubuntu-latest

  variables:
    - name: MAVEN_CACHE_FOLDER
      value: $(Pipeline.Workspace)/.m2/repository
    - name: MAVEN_OPTS
      value: '-Dmaven.repo.local=$(MAVEN_CACHE_FOLDER)'

  steps:
  - task: Cache@2
    # This task create cache of your all dependancies to fast the build for next time
    # https://docs.microsoft.com/en-us/azure/devops/pipelines/caching/?view=azure-devops
    inputs:
      key: 'maven4 | "$(Agent.OS)" | **/pom.xml,!**/target/**/pom.xml'
      restoreKeys: |
        maven4 | "$(Agent.OS)"
        maven4
      path: $(MAVEN_CACHE_FOLDER)
    displayName: 'Cache local maven repo'

  - task: MavenAuthenticate@0
    # This task will authenticate your maven feed for input dependancies and output dependancies
    inputs:
      artifactsFeeds: $parameters.incomingFeedName
    displayName: 'Authenticate to Azure Maven feed'

  - task: Maven@3
    inputs:
      mavenPomFile: 'pom.xml'
      mavenOptions: '-Xmx3072m $(MAVEN_OPTS)'
      javaHomeOption: 'JDKVersion'
      jdkVersionOption: '11'
      jdkArchitectureOption: 'x64'
      publishJUnitResults: true
      testResultsFiles: '**/surefire-reports/TEST-*.xml'
      mavenVersionOption: 'Default'
      mavenAuthenticateFeed: false
      goals: 'deploy'
    displayName: 'Maven build'

  - task: CopyFiles@2
    # pick up the results of the build (JAR file) and stage them.
    inputs:
      SourceFolder: '$(System.DefaultWorkingDirectory)/target'
      Contents: '*.jar'
      TargetFolder: '$(Build.ArtifactStagingDirectory)'
    displayName: Stage Jar files for publishing

  - task: PublishPipelineArtifact@1
    # Upload the results into the pipeline. This files can be used in the release pipeline
    inputs:
      targetPath: '$(Build.ArtifactStagingDirectory)'
      artifact: 'Binaries'
      publishLocation: 'pipeline'
    displayName: Publish Jar file

还有 deploy.yml:

parameters:
- name: envName
  type: string

jobs:
- deployment: deploy_jar
  displayName: Deploy JAR
  environment: $ parameters.envName 
  variables:
 # - group: 'global'
  - name: 'endpoint'
    $ if eq(parameters.envName, 'Test') :
      value: 'deploy'
 # pool:
  #  name: 'poolname here'
  strategy:
    runOnce:
      deploy:
        steps:
          # - task: DeleteFiles@1
          #   inputs:
          #     sourceFolder: '$Pipeline.Workspace)\Binaries'
          #     contents: '*'
          #     removeSourceFolder: true
          #   displayName: 'Clear JAR file from pipeline downloads: Before'
          # - download: current
          #   artifact: Binaries

          - task: CopyFilesOverSSH@0
            inputs:
              sshEndpoint: $variables.endpoint
              sourceFolder: '$(Build.ArtifactStagingDirectory)'
              # contents: '*'
              targetFolder: '~/_work/'
              readyTimeout: '20000'
              # overWrite: true
            displayName: 'Copy JAR to host $variables.endpoint'

          # - task: SSH@0
          #   inputs:
          #     sshEndpoint: $variables.endpoint
          #     runOptions: 'commands'
          #     commands: 'execute service'
          #     readyTimeout: '200'
          #   displayName: 'Run service'

          # - task: DeleteFiles@1
          #   inputs:
          #     sourceFolder: '$Pipeline.Workspace)\Binaries'
          #     contents: '*'
          #     removeSourceFolder: true
          #   displayName: 'Clear JAR file from pipeline downloads: After'

【问题讨论】:

【参考方案1】:

在部署时,您的构建工件将下载到$(Pipeline.Workspace)$(Build.ArtifactStagingDirectory) 在这里无效。

您可以通过在您的副本之前添加这样的步骤来验证这一点:

- pwsh: |
    Write-Host "Build.ArtifactStagingDirectory is $(Build.ArtifactStagingDirectory)"
    Write-Host "Pipeline.Workspace is $(Pipeline.Workspace)"
    Get-ChildItem $(Build.ArtifactStagingDirectory)
    Get-ChildItem $(Pipeline.Workspace)

【讨论】:

以上是关于Azure Devops 在管道中找不到要复制到远程计算机的文件的主要内容,如果未能解决你的问题,请参考以下文章

在为 ARM 模板部署创建的 Azure DevOps Pipeline 中找不到 JSON 文件路径

Azure DevOps 管道中的 Carthage 复制框架失败

如何通过 SSH 在 azure devops 管道复制文件中使用公钥

Azure DevOps YAML并行运行未排队

用于运行进程的 Azure DevOps 管道

Azure Devops 找不到恢复的 nuget 包