Search code examples
npmazure-devopssemantic-versioning

How to automatically version npm package in Azure DevOps (without triggering new pipeline)?


What we're trying to do

We are using Azure Pipelines (azure-pipelines.yml) to automate ci/cd. Part of our configuration completes the versioning of our project for publishing to Azure Artifacts. We're also trying to configure this to update the existing version number in package.json without triggering a new pipeline in Azure DevOps.

This is the relevant section for our azure-pipelines.yml file:

  - script: |
      git config --global user.email "[email protected]"
      git config --global user.name "User name"
      npm version patch -m "Bump version to %s [skip ci]" --force
    displayName: 'Bump release version'
  - script: |
      npm pack
    displayName: 'Package package'

This works well to publish the package to our Azure Artifacts feed, but does not update the existing version in package.json

Our package.json contains the following:

"release": {
    "plugins": [
        "@semantic-release/commit-analyzer",
        "@semantic-release/release-notes-generator",
        "@semantic-release/changelog",
        [
            "@semantic-release/npm",
            {
                "npmPublish": false
            }
        ],
        [
            "@semantic-release/git",
            {
                "assets": [
                    "dist/",
                    "package.json",
                    "CHANGELOG.md"
                ],
                "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
            }
        ]
    ]
}

Question

How would we update the script to ensure the version value in package.json is also updated without triggering another pipeline execution (which would result in an endless loop of triggering new pipeline executions)?


Solution

  • You can add another script task to push back to your devops git repo. But firstly, you need to add checkout step and set the persistCredentials to true to authenticate the git command. See below example:

    - checkout: self
      persistCredentials: true
    
    - script: |
    
       git config --global user.email "[email protected]"
       git config --global user.name "User name"
       npm version patch -m "Bump version to %s [skip ci]" --force
    
      displayName: 'Bump release version'
    
    - script: |
    
       git push origin HEAD:$(Build.SourceBranchName)
    
      displayName: 'Update Git Repo'
      
    

    Since you have [skip ci] in the commit message. Push back the updated package.json file will not trigger a new build. See here.