skip to Main Content

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:

enter image description here

But the output tags is like this:

enter image description here

How could I do it?

Thanks

2

Answers


  1. 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.

    steps:
    - script: |
        npmVersion=$(node -p "require('./package.json').version")
        echo "##vso[task.setvariable variable=npmVersion]$npmVersion"
      displayName: 'Set npm version'
    
    - script: |
        echo "dev_$(npmVersion)_$(build.buildNumber)"
      displayName: 'Print source version'
    
    Login or Signup to reply.
  2. I can reproduce the same situation.

    enter image description here

    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.

    How could I do it?

    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:

    enter image description here

    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:

    npmVersion=$(node -p "require('./package.json').version")
    echo "##vso[task.setvariable variable= npmVersion]$npmVersion"
    

    Then you can add the command line task to run the following git commands:

    git config --global user.email "[email protected]"
    git config --global user.name "Your Name"
    echo $(npmVersion)
    git tag "dev_$(npmVersion)_$(build.buildNumber)" -m "tag description"
    git push origin --tags
    

    For example:

    enter image description here

    If you are using YAML Pipeline, you can add the argument: persistCredentials: true in checkout step.

    For example:

    - checkout: self
      persistCredentials: true
    

    Then you can use the following YAML sample to set the git tag:

    steps:
    - checkout: self
      persistCredentials: true
    
    - script: |
       npmVersion=$(node -p "require('./package.json').version")
       echo "##vso[task.setvariable variable=npmVersion]$npmVersion"
      displayName: 'Set variable'
    
    - script: |
       git config --global user.email "[email protected]"
       git config --global user.name "Your Name"
       echo $(npmVersion)
       git tag "dev_$(npmVersion)_$(build.buildNumber)" -m "tag description"
       git push origin --tags
      displayName: 'Git command Set git tag'
    

    Result:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search