DEV Community

Cover image for Automatic versioning in a Lerna monorepo using Github actions

Automatic versioning in a Lerna monorepo using Github actions

Xavier Canchal on October 04, 2021

Prerequisites This is a continuation of my previous article: Monorepo using Lerna, Conventional commits, and Github packages. The prereq...
Collapse
 
benyitzhaki profile image
Ben Yitzhaki

thanks, great post! One comment though, I would look into replacing the custom token with GITHUB_TOKEN to follow best practices security wise. according to their docs "GitHub Packages allows you to push and pull packages through the GITHUB_TOKEN available to a GitHub Actions workflow."

docs.github.com/en/packages/managi...

Collapse
 
xcanchal profile image
Xavier Canchal

Great suggestion Ben. I was doubtful about wether the GITHUB_TOKEN would allow authenticating against package registries and I see it does!

Collapse
 
srd profile image
subhranshu das

What an awesome series, clarified lot of Lerna sticking points for me.
Questions-
1) lets say you keep add 3 merges to development without pulling to main
so how does the versions work out?
a) after 1st merge (fix) in development 2.1.0-beta.0 --> 2.1.0-beta.1 ? or 2.1.1-beta.0?
b) assuming 2.1.1-beta.0, then 2nd merge (fix) in development 2.1.2-beta.0
c) then 3rd merge (fix) in development 2.1.3-beta.0
Now when we merge to main then it becomes 2.1.3? hence directly jumping from
2.1.0 to 2.1.3 in main?
Any good reading resources on semantic relases apart from the dry semVer doc??

2) Do you see if there is a need to changes fetch depth in the YAML - github.com/lerna/lerna/issues/2542

Collapse
 
srd profile image
subhranshu das

How can we add conventional commits for a git commit message which involves ay changes in 3 packages - pkg1, pkg2, pkg3 ?
Scenario
Previously,
pkg1 - 1.0.0
pkg1 - 1.0.0
pkg1 - 1.0.0

git commit -m "
feat(pkg1): added button color
fix(pkg2): fixed typo
fix(pkg3): fixed header
"

Now,
pkg1 - 1.1.0
pkg1 - 1.0.1
pkg1 - 1.0.1

Is that so? Also how to add breaking changes in the multiple package change commit where 1 of the packages have a breaking change, others do not.

Thanks!!!

Collapse
 
xcanchal profile image
Xavier Canchal

Hi! I would commit the changes of each package separately (one commit for each package)

Collapse
 
tutods profile image
Daniel Sousa @TutoDS

I'm facing this issue:

lerna info Looking for changed packages since undefined
lerna ERR! Error: Command failed with exit code 128: git diff --name-only  -- packages/shared
lerna ERR! fatal: bad revision 'undefined'
Enter fullscreen mode Exit fullscreen mode

Any idea?

Collapse
 
srd profile image
subhranshu das

Had a query on the usage of the packages after they are published-
let's say
step1) changes merged to dev --> publishes --> 1.0.1-beta.0
step2) the changes from dev are now merged to main --> publishes --> 1.0.1

Now can an end user install both 1.0.1-beta.0 & 1.0.1 ?

Collapse
 
xcanchal profile image
Xavier Canchal

Yes, they are published and available in the registry unless you delete them.

Collapse
 
sfilinsky profile image
SFilinsky

Thanks for post, great help! Question for me is how do you upgrade project version? After you merge to master, it removes -beta postfix, but how do you upgrade package version from 2.1.0 further to 2.1.0-beta.0? And how can it determine if to change major, minor or patch number?

Collapse
 
xcanchal profile image
Xavier Canchal

Hi SFilisnky. Lerna parses the conventional commit history, which include of the scope change, to determine the major, minor or patch version (see conventional commits specification). For the -beta removal, it’s the other way around. Let’s say you are in 2.1.0 and you merge a patch PR against development. Then, Lerna will bump to 2.1.1-beta.0 and when merging against master it will remove the suffix, leaving the final 2.1.1 version. Does that make sense?

Collapse
 
alex_boulay_036612da9a348 profile image
Alex Boulay • Edited

Thank you for the great post! Got everything working except one thing that is bugging me:

When using --conventional-prerelease, lerna creates the correct changelogs(with the correct commit history)

but when I use --conventional-graduate, lerna only puts this into my changelog:
Note: Version bump only for package

Also this only happens when running the command in github action, outside of it, it's fine!

Do you know why? Thanks!

Collapse
 
richfrost profile image
Rich Frost

Is this still a workable solution? I followed your steps but on the version/publish step I get the error:
EUNCOMMIT Working tree has uncommitted changes, please commit or remove the following changes before continuing:

because the version command has updated the package.json and is therefore not able to carry on.

Collapse
 
tushar199 profile image
Tushar Mistry

Hi Some Edge Cases I wanted to discuss
So we have master and dev branch merging to dev branch npm ci works perfectly and versioning is performed and beta is applied. But now if you try to merge this branch into master your lockfile is outdated npm ci would throw error cant perform installing node_modules.

PS: using pnpm as package manager

Collapse
 
smerth profile image
Stephen Merth

This is a great post, thanks. When I create a new Pull Request from development against master (main), in my case, I end up with merge conflict. The issue is that the development branch has beta versions and the main has major versions. So there is a conflict everywhere the version is defined. This can't be the desired behavior but I can't see a way around it... Maybe I misunderstand something about the workflow? Any Ideas about where I am going wrong?

Collapse
 
danicaliforrnia profile image
Daniel Stefanelli

have you found a better branching approach to solve that problem?

Collapse
 
tutods profile image
Daniel Sousa @TutoDS

Hi.
for me the packages doesn't appear on the repository, and other thing, how can I list the same packages on npmjs.com?