How to Commit files from edited version of pom.xml file into azure repos

77 Views Asked by At

I am trying to implement versioning strategy of our apps on pom.xml via CI/CD.

I have followed the solution to this question and applied powershell script task on Azure DevOps pipeline: How to update version in POM file on branch after build number is incremented in Azure Pipeline

I can now see the incremented pom version on the working directory but I am having difficulty in commit and push this changes back to azure repo.

Do I need to run the git commands as well? Do you have example in mind? Or do you have any alternative approach in this objective?

Thank you

1

There are 1 best solutions below

1
Bright Ran-MSFT On

Generally, in the source repository, it is recommended keeping the value of <version> as a placeholder in the pom.xml file. Similar to other properties if you want to update their values every time when building a new package version.

So, it is not necessary to commit the updated pom.xml file back to the source repository.

Since the values of properties in the pom.xml file also support environment variables, so you also can set the placeholders like as below. The expression '${env.VAR_NAME}' is referencing the value of the specified environment variable. More details, see here.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  . . .
  <version>${env.PACKAGE_VERSION}</version>
  . . .
</project>

In the pipeline, you just need to ensure the actual new values have been mapped to environment variables with the same names (i.e., PACKAGE_VERSION) referenced by the placeholders.

By default, the pipeline variables (except secret variables) will be automatically map as environment variables. So, you just need to use the values to define the pipeline variables with the same names.

When building the package in pipeline, it will automatically use the actual values of environment variables to replace the placeholders in the pom.xml file.


If you want to use the run (build) number to set the package version, we have the predefined variable for it. You do not need to define extra pipeline variable. And in the pom.xml file, you just need to set it like as below.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  . . .
  <version>${env.BUILD_BUILDNUMBER}</version>
  . . .
</project>

If you still want to push the updated pom.xml file back to Azure Repos, yes, you need to run the related git commands to do this.

git config --global user.name xxxx
git config --global user.email xxxx
git add relative/path/to/pom.xml
git commit -m "Update version number in pom.xml"
git push