lerna / lerna/lerna

Lerna publish workflow not working as expected

Open
#3,025 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: bug
Dominant language
TypeScript
Stars
36.1k
Forks
2.3k
Avg merge
1d 4h
Merged PRs (30d)
50

Description

For smoother CI experience, I have made a Github Action workflow to publish the monorepo packages with a prerelease version, everytime any member opens a PR against master with a particular label 'publish'. This workflow should ideally publish all the changed packages since the last publish with a preid -pr{pr#} e.g., package-pr1049.0. Have added dist-tag and predist-tag also here.

Background: Before publishing the packages, I also run a MAKE executable(make -j init script) to clean and bootstrap all the packages. Post this, it will fetch the repo, checkout to the required branch, and run the publish command with the PR number parameter.

There are 2 problems I am facing in this workflow:

Publishes all the packages in the first commit of the PR: To debug the issue , Had also added a logger to see if it has the correct record for last 10 commits, which reflects the correct set of commits.
Second commit onwards, only the changed packages are published which is as expected. refer the log
lerna notice cli v3.20.2
lerna info versioning independent
lerna info ci enabled
lerna info Assuming all packages changed
lerna info getChangelogConfig Successfully resolved preset "conventional-changelog-angular"

Changes:

  • @swiggy-private/package-1: 1.0.4 => 1.1.0-pr19000.0
  • @swiggy-private/package-2: 1.1.4 => 1.2.0-pr19000.0
  • @swiggy-private/package-3: 2.41.2 => 2.42.0-pr19000.0 (private)
    Always updates the patch version as 0, and increments the preid (PR number with -pr prefix) PACKAGEv0-pr{##}.0, PACKAGEv0-pr{##}.1, ....
    For speeding up debugging process, have limited monorepo packages in lerna.json to only 3 of the packages.

My GH workflow

name: Branch Publish

on:
    pull_request:
        types: [opened, synchronize, reopened, labeled]
        branches:
            - master

jobs:
    check:
        runs-on: ubuntu-latest
        timeout-minutes: 15

        outputs:
            author: ${{ steps.step1.outputs.author }}
        steps:
            - uses: actions/checkout@v2
              with:
                  ref: ${{ github.event.pull_request.head.sha }}
            - id: "step1"
              run: |
                  AUTHOR_NAME=$(git show ${{ github.event.pull_request.head.sha }} | grep Author)
                  echo "::set-output name=author::$AUTHOR_NAME"

    init:
        if: "!contains(needs.check.outputs.author, 'GitHub Action Branch') && !contains(github.event.head_commit.message, '[skip ci]')"
        runs-on: ubuntu-latest
        timeout-minutes: 15

        needs: [check]
        steps:
            - uses: actions/checkout@v2
            - uses: actions/setup-node@v1
              with:
                  node-version: "12.x"
            - run: git fetch --prune --unshallow
            - run: |
                  make -j init
              env:
                  NPM_TOKEN: ${{ secrets.GH_TOKEN }}
            - uses: actions/cache@v1
              id: cache-build
              with:
                  path: "."
                  key: ${{ github.sha }}

    release:
        if: "contains(github.event.pull_request.labels.*.name, 'publish')"
        runs-on: ubuntu-latest
        timeout-minutes: 15
        needs: [init]

        steps:
            - uses: actions/checkout@v2
              with:
                  fetch-depth: "0"
            - uses: actions/setup-node@v1
              with:
                  node-version: "12.x"
            - uses: actions/cache@v1
              id: restore-build
              with:
                  path: "."
                  key: ${{ github.sha }}

            - name: Setup Git
              uses: webfactory/ssh-agent@v0.4.1
              with:
                  ssh-private-key: ${{ secrets.GHA_DEPLOY_KEY }}

            - name: Lerna Publish
              if: success()
              env:
                  GH_TOKEN: ${{ secrets.GH_TOKEN }}
                  NODE_ENV: production
              run: |
                  git config user.email "action@github.com"
                  git config user.name "GitHub Action Branch"
                  git remote set-url origin "git@github.com:${{ github.repository }}"
                  git fetch --depth=1 origin +refs/tags/*:refs/tags/*
                  git checkout -- .
                  git log  --pretty=oneline -n 10 
                  git checkout --track origin/$(echo $GITHUB_HEAD_REF | cut -d'/' -f 3)
                  NUMBER=${{ github.event.number }} npm run publish-branch

            - name: Possible Package lock update
              if: success()
              run: |
                  git config user.email "action@github.com"
                  git config user.name "GitHub Action Branch"
                  git remote set-url origin "git@github.com:${{ github.repository }}"
                  npx lerna clean -y
                  npx lerna exec -- npm i --package-lock-only --ignore-scripts --no-audit
                  echo `git add . && git commit -m "chore: package lock update" --no-verify && git push`

Publish command

"publish-branch": "lerna publish --conventional-prerelease --exact --no-changelog --preid pr$NUMBER --dist-tag beta --pre-dist-tag beta --no-verify-access --yes"

Lerna.json

{
    "packages": ["*"],
    "version": "independent",
    "command": {
        "publish": {
            "npmClient": "npm",
            "graphType": "all",
            "allowBranch": ["master", "integration", "*"],
            "conventionalCommits": true,
            "message": "chore(release): publish",
            "includeMergedTags": true,
            "ignoreChanges": ["**/__tests__/**", "**/*.md"]
        }
    }
}

Make Script to bootstrap packages

init: clean-all
    $(MAKE) create-npmrc-all
    npm ci 
    npm run bootstrap:ci
    NODE_ENV=production npm run prepare:all

create-npmrc-all:
    echo $(GITHUB_SCOPE_REGISTRY) >> .npmrc
    echo $(GITHUB_REGISTRY_TOKEN) >> .npmrc
    $(foreach source, $(DIRECTORY), $(call pass-to-npmrc, $(source), $(GITHUB_SCOPE_REGISTRY)))
    $(foreach source, $(DIRECTORY), $(call pass-to-npmrc, $(source), $(GITHUB_REGISTRY_TOKEN)))

clean-all:
    rm -rf node_modules

    $(foreach source, $(SOURCES), \
        $(call clean-source-all, $(source)))

    rm -rf .npmrc
    rm -rf packages/*/.npmrc
    rm -rf coverage
    rm -rf packages/*/npm-debug*

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the GitHub Actions workflow, the publish-branch command, lerna.json, and the Make init script, then compare the logged commit history with Lerna's publish behavior. Reproduce the first-commit and prerelease-version cases using the three-package configuration. Done means the workflow publishes only changed packages and generates the intended prerelease versions consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
github-actions, javascript, node.js, typescript
Domain
build-system, ci-cd, release
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.