I’m trying to get the npm version from package.json and use it to tag my git repo, but seems it’s not working because the variable seems to be "preloaded" before it is set.
I’ve created a commandline task that just do this:
npmVersion=$(node -p "require('./package.json').version")
echo '##vso[task.setvariable variable= npmVersion]$npmVersion'
Then, in the Get sources, I’ve set my repo and branch and tag format like this:
But the output tags is like this:
How could I do it?
Thanks
2
Answers
Make sure your commandline task comes after your get sources step.
You can also try an inline script step to see verify it is persisted across commands.
I can reproduce the same situation.
The cause of the issue is that the step: Label sources to set the git tag will execute before the command line task to set the variable.
In this case, the custom variable is not able to pass to the set tag step successfully.
To meet your requirement, you need to add an additional command line task to run git command to set tag and push to repo.
Refer to the following steps:
Step1: Navigate to Project Settings -> Repositories -> Target Repo -> Security and set Contribute permission for the Build Service Accounts: ProjectName Build Service(OrganizationName) and Project Collection Build Service(Organization)
For example:
Step2: If you are using Classic Pipeline, you can enable the option: Allow scripts to access the OAuth token in Agent Job -> Additional options.
You can set the variable with the following commands:
Then you can add the command line task to run the following git commands:
For example:
If you are using YAML Pipeline, you can add the argument: persistCredentials: true in checkout step.
For example:
Then you can use the following YAML sample to set the git tag:
Result: