Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically push tag to git after npm version command?

Tags:

shell

npm

npm version:

Run this in a package directory to bump the version and write the new data back to package.json [..] If run in a git repo, it will also create a version commit and tag.

How do I configure npm/ wrap npm version command to automatically push tag to git?, i.e. an equivalent of:

npm version patch
+ [email protected]
git push origin v3.0.1

NPM documentation recommends adding a postversion script to the prackage.json, e.g.

"scripts": {
    "postversion": "git push && git push --tags && rm -rf build/temp"
}

However, this suggestion applies to a single package only and it is bad because it syncs all the tags, not just the last created tag.

like image 328
Gajus Avatar asked Jan 26 '16 14:01

Gajus


People also ask

Does npm version create a git tag?

If run in a git repo, it will also create a version commit and tag. This behavior is controlled by git-tag-version (see below), and can be disabled on the command line by running npm --no-git-tag-version version .

Are git tags automatically pushed?

Sharing tags is similar to pushing branches. By default, git push will not push tags. Tags have to be explicitly passed to git push . To push multiple tags simultaneously pass the --tags option to git push command.

How do I push a git tag?

Sharing Tags You will have to explicitly push tags to a shared server after you have created them. This process is just like sharing remote branches — you can run git push origin <tagname> . If you have a lot of tags that you want to push up at once, you can also use the --tags option to the git push command.

What does npm version patch do?

Purpose. This NPM command allows easy incrementing in both package files and git tags, with a given tag or increment level. Run this in a package directory to bump the version and write the new data back to package.


1 Answers

Assuming the new tag is the only tag on the current revision something like this might work.

$ npm version patch
$ tag=$(git tag --points-at HEAD)
$ git push origin "$tag"

Otherwise you could try catching (and parsing) the output from npm version patch like this perhaps (assuming the output is always + [email protected] and the tag is always v<part after @).

$ tag=$(npm version patch 2>&1)
$ tag=v${tag#*@}
$ git push origin "$tag"

You could also try grabbing the available tags before the npm version patch call and then diffing that list against the available tags after which should find the new tag and you can push it.

like image 103
Etan Reisner Avatar answered Oct 31 '22 11:10

Etan Reisner