【问题标题】:Inline PythonScript Azure Pipelines task in external file外部文件中的内联 PythonScript Azure Pipelines 任务
【发布时间】:2021-03-16 21:52:52
【问题描述】:

我正在开发一个 Azure 任务模板,我有一个大的 .py 文件,我想一步一步执行

  - task: PythonScript@0
    displayName: 'Run a Python script'
    inputs:
      scriptSource: inline
      script: |
        ... really long python code

是否可以将代码存储在另一个文件中,与yml 模板处于同一级别,然后从那里使用它?或者保持模板干净的最佳方法是什么?

我知道可以使用scriptSource

  - task: PythonScript@0
    displayName: 'Run a Python script'
    inputs:
      scriptSource: 'filePath'
      scriptPath: 'my_python.py'
      arguments: '${{ parameters.my_param }}'

但由于模板位于另一个存储库中,而不是在管道中运行的存储库,我认为如果不使用 wget 下载它、克隆或执行其他步骤,我无法到达 my_python.py。我说的对吗?

问候!

【问题讨论】:

  • 嗨@Del。这张票有更新吗?如果 Krzysztof Madej 的回答可以解决这个问题,您可以考虑接受它作为回答。这将对其他用户有所帮助

标签: azure-devops azure-pipelines


【解决方案1】:

要使用来自另一个 repo 的模板,您需要定义 repository source,如下所示:

# Repo: Contoso/LinuxProduct
# File: azure-pipelines.yml
resources:
  repositories:
    - repository: templates
      type: github
      name: Contoso/BuildTemplates

steps:
- template: common.yml@templates  # Template reference

一旦你有了,你只需要签出这个 repo:

# Repo: Contoso/LinuxProduct
# File: azure-pipelines.yml
resources:
  repositories:
    - repository: templates
      type: github
      name: Contoso/BuildTemplates

steps:
- checkout: self
- checkout: templates #this download whole repo
- template: common.yml@templates  # Template reference

现在你需要弄清楚它在哪里 downloaded ;)

多个存储库:如果您的作业中有多个签出步骤,您的源代码将签出到以存储库命名的目录中,作为(Agent.BuildDirectory) 中 s 的子文件夹。如果 (Agent.BuildDirectory) 是 C:\agent\_work\1 并且您的存储库被命名为工具和代码,那么您的代码将签出到 C:\agent\_work\1\s\toolsC:\agent\_work\1\s\code

因此,如果您在templates repo 中的scripts 文件夹中有脚本,您将在$(Agent.BuildDirectory)\templates\scripts\script.py 中找到它。

那么你可以像这样使用它:

  - task: PythonScript@0
    displayName: 'Run a Python script'
    inputs:
      scriptSource: 'filePath'
      scriptPath: '$(Agent.BuildDirectory)\templates\scripts\script.py'
      arguments: '${{ parameters.my_param }}'

【讨论】:

  • OP 要求他的模板引用位于模板存储库中的 python 脚本......您最底层的代码 sn-p 建议您放入客户端管道的代码,因为您提到了模板你只在客户端管道中定义......但是OP希望避免客户端管道看到底层的python脚本...... OP的目标是将所有与python的交易隐藏在模板中......请更新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
  • 1970-01-01
  • 2020-02-23
  • 1970-01-01
相关资源
最近更新 更多