Lerna monorepo publish docker images instead of npm packages

Viewed 311

I'm currently moving my backend from polyrepo or multirepo to a monorepo. I've successfully moved the shared packages into the Lerna monorepo, now it's time for microservices. My project structure is simple as that:

root
  services
    service_written_in_node
       Dockerfile
       package.json
    service_Written_in_something_else
       Dockerfile
  packages
     shared_package_1
         package.json
     shared_package_2
         package.json
    

So basically everything in the directory packages should be published to the npm, and everything in services dir should be published to the Docker registry.

1 Answers

You can use functionality of package.json and add a postinstall script there.

ex.:

"scripts": {
    "postinstall": "npm run docker-build-publish",
    "docker-build-publish: "docker build .... {ADD HERE SCRIPTS TO PUSHLISH}
}

Have this in each of your services that has a Dockerfile.

At root of repo you can use lerna bootstrap as your postinstall to trigger install on each service.

You can always check other pre and post scripts that can fit better you usecase: https://docs.npmjs.com/cli/v8/using-npm/scripts

Related