tomab
tomab

Reputation: 2151

Azure DevOps Pipelines: how to check out branch of the self repo?

I have a yaml pipeline which I want to make it to run for more branches. Therefore I am trying to checkout one branch, specified in pipeline variables. I'm unable to do so, the error being Unexpected value 'ref'.

The pipeline file is:

trigger:
  batch: true
  branches:
    include:
      - 'release/dev'
      - 'release/test'
      - 'release/prod'
  paths:
    include:
    - '*'

resources:
- repo: self
  ref: $(branch)

variables:
  dockerRegistryServiceConnection: 'conn'
  containerRegistry: 'conn.reg'
  tag: '$(Build.BuildId)'
  vmImageName: 'ubuntu-latest'

stages:
- stage: Build
  displayName: Build and push stage
  jobs:  
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - template: docker-build-template.yml
      parameters:
        dockerfilePath: $(Build.SourcesDirectory)/Dockerfile.release
        imageName: conn.reg/container
        imageRepository: container
        pushToRegistry: true
    

How can I checkout different branches for building the container out of them?

Later edit: I want the pipeline to automatically run after a PR or a commit is pushed on any of the braches. (Manually it can be run with specifiyng a branch.)

Upvotes: 7

Views: 42797

Answers (2)

Leo Liu
Leo Liu

Reputation: 76760

Azure DevOps Pipelines: how to check out branch of the self repo?

You could specify the name of the self repo in the resource with a specific ref,like:

resources:
  repositories:
  - repository: MyTestProject 
    type: git
    name: MyTestProject
    ref: $(branch)

Then checkout with another path:

steps:
- checkout: MyTestProject
  path: Another path/xxxx/xxx

Upvotes: 6

YoavKlein
YoavKlein

Reputation: 2703

You don't define the self repo as a resource. If you want to run your build on another branch, just choose your branch in the "Run pipeline" screen:

enter image description here

As for running automatically after completing a PR, you already have the triggers.branches.include set, so merges (or pushes) to all these branches will trigger a build in which the relevant branch will be checked out.

Upvotes: 0

Related Questions