Search code examples
gitazure-devopsazure-pipelines-yaml

devops multiple yaml pipelines that push changes to the same branch


We have multiple yaml pipelines that use the same yaml to push a NuGet and update the .csproj file with the new version number. This works fine, until you run multiple pipelines that update a different NuGet each. In that case the local repository is out of sync.

I tried a lot of different things like fetching, puling etc. But nothing seems to work. Any input wold be appreciated.

The yaml looks like this.

trigger: none

pool: 'Self-hosted in Azure vms'

variables:
  - group: 'product Assembly info'
  - name: solution
    value: '**//$(ProjectFile)'
  - name: buildPlatform
    value: 'Any CPU'
  - name: buildConfiguration
    value: 'Release'
  - name: PackageVersion
    value: '$(Major).$(Minor).$(Build)$(Preview)'
  - name: Preview
    ${{if eq(variables['Build.SourceBranchName'], 'master') }}: 
      value: ''
    ${{ else }}:
      value: '-$(Build.SourceBranchName).$(PreviewBuild)'

steps:
- checkout: self
  persistCredentials: true
  fetchDepth: 0

- script: |
    git config --global user.email [email protected] & git config --global user.name "DevOps"  
  workingDirectory: $(Build.SourcesDirectory)

- script: |
   git pull origin HEAD:$(Build.SourceBranchName)
  displayName: pull $(Build.SourceBranchName) 
  workingDirectory: $(Build.SourcesDirectory)

/// logic for creating nuget and updating a single .csproj file (different for every pipeline)

- script: |
   git add *.csproj
   git commit -m "$(ProjectFile) build $(PackageVersion)"
   git push origin HEAD:$(Build.SourceBranchName)
  displayName: commit package buildnumber
  workingDirectory: $(Build.SourcesDirectory)


Solution

  • Below the .yml that does what I expected.

    differences:

    • missing clean: true (thanks @alvin-zhao-msft).
    • pull should not have HEAD: but it is required for push.
    • pull is now at both the beginning of the yml and direct before the push.
    
    trigger: none
    
    pool: 'Self-hosted in Azure vms'
    
    variables:
      - group: 'product Assembly info'
      - name: solution
        value: '**//$(ProjectFile)'
      - name: buildPlatform
        value: 'Any CPU'
      - name: buildConfiguration
        value: 'Release'
      - name: PackageVersion
        value: '$(Major).$(Minor).$(Build)$(Preview)'
      - name: Preview
        ${{if eq(variables['Build.SourceBranchName'], 'master') }}: 
          value: ''
        ${{ else }}:
          value: '-$(Build.SourceBranchName).$(PreviewBuild)'
    
    steps:
    - checkout: self
      persistCredentials: true
      fetchDepth: 0
      clean: true
    
    - script: |
        git config --global user.email [email protected] & git config --global user.name "DevOps"  
      workingDirectory: $(Build.SourcesDirectory)
    
    - script: |
       git pull origin $(Build.SourceBranchName)
      displayName: pull $(Build.SourceBranchName) 
      workingDirectory: $(Build.SourcesDirectory)
    
    /// logic for creating nuget and updating a single .csproj file (different for every pipeline)
    
    - script: |
       git add *.csproj
       git commit -m "$(ProjectFile) build $(PackageVersion)"
       git pull origin $(Build.SourceBranchName)
       git push origin HEAD:$(Build.SourceBranchName)
      displayName: commit package buildnumber
      workingDirectory: $(Build.SourcesDirectory)