USING RUNTIME VARIABLES IN AZURE YAML CHECKOUT

Azure devops pipeline
I am trying to checkout 2 repositories. The one which I’m running from is fine. The second repository. I retrieve a branch name from job 1. I then pass this branch name into the checkout of the second repository using inline commands and environmental variables. But it does not work.

Trying to dynamically change a branch during a YAML for checking out a repo. the below is an extract of the code, there are more defined variables which i have removed

code:

stages:

- stage: build
  jobs:
  - job: config
    steps:
    - checkout: self
      clean: true
      submodules: recursive
      persistCredentials: true
    - task: CmdLine@1
      inputs:
        filename: Call
        arguments: (here calling a script which sets PRJ_BRANCH, removed for Export Control)
    - task: PublishBuildArtifacts@1
      inputs:
        PathtoPublish: '$(OUTPUTDIR)'
        ArtifactName: Reports
      condition: succeededOrFailed()
    
- stage: projectconfig
  dependsOn:
  - build
  variables:
    prjref: $[ stageDependencies.build.config.outputs['CmdLine.PRJ_BRANCH'] ]
  jobs:
  - job: test
    steps:
    - script: echo $(prjref)
    - checkout: self
      # clean: true
      # submodules: recursive
      # persistCredentials: true
    - checkout: repo@$(prjref)

6

Based on your comments, you are using Github Repo as the repo resource. In this case, you need to set the Repo Resource to reference the Github repo.

I am afraid that the Repo Resource doesn’t support using the variables to define the branch in checkout step(e.g. - checkout: repo@$(prjref)).

We need to define the define ref settings in Repo resource to define the checkout branch.

For example:

resources:
  repositories:
  - repository: MyGitHubRepo
    type: github
    endpoint: MyGitHubServiceConnection
    name: MyGitHubOrgOrUser/MyGitHubRepo
    ref: ${{ variables['Build.SourceBranch'] }}

steps:
- checkout: MyGitHubRepo

The ref field in Repo resource only supports compile time variable (e.g. ${{ variables['Build.SourceBranch'] }}).

Refer to this doc: resources.repositories.repository definition

ref name to checkout; defaults to ‘refs/heads/main’. The branch checked out by default whenever the resource trigger fires. Template expressions are supported.

Currently, Azure DevOps only supports using runtime variables in Inline syntax checkout. But only Azure Repos Git repositories in the same organization can use the inline syntax.

For more detailed info, you can refer to this doc: Check out multiple repositories in your pipeline

Update:

For a workaround, we can spilt the single pipeline into two pipelines to run the two stages. One is used to set the variable and pass the value to parameters in Pipeline two. The other Pipeline use the parameters collect the branch name and use it in the ref field.

Here is an example:

Pipeline One

stages:

- stage: build
  jobs:
  - job: config
    steps:
    - checkout: self
      clean: true
      submodules: recursive
      persistCredentials: true
    - task: CmdLine@1
      inputs:
        filename: Call
        arguments: (here calling a script which sets PRJ_BRANCH, removed for Export Control)
    - task: PublishBuildArtifacts@1
      inputs:
        PathtoPublish: '$(OUTPUTDIR)'
        ArtifactName: Reports
      condition: succeededOrFailed()
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
              $token = "$(pat)"      
              $url="https://dev.azure.com/{Orgname}/{Projectname}/_apis/pipelines/{PipelineRunID}/runs?api-version=5.1-preview"
              
              $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
              
              $JSON = "
              {
                
              
              
                `"resources`": {
                  `"repositories`": {
                    `"self`": {
                      `"ref`": `"refs/heads/main`"
                    }
                  }
                },
                `"templateParameters`": {
                  `"Outputref`":`"$(PRJ_BRANCH)`"
                },
              
              
              
              }"
              
              
              $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json  

Pipeline Two:

parameters:
  - name: Outputref
    type: string
resources:
  repositories:
  - repository: repo
    type: github
    endpoint: MyGitHubServiceConnection
    name: MyGitHubOrgOrUser/MyGitHubRepo
    ref: ${{ parameters.Outputref }}


stages:

- stage: projectconfig
  jobs:
  - job: test
    steps:
    - script: echo ${{ parameters.Outputref }}
    - checkout: self
      # clean: true
      # submodules: recursive
      # persistCredentials: true
    - checkout: repo  

In this case, the variable value: PRJ_BRANCH will be passed from Pipeline one to Pipeline two -> parameters. And it can be used in the ref field of the repo resources.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật