Azure管道: is it mandatory to have azure pipelines and code in same branch

标签 azure azure-devops azure-pipelines

我是 azure 管道的新手。我有使用 azure 管道的构建作业,源代码位于同一分支开发中并且运行良好,但是是否可以在单独的分支中拥有 azure 管道和源代码?

如果是这样,请帮助我

此外,我如何在 azure 管道中实现分支参数化作业?

最佳答案

不,这没有必要。当您为现有文件定义管道时,您可以选择一个分支:

enter image description here

您甚至可以将管道定义放在不同的存储库中并从 multiple repo pipeline 中受益来实现这个目标。

如果你想参数化管道,你应该看看 templates :

# File: templates/npm-with-params.yml

parameters:
- name: name  # defaults for any parameters that aren't specified
  default: ''
- name: vmImage
  default: ''

jobs:
- job: ${{ parameters.name }}
  pool: 
    vmImage: ${{ parameters.vmImage }}
  steps:
  - script: npm install
  - script: npm test

然后你可以这样使用它:

# File: azure-pipelines.yml

jobs:
- template: templates/npm-with-params.yml  # Template reference
  parameters:
    name: Linux
    vmImage: 'ubuntu-16.04'

- template: templates/npm-with-params.yml  # Template reference
  parameters:
    name: macOS
    vmImage: 'macOS-10.14'

- template: templates/npm-with-params.yml  # Template reference
  parameters:
    name: Windows
    vmImage: 'vs2017-win2016'

您还可以使用来自不同存储库的模板。假设您在 Contoso/BuildTemplates 存储库中有 common.yml 模板:

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

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

编辑:

就这个问题而言:

Additionally how can i achieve branch parameterized job in azure pipelines?

这是可能的,但不使用内置功能来获取存储库。您需要使用例如 powershell 任务和此命令:

GIT clone -b <branch>  https://<PAT>@dev.azure.com/Organization/My%20Project/_git/MyRepo

请同时输入您的 YAML checkout: none 因为我们不想通过 standard pipeline task 获取源代码.

在上面的命令中,您必须输入 PAT token 。有关此的更多信息,您会发现 here

关于Azure管道: is it mandatory to have azure pipelines and code in same branch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63369113/

相关文章:

.net-core - VSTS 版本 : Packages failed to restore - Unable to resolve 'NETStandard.Library (>= 1.6.1)' for '.NETStandard,Version=v2.0'

azure-devops - 适用于 Azure DevOps 的带有 azure-pipelines.yml 的 SpecFlow 示例

azure - 如何运行已经部署到azure应用程序服务容器的?

azure - Azure 上的多个网站/角色,临时服务器的影响

amazon-web-services - 如何监听本地链接地址 169.254.169.254

android - 尝试在 Android 上的 UnityWebRequest 上使用 PATCH 并获取 'Unsupported Protocol'

azure-devops - git clone 在 Azure DevOps 构建任务中挂起

Azure管道托管代理,指定镜像版本

Azure DevOps Pipeline - 推送到 Azure 容器注册表 - 权限错误

azure - 使用哪些 Azure 服务来通知某些内容已完成?