Unitech / pm2-deploy

Deploy part of PM2
http://pm2.keymetrics.io/docs/usage/deployment/
MIT License
178 stars 71 forks source link

Deploy different branch on demand #154

Open bassuff opened 6 years ago

bassuff commented 6 years ago

First of all thanks to the PM2 developers. It's a nice tool for managing Node.js processes.

Ok now back to my issue.

I want to set up a continuous integration and deployment workflow with PM2. As integration- / building tool we are using Jenkins. Furthermore we have several (testing) environments:

My goal is to build and deploy our application on each env not only restricted to a fixed branch e.g. origin/develop. I want to choose the preferred branch on Jenkins. So as a developer i want to test my feature branch (which is different for each new feature) on a real host deployed via Jenkins. But until now i didn't find any solution. Maybe it's not even possible with pm2 deploy.

I prepared an ecosystem.json file with all the required data. "repo": "blabla.git", "ref:" "origin/develop". After that i expected that i can overwrite the ref as an arguement when i execute pm2 deploy ecosystem.json dev ref origin/feature/cool-feature as mentioned on http://pm2.keymetrics.io/docs/usage/deployment/#deployment-options.

No matter what i try (omitting the ref prop inside ecosystem file) the remote git branch on the target host is never gonna checked out to my desired branch "origin/feature/cool-feature".

I want to use the same process for all our environments (from local to production).

So my first question Is it even possible to deploy both static and dynamic Git branches with PM2?

Second question If not what else could i do? Any ideas / workarounds?

In my opinion deploying different branches on demand is a common task. Is that really not possible with PM2 or am i doing wrong something? Or is it not designed for that?

harish0507 commented 6 years ago

+1

tapanand commented 6 years ago

use fetch property in deploy object:

deploy: { dev: { user: 'xxxx', host: 'xxxx', ref: 'xxxxx/xx/xx', branch: 'development', repo: 'git@github.com:xxx/xxxx.git', path: '/xx/xx/x', fetch: '--all', env: { NODE_ENV: 'development', PORT: 1234 } } }

Maples7 commented 6 years ago

@tapanand How does it works? No explanation found in the document of PM2 about this field.

ldarren commented 6 years ago

@tapanand fetch doesn't work this way, the valid fetch option is fast or undefined

@Maples7 you can try my PR https://github.com/Unitech/pm2-deploy/pull/162 or my fork https://github.com/ldarren/pm2-deploy/tree/live

to test it, try this: it deploys tag tags/prof-v1.0.1 to branch named v1.0.1 (optional, if omitted the tag name will be used)

echo '{"user":"SSH_USER","host":"REMOTE_ADDR","port":"SSH_PORT","ref":"origin/develop","repo":"git@github.com:ORG_NAME/REPO_NAME.git","path":"REMOTE_PATH","post-deploy":"export  && npm install && pm2 reload ecosystem.config.js --env staging"}' | "/opt/ops/repos/pm2-deploy/deploy" ref tags/prod-v1.0.1 v1.0.1

make sure

you can also deploy a branch, to do that replace tags/prod-v1.0.1 by your branch name, e.g. master

tapanand commented 6 years ago

@Maples7 I went through the source code and found this as an option.

cc: @ldarren

jmfarina commented 6 years ago

My two cents: I've managed to specify the branch dynamically by setting an env variable and referring to it from within the ecosystem.config.js with process.env.theBranchVar. For instance, I would specify the ecosystem.config.js as follows:

module.exports = {
  apps : [{
  //...
  }],

  deploy : {
    myEnv : {
      user : 'myUser',
      host : 'my.host.com',
      ref  : process.env.DEPLOY_BRANCH,
      repo : 'git@my.repo.com:some/project.git',
      path : '/remote/destination/path',
      'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env myEnv'
    }
  }

Set the environment variable: export DEPLOY_BRANCH="origin/master"

Finally, deploy as usual: pm2 deploy ecosystem.config.js myEnv

Bonus: to trigger the deploy from Jenkins, you can simply set your variable to the value of $GIT_BRANCH variable defined by Jenkins (I attempted to refer directly to GIT_BRANCH variable in ecosystem.config.js, but for some reason, such variable wouldn't be set in process.env): export DEPLOY_BRANCH=$GIT_BRANCH && pm2 deploy ecosystem.config.js stg

vipinjn24 commented 4 years ago

@jmfarina Thank you for the solution. :)