Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript node app deployment process with pm2

Using pm2 I would like to deploy a node app written in typescript.
The repo of this app sitting on git, the remote machine got an ssh connection to git.

What is the standard work flow for deployment in this case ?

on the git repo of course there's only the source (uncompiled) code, and ideally only the compiled (no .ts files) will be sitting on the server.

On what stage the compilation should take place ? and where ?

If some of you encounter this scenario and got an actual example, a concrete configuration for this, it would be very very helpful

Note:
Using heroku, I just add a "postinstall": "npm run build" script in package.json and had a Procfile with somting like web: npm start,
for some reason in this case things are more complicated.

additionally the deployment is to Ec2 linux instance, with standard environment (node, git, pm2 global, and all typescript dependencies if needed, etc.)

like image 489
Ofir G Avatar asked Nov 07 '22 21:11

Ofir G


1 Answers

Ideally you use a build-server like travis-CI or gitlab-CI that builds, tests and deploys your source.
I asked a similar question recently and ended up building the following pipeline:

  • build-stage:
    • npm install to install node_modules *
    • compile typescript to dist folder
  • test-stage:
    • linting (although some recommend this before building)
    • other code inspection
    • unit testing
  • deploy: (only on master branch)
    • npm prune --production to remove dev-dependencies from node_modules
    • use scp to copy dist and node_modules to prod server
    • use ssh to remove files from earlier deployments and to tell pm2 to reload server.js

* note that if you use modules that use native code and thus are compiled on npm_install (node-gyp), you have to make sure your build-server uses the same architecture as your production environment

like image 80
N4ppeL Avatar answered Nov 14 '22 22:11

N4ppeL