1
votes

I'm new to azure pipelines. I have build job with azure pipeline and source code are in same branch dev and running fine but is it possible to have azure pipeline and source code in separate branches?

If so, kindly help me on this

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

2

2 Answers

0
votes

Thank you quick reply on this Checkout code from different repo worked fine But regarding the parameterizing the branch name I used the following but didn't work

Example

- name: branch
  displayName: Branch Name
  type: string
  default: dev
  values:
   - dev
   - test

resources:
  repositories:
  - repository: project
    type: git
    name: project
    ref: ${{ parameters.branch }}
0
votes

No, this is not necessary. When you define a pipeline for already existing file you can select a branch:

enter image description here

You can even put a pipeline definition in a different repo and benefit from multiple repo pipeline to achieve this goal.

If you want to parameterized pipeline you should take a look at 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

And then you can use it in this way:

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

You can also use template froma different repo. Assuming you have common.yml template in Contoso/BuildTemplates repo:

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

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

EDIT:

And in terms of this question:

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

This is possible but not using built-in feature for getting repositories. What you need is use for instance powershell task and this command:

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

Please also put in your YAML also checkout: none as we don't want to get source code by standard pipeline task.

In above command you must put PAT token. More about this you will find here