Calculate and apply Git Tag for different Repo in Azure pipelines

76 views Asked by At

Currently we have two repo one for Infra related and one for Source code. when I want to calculate its calculating against Infra branch because that's where its actually running. How can I calculate git version for a source code branch in different repo and apply? I am using Azure pipeline. currently its working for the Infra branch

Calculate tag

     - task: gitversion/setup@0
       displayName: Install GitVersion
       inputs:
        versionSpec: '5.x'

     - task: gitversion/execute@0
       displayName: Determine Version
       inputs:
         useConfigFile: true
         configFilePath: 'global/GitVersion.yml'

Apply Tag

    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: | 
          git config --global user.email "$(Build.RequestedForEmail)"
          git config --global user.name "$(Build.RequestedFor)"
          if [ $(git tag -l "$(GitVersion.SemVer)") ]; then
            echo "Tag $(GitVersion.SemVer) already exists. Skipping Tagging."
          else
            git tag $(GitVersion.SemVer) -m "release build"
            git push origin $(GitVersion.SemVer) --force
          fi

Update*

based on this git https://github.com/GitTools/actions/issues/172 I can see we can set target branch, when I set I am getting warning and it picks dev as default.

Multiple source branches have been found, picking the first one (dev). This may result in incorrect commit counting. Options were: dev, release/1.0.1, releases/1.0.2, releases/1.0.0, main

1

There are 1 answers

2
Alvin Zhao - MSFT On

Based on your expectation and update, you may test with the sample below.

azure-pipelines.yml file in the Infra branch of Infra repo (self/Repo1)

trigger:
- Infra

pool:
  vmImage: ubuntu-latest
resources:
  repositories:
  - repository: Repo2
    name: SourceCode
    type: git

steps:
- checkout: self
  path: s/Repo1
  fetchDepth: 0
  persistCredentials: true

- checkout: Repo2
  path: s/Repo2
  fetchDepth: 0

- task: gitversion/setup@0
  displayName: Install GitVersion
  inputs:
    versionSpec: '5.x'

- task: gitversion/execute@0
  displayName: Determine Version
  inputs:
    targetPath: '$(Agent.BuildDirectory)/s/Repo1'
    useConfigFile: true
    configFilePath: '$(Agent.BuildDirectory)/s/Repo1/global/GitVersion.yml'

- task: Bash@3
  inputs:
    targetType: 'inline'
    script: |
      echo "========== Show file structure in Agent.BuildDirectory =========="
      tree $(Agent.BuildDirectory)
      echo "GitVersion.SemVer is $(GitVersion.SemVer)"
      
      git config --global user.email "$(Build.RequestedForEmail)"
      git config --global user.name "$(Build.RequestedFor)"
      if [ $(git tag -l "$(GitVersion.SemVer)") ]; then
        echo "Tag $(GitVersion.SemVer) already exists. Skipping Tagging."
      else
        git tag $(GitVersion.SemVer) -m "release build"
        git push origin $(GitVersion.SemVer) --force
      fi
    workingDirectory: '$(Agent.BuildDirectory)/s/Repo1/'

This pipeline first checks out the self repo (Infra) into the s/Repo1 folder that is relative to the $(Agent.BuildDirectory); then, similarly, it checks out the Repo2 (SourceCode) into the s/Repo2 folder; see Checkout path;

enter image description here

Since the expectation is to tag Infra branch from Infra repo, the pipeline then executes gitverion with the cofigFile in

$(Agent.BuildDirectory)/s/Repo1/global/GitVersion.yml and with targetPaht of $(Agent.BuildDirectory)/s/Repo1; this path is where the Infra repo is checked out.

Then with the help of git commands, the pipeline tags infra with $(GitVersion.SemVer). enter image description here