Skip to content

publish to latest using CI  #461

Closed
Closed

Description

@bramthedev

Hello,
as far as I understand it is impossible to publish to npm as the latest version using a CI script.
I'm currently using this script, which is run on the server:

lerna publish -c --yes --repo-version grep -Po '(?<="version":\s")[^"]*' package.json

this takes the version of package.json. But npm automatically sets it default to a previous version, even when the version is incremented by 0.0.1

npm install 'package' does not return the one that was succesfully pushed, even though it exists in npm when asking for that specific version.

I'm sorry if this is not the correct place to discuss this issue, I've never opened an issue on github before, this is my second month employed in IT :)

Activity

  1. gigabo commented on Dec 22, 2016

    @gigabo
    Contributor

    Yeah, it's not really straightforward to automatically publish a new non-canary version right now.

    Something like what np does might be nice:

        Version can be:
          patch | minor | major | prepatch | preminor | premajor | prerelease | 1.2.3
    

    It looks like the grep you're using to produce your --repo-version is actually returning the current version? It may be that you need to increment the patch level on that to get what you're looking for. Also the -c (--canary) option you're including prohibits Lerna from tagging your newly published version as "latest", so npm won't provide it by default.

  2. gigabo commented on Dec 22, 2016

    @gigabo
    Contributor

    I've never opened an issue on github before, this is my second month employed in IT :)

    Welcome! 😀

  3. kristianmandrup commented on Apr 1, 2017

    @kristianmandrup

    Would be nice with a short paragraph/recipe on a typical setup for CI/CD. I guess it would just be publish without pushing to npm, triggering the CI from the git repo? or is there a smarter way?

  4. Rowno commented on Dec 22, 2017

    @Rowno

    I got CI publishing working on CircleCI using lerna publish --skip-npm, this script:

    #!/usr/bin/env node
    'use strict'
    const path = require('path')
    const fs = require('fs')
    const execa = require('execa')
    
    const TAG_REGEX = /[^@]+@[0-9]+(\.[0-9]+)*/
    
    const tag = process.argv[2]
    
    if (typeof tag !== 'string') {
      console.error('Tag required')
      process.exit(1)
    }
    
    if (!TAG_REGEX.test(tag)) {
      console.error('Invalid tag')
      process.exit(1)
    }
    
    const [packageName, packageVersion] = tag.split('@')
    
    const packagePath = path.join(__dirname, '..', 'packages', packageName)
    const packageJsonPath = path.join(packagePath, 'package.json')
    
    if (!fs.existsSync(packageJsonPath)) {
      console.error('Package doesn՚t exist')
      process.exit(1)
    }
    
    const pkg = require(packageJsonPath)
    
    if (pkg.version !== packageVersion) {
      console.error('Version mismatch')
      process.exit(1)
    }
    
    execa('npm', ['publish'], {
      cwd: packagePath,
      stdio: 'inherit'
    })
      .then(() => {
        process.exitCode = 0
      })
      .catch(() => {
        process.exitCode = 1
      })

    And this CircleCI config:

    deployment:
      publish:
        tag: /[^@]+@[0-9]+(\.[0-9]+)*/
        commands:
          - ./tools/publish-tag.js $CIRCLE_TAG
  5. kopax commented on Apr 9, 2018

    @kopax

    Hi, we would like to publish to npm in our ci. We need to do it non interactive.

    Usually, we just bump the next version of package.json on dev branch when we tag from master. This way we just need to keep or update the version in package.json before merging to master to tag a specific version.

    Is it possible with Lerna ?

  6. evocateur commented on Apr 9, 2018

    @evocateur
    Member

    @kopax No, not at present.

  7. kopax commented on Apr 9, 2018

    @kopax

    @evocateur do you have another purpostion on how we should adapt our publishing process to integrate Lerna in our stack? Why won't this be implemented that way?

  8. evocateur commented on Apr 9, 2018

    @evocateur
    Member

    If you're comfortable with allowing CI to make commits and push tags, I would restrict non-canary publishing to master and adopt conventional commits (available via the --conventional-commits lerna flag).

    The process for automated publishing in CI is not ideal right now, so I'd recommend against it in most cases. I hope to improve this in the future.

  9. kopax commented on Apr 9, 2018

    @kopax

    Thanks @evocateur for conventional commits link. I will consider it in the futur to automate our changelog.

    So far, the only solution in the CI is to run my own script and iterate in each package to tag ?

  10. evocateur commented on Apr 9, 2018

    @evocateur
    Member

    Not the only solution, but almost all of the currently-available solutions using only lerna are painful in one way or another.

    semantic-release-monorepo is another approach.

  11. kopax commented on Apr 9, 2018

    @kopax

    @evocateur painful in what way ?
    I feel like the simple script would just iterate for packages/**/package.json and run npm version patch then git tag. Do you see any other complication ?

  12. evocateur commented on Apr 9, 2018

    @evocateur
    Member

    Well, in that case, I guess why bother with lerna, then? There's lots of logic related to choosing "what" to publish. lerna publish --canary would get you most of the way there, but with no record of the changes, and a version number that is really awkward.

  13. kopax commented on Apr 9, 2018

    @kopax

    Well, in that case, I guess why bother with lerna, then?

    I've been playing with lerna and it is an amazing tool, that is I think a good reason to use it.

    To use it in our stack, we need to be able to release and tag during our CI process and that's a requirement.

    but with no record of the changes, and a version number that is really awkward.

    I don't get that point. We are doing all our change in dev branch, the CHANGELOG.md is updated manually and controlled before merging to our master.

    The person who is opening the merge request to master is in charge of setting the proper version in package.json. The person who validate the commit is in charge of confirming the version.

    With this in place, we have been able to manage many projects and dependencies without worrying about the release part. We'd like to keep it that way.

    Now we have tried lerna, we like to be able to host multiple packages on a single git repository which will increase even more our productivity.

    Do you think another option to fit that need could be requested?

    If lerna can let us play that way, I'll be happy to help (writing the feature request) and programming (if assisted).

  14. evocateur commented on Apr 9, 2018

    @evocateur
    Member

    Lerna doesn't really support the separation of versioning and publishing very gracefully right now.

  15. revelt commented on May 30, 2019

    @revelt
    Contributor

    hi guys, I did manage to set up Lerna monorepo on Gitlab's CI to publish to npm. When git push is done, GitLab CI triggers and lerna bootstrap + lerna version builds the latest sources (using Conventional Commits) including changelogs and bumped package.jsons. From there on, I don't use Lerna any more for the rest of publishing. This allows me to granularly control tag pushing, committing and pushing to git source.

    Main idea is, npm won't publish packages which don't have a new, bumped package.json version. It's not very efficient to manually npm publish each of the packages but it's safe and it works.

    CI logs in as me into git and npm (using login details as secrets put in GitLab preferences), final bits are commited and published to npm, then git pushed to git origin. After it's done, I git pull the latest from CI onto my laptop.

    Here's the GitLab CI yml:

    #
    #    C O D S E N
    #
    
    deploy:
      except:
        - tags
      image: node:latest
      stage: deploy
      script:
        - git config --global credential.helper store
        - git remote set-url origin https://gitlab-ci-token:[email protected]/codsen/codsen.git
        - git config --global user.email "$GITLAB_USER_EMAIL"
        - git config --global user.name "$YOUR_NAME_SURNAME"
        - npm set unsafe-perm true -g
        - npm set //registry.npmjs.org/:_authToken $NPM_TOKEN -g
        - npm set username $NPM_USERNAME -g
        - npm set email $NPM_EMAIL -g
        - echo "git user:"
        - git config user.name
        - echo "npm user:"
        - npm whoami
        - echo '█████████████████████████████ 1 █████████████████████████████'
        - git status
        - git checkout master
        - echo '█████████████████████████████ 2 █████████████████████████████'
        - git status
        - npm run bootstrap
        - echo '█████████████████████████████ 3 █████████████████████████████'
        - git status
        - npm run all:test:only
        - echo '█████████████████████████████ 4 █████████████████████████████'
        - git status
        - npm run readme:generate
        - echo '█████████████████████████████ 5 █████████████████████████████'
        - git status
        - git add packages
        - "git commit -m '[skip ci] chore: Automated build tasks' --no-verify"
        - npx lerna changed
        - npm run pub:vers
        - echo '█████████████████████████████ 6 █████████████████████████████'
        - git status
        - git add packages
        - echo '█████████████████████████████ 7 █████████████████████████████'
        - git status
        - "git commit -m '[skip ci] chore: Automated build tasks' --no-verify"
        - echo '█████████████████████████████ 8 █████████████████████████████'
        - git status
        - npm run republish
        - echo '█████████████████████████████ 9 █████████████████████████████'
        - git status
        - git push origin master
        - echo '█████████████████████████████ 10 █████████████████████████████ - done'
    

    $GITLAB_USER_EMAIL and all other variables that start with dollar are secrets you set in GitLab repo preferences.

    npm script pub:vers looks like this:

    "pub:vers": "lerna version --conventional-commits --no-commit-hooks --yes && lernacleanchangelog '**'",
    

    lernacleanchangelog is lerna-clean-changelogs-cli call. As you know, Conventional Commits puts "Note: Version bump only for package ranges-apply" if there were no feat/fix commits and CLI removes those changelog entries.

    make republish is a shell script which loops through all package directories in packages/ and does npm publish.

    It took me pretty much all the Christmas holidays of 2018 to get this setup above. Whoever is interested to see the repo, it's here: https://gitlab.com/codsen/codsen — 84 npm packages.

  16. JamesHenry commented on Nov 29, 2022

    @JamesHenry
    Member

    Hi Folks 👋

    Publishing using lerna in CI has been well supported for some time now and so I am going to close this older issue. If you have any requests that could enhance your usage of Lerna 6, then please do let us know by opening a new discussion (for a feature) or issue (for a bug report) with as much context as possible.

    You can also check out our published roadmap for Lerna v7 here: #3410

    Many thanks 🙏

  17. locked and limited conversation to collaborators on Nov 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions