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