Azure devops pipeline restAPI 如何使用复制文件任务的源文件夹传递变量,容器中的自托管代理

Azure devops pipeline restAPI How to pass varible with SourceFolder for CopyFiles Task , self hosted agent in container

我的设置如下 我有托管代理,作为第一份工作从作为 docker 容器

启动的 self-hosted 代理复制文件

托管管道由管道“运行”触发 rest API :
https://docs.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/run%20pipeline?view=azure-devops-rest-6.0

这就是 body 现在的样子:

"resources": {
        "repositories:": {
            "self": {
                "refName": "refs/heads/my_branch"
            }
        }
    }

效果很好。
现在托管管道的一部分如下所示:

- job: self_hosted_connect
  timeoutInMinutes: 10
  pool: Default
  steps:
  - task: CopyFiles@2
    inputs:
      SourceFolder: '/home/copy_dir'
      Contents: '**'
      TargetFolder: '$(build.artifactstagingdirectory)'

还有,干得好。

我的问题是:

  1. 我想在“运行”中发送 rest API 另一个包含 SourceFolder 路径的参数 这样 CopyFiles 任务将是动态的并且没有硬编码 SourceFolder 路径

  2. 当我 运行 来自 docker 的 self-hosted 代理时,我如何告诉 self-hosted 代理将目录包含在其工作目录之外?所以管道不会因错误而失败:

    #[error]未处理:未找到 SourceFolder:/home/copy_dir

更新 我将请求更新为:

{
    "resources": {
        "repositories:": {
            "self": {
                "refName": "refs/heads/my_branch"
            }
        }
    },
    "templateParameters": {
        "Folderpath":"{/home/foo/my_dir}"
    }
}

但我收到一个错误:

{
    "$id": "1",
    "innerException": null,
    "message": "Unexpected parameter 'Folderpath'",
    "typeName": "Microsoft.Azure.Pipelines.WebApi.PipelineValidationException, Microsoft.Azure.Pipelines.WebApi",
    "typeKey": "PipelineValidationException",
    "errorCode": 0,
    "eventId": 3000
}

send in the "run" rest API another parameter that contains the SourceFolder path We can use runtime parameters in pipeline.

YAML 示例:

parameters:
- name: Folderpath
  displayName: 'configure Folder path'
  type: string
  default: {SourceFolder path}

steps:
- task: CopyFiles@2
  inputs:
    SourceFolder: '${{ parameters.Folderpath}}'
    Contents: '**'
    TargetFolder: '$(build.artifactstagingdirectory)'

请求URL:

POST https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=6.0-preview.1

请求正文:

{
    "resources":{
        "repositories":{
            "self":{"refName":"refs/heads/{my_branch}"
            }
         }
    },
    "templateParameters": {
        "Folderpath":"{SourceFolder path}"
        }
}

how do i tell the self-hosted agent to include the directory outside its working dir?

我们可以复制本地文件夹或azure DevOps predefined variables来定义源文件夹。

更新1

我们应该在 YAML 构建中定义参数,否则,我们将收到错误 Unexpected parameter 'Folderpath'"

更新 2

因为我喜欢它取自 self-hosted docker 所在光盘上的真实路径(我在请求中传递的路径) 运行 而不是相对于 docker 工作目录,所以现在它给了我这个错误:

[error]Unhandled: Not found SourceFolder: /azp/agent/_work/1/s/{/home/copy_dir}  
 

其中 /azp 是 docker 工作目录

我从这个 link 配置了 docker :
https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/docker?view=azure-devops