2

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@example.com"
      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)?

ctwheels
  • 21,901
  • 9
  • 42
  • 77

1 Answers1

5

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@example.com"
   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.

Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
  • Thank you! This worked perfectly. For anyone getting a `GenericContribute` error, see [this question](https://stackoverflow.com/questions/56541458/azure-pipeline-doest-allow-to-git-push-throwing-genericcontribute-permission). If you still can't get it to work, copy the uid `Build/` part in the error and paste it into the user field on the security tab as the answer in the other question suggests + set appropriate permissions as the answer mentions. – ctwheels Jun 04 '21 at 21:55