Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull latest git tag before assuming that the package.json is correct #37

Open
cecilia-sanare opened this issue Apr 13, 2017 · 4 comments

Comments

@cecilia-sanare
Copy link

cecilia-sanare commented Apr 13, 2017

Description

semantic-release pulls the latest npm version before assuming there isn't a published version.

Meanwhile, corp-semantic-release takes the version in the package.json at face-value.

Workaround

package.json

{
  // ...
  "scripts": {
    "semantic-release": "node scripts/semantic-release-pre.js && corp-semantic-release --pre-commit build-code && npm publish"
    // ...
  }
  // ...
}

scripts/semantic-release-pre.js

const fs = require('fs');
const nodePackage = require('../package.json');
const semanticRelease = require('corp-semantic-release/src/lib');

const latestTag = semanticRelease.getLatestTag(false);

let [_dummy, version] = latestTag.match(/v?(\d+\.\d+\.\d+)/);

nodePackage.version = version;

fs.writeFileSync('package.json', JSON.stringify(nodePackage, null, '  '));

Resources

@leonardoanalista
Copy link
Owner

Hi Nick,

Do you set the version to empty when getLatestVersion recognises is the first time?

I just wonder what happens when corp-sem-release tries to bump the version and value in package.json is empty.

Could you explain the consequences if you don't run your semantic-release-pre.js?

By the way the de facto version value is retrieved from the local Git repo tags. However, I remember now the bump is done via npm version minor|major command. I am not sure if this last command will know what to do when version is empty.

For first time ever, corp-sem-release, bumpUpVersion.js will literally run npm version --no-git-tag-version ' v1.0.0. That's why I think for first run the tool should work it all out for us.

@cecilia-sanare
Copy link
Author

So I actually have it so that the built code is deployed to NPM and the Latest branch (instead of master).

This means that the version isn't located on the master branch.

Instead the version on the master branch is 0.0.0-semantically-released.0.

When corp-semantic-release is run it bumps the version located in the master branches package.json. To prevent the version from being completely off, I run the pre script to force it to grab the latest semver version from git and overwrite the package.json version.

I don't recall if corp-semantic-release blows up or if it bumps the version incorrectly when it reads 0.0.0-semantically-released.0.

@leonardoanalista
Copy link
Owner

Ok just for clarity, if I run npm version --no-git-tag-version major this is what happens depending on the contents of the version field:

  • "version": "1.0.0", ==> result is ==> "2.0.0" as expected
  • "version": "0.0.0-semantically-released.0.", ==> result is ==> Error
  • "version": "0.0.0-semantically-released.0", ==> result is ==> "0.0.0" on file package.json (without the trailing dot!)
  • "version": "", ==> result is ==> Error

@nick-woodward Do you release from a branch or master?

We have been using for months and we always release from master. It has been working for new and old projects. Maybe there is a genuine use case that corp-semantic-release needs to support in order to guarantee consistent version. If this is the case of your script semantic-release-pre.js, we can consider incorporating to the tool.

Looking at the code those days, I have small concerns regarding to the latest version being retrieved from git tags but the bump being trusted to the command npm version bumpType. I can see cases where the version can get out-of-sync if you run it in branch other than master.

One way I can see if always use the git tag as source of true. If so, corp-sem-release should always run npm version with a version number, never a bump type. It seems like your script is doing what I am describing here but you run off master.

cheers
Leonardo

@cecilia-sanare
Copy link
Author

cecilia-sanare commented Apr 18, 2017

Our source code is located in master, we end up releasing from a latest branch though that way we don't pollute master with build artifacts.

This is currently how we're handling our releases: (the above example was slimmed down for simplicity)

#!/bin/sh
git checkout latest
git reset --hard origin/latest
git merge -X theirs origin/master
npm run semantic-release
git push origin latest --tags

package.json

{
  // ...
  "scripts": {
    // ...
    "semantic-release:pre": "npm run build && git add .",
    "semantic-release": "node scripts/semantic-release-pre.js && corp-semantic-release --pre-commit semantic-release:pre -r 0 -b latest --changelogpreset angular-bitbucket && npm publish",
    // ..
  },
  // ...

@cecilia-sanare cecilia-sanare changed the title Pull latest git tag before assuming that the package.json is correctl Pull latest git tag before assuming that the package.json is correct Apr 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants