1
votes

We have all our code in Azure devops and most of the pipelines are creating Artifacts (Nuget Packages). These Nuget Packages often depend on each other.

What I do now: I change some code in 'A' which I commit and Azure creates an artifact. I then goes to 'B' and update the NuGet package version that I just created.

This I would love to automate!

But how?

Maybe, at 'A' Pipelines (YAML) after the nuget packages is created, somehow go into 'B' Repo and change its packages.config and create new commit. But I have no idea where to start.

1
Hi Did you get a chance to check out below answer, how did it go?Levi Lu-MSFT

1 Answers

1
votes

You create an additional pipeline C for B repo to update the package version. And you can add a task to trigger this pipeline from A Pipeline and pass the package version as its variable. See below steps:

1, Create a pipeline C for B repo to update the package version.

Click the Variables button to define a Variable version to the hold the version value. (Note: Check this option Let users override this value when running this pipeline, so that it can be overrode from A pipeline )

enter image description here

enter image description here

Add tasks(MagicChunks/RegEx Find & Replace) to update the package version in Package.config file. (Note: install the task to your organization from the Marketplace if it wasnot installed before). Configure the task to update the version. You can check out this thread for more information.

Note: If the B repo's .csproj have below reference to A repo, see below. You need to update the package version in the .csproj file for repo B too.

<Reference Include="***8, Version=1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>..\packages\***.1.2.0\lib\net45\....dll</HintPath>
    </Reference>

Add a script task to run the git commands to commit the changes and push back to B repo. See below example pipeline C yaml:

trigger: none
pool:
  vmImage: windows-latest
  
steps:
- checkout: self
  persistCredentials: true
- task: MagicChunks@2
  inputs:
    sourcePath: '$(system.defaultworkingdirectory)/package.config'
    fileType: 'Auto'
    targetPathType: 'source'
    transformationType: 'json'
    transformations: |
      {
        "package/version": "$(version)" #just an example
      }

- powershell: |
    git config --global user.email "[email protected]"
    git config --global user.name "Your Name"

    git add .
    git commit -m "update package.json" #add [skip ci] to the commit message to keep B pipeline from being triggered
    git push origin Head:$(Build.SourceBranchName)
 

2, Trigger Pipeline C from A pipeline after nuget packages is created,

Add task Trigger Build at the end of A pipeline. Then configure the task to configure C pipeline. And pass the version variable to C pipeline

- task: benjhuser.tfs-extensions-build-tasks.trigger-build-task.TriggerBuild@3
  displayName: 'Trigger a new build of 48'
  inputs:
    buildDefinition: {ID of C pipeline}
    buildParameters: 'version: 1.0.3'  
    password: '$(System.AccessToken)'

Instead of using above Trigger Build task, you can also trigger C pipeline via Rest api in a script task. see this example.

Additional:

You might want to parameterize the version value in above buildParameters: 'version: 1.0.3' like buildParameters: 'version: $(newversion)'. In this case you need to use a scripts to get the new version value and set this variable in a script task. See here for more information.

You can also use the rest api to get the latest artifact version of A repo directly in C pipeline. In this way you will not need to pass the buildParameters in A pipeline. See here for more information.