Skip to content

Azure DevOps: Incorrect GitRepository branch value while pipline has multi repoΒ #1587

@patelriki13

Description

@patelriki13

Usage Information

latest

Description

I have two repositories in Azure DevOps pipeline: infra and app.
The Azure DevOps pipeline is defined in the infra repository, while the application source code is in the app repository. I trigger the pipeline manually.

[GitRepository]
readonly GitRepository GitRepository;

When I access following prop value that return incorrect value means it's return infra repo values instead of app repo values.

GitRepository.Commit
GitRepository.Branch

However GitRepository.HttpsUrl or GitRepository.Head is return correct values

Reproduction Steps

NukeBuild code:

class Build : NukeBuild
{
    [GitRepository]
    readonly GitRepository GitRepository;

    Target Print => _ => _
        .Executes(() =>
        {
            Log.Information("NukeBuild RootDirectory = {Value}", NukeBuild.RootDirectory);
            Log.Information("Commit = {Value}", GitRepository.Commit);
            Log.Information("Branch = {Value}", GitRepository.Branch);

            Log.Information("Https URL = {Value}", GitRepository.HttpsUrl);
            Log.Information("SSH URL = {Value}", GitRepository.SshUrl);

            Log.Information("LocalDirectory = {Value}", GitRepository.LocalDirectory);
            Log.Information("Head = {Value}", GitRepository.Head);
        });
}

Azure DevOps pipeline at infra repo:
declare repo:

resources:
  repositories:
    - repository: app
      type: git
      name: {Project }/{Repo}      

I checkout app repo specific path in azure devops pipeline
e.g.

 - checkout: app
   displayName: 'Checkout source code'
   path: repo/app
   fetchDepth: '1'
   submodules: 'false'

last run build.cmd with required param

- script: |
          build.cmd BuildArtifacts --configuration $(buildConfiguration) --version $(Build.BuildNumber) --RootDirectory "$(appRootDirectory)"
        displayName: 'Build Solution using Nuke Build'
        workingDirectory: '$(appRootDirectory)'

Note: I already provided --RootDirectory "$(appRootDirectory)" param so when GitRepository instance created then it will use this directory.

Expected Behavior

GitRepository.Commit and GitRepository.Branch values return correct values from app repo instead of infra repo

Actual Behavior

Only the GitRepository.Commit and GitRepository.Branch values are incorrect. Other properties, such as GitRepository.HttpsUrl and GitRepository.Head, are returning the correct values.

Regression?

I found the cause AzurePipelines : Host, IBuildServer use azure devops env. values which contains value of infra repo.

string IBuildServer.Branch => SourceBranch;
string IBuildServer.Commit => SourceVersion;

public string SourceBranch => EnvironmentInfo.GetVariable("BUILD_SOURCEBRANCH");

But if the GitRepository instance is created from a local directory, it should use the local folder branch and last commit values instead of the azure devops pipeline repo values.

public class GitRepositoryAttribute : ValueInjectionAttributeBase
{
public override object GetValue(MemberInfo member, object instance)
{
return GitRepository.FromLocalDirectory(Build.RootDirectory);
}
}

Known Workarounds

No response

Could you help with a pull-request?

No

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions