Azure devops管道restAPI如何通过SourceFolder for CopyFiles Task传递变量,容器中的自托管代理

问题描述

我的设置如下 我已经托管了代理,作为第一项工作,它从作为Docker容器启动的自托管代理复制文件

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

这是现在身体的样子:

data.real - data.fiction

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

import calendar
for index,row in data.iterrows():
    if row.maskr:
        print('Going for',index)
        date = calendar.monthrange(int(row.year),int(row.month))
        date = pd.to_datetime(str(int(row.year))+'-'+str(int(row.month))+'-'+str(date[1]))
        diff = row.real - row.fiction
        df.loc[df.date==date,'zeta']+=diff

也很好,

我的问题是:

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

  2. 当我从docker运行自托管代理时,如何告诉自托管代理在其工作目录之外包含目录?因此管道不会因错误而失败:

    #[错误]未处理:找不到SourceFolder:/ home / copy_dir

更新 我将请求更新为:

"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)'

解决方法

在“运行” rest API中发送另一个包含SourceFolder路径的参数 我们可以在管道中使用runtime parameters

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)'

请求网址:

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}"
        }
}

我如何告诉自托管代理在其工作目录之外包含目录?

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

更新1

我们应该在YAML构建中定义参数,否则,我们将收到错误的意外参数'Folderpath'“

enter image description here

更新2

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

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

其中/ azp是docker工作目录

我通过此链接配置了docker:
https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/docker?view=azure-devops