Unitech / pm2-deploy

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

Git error «ambiguous argument...unknown revision or path not in the working tree» when deploying #121

Closed jornki closed 7 years ago

jornki commented 7 years ago

Have used the same setup before with success, but this time it refuses to do a deploy properly.

The error log:

--> Deploying to preprod environment
--> on host 10.3.2.16
  ○ deploying origin/preproduction
  ○ executing pre-deploy-local
  ○ hook pre-deploy
  ○ fetching updates
Fetching origin
  ○ resetting HEAD to origin/preproduction
fatal: ambiguous argument 'origin/preproduction': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

  git reset failed

The config for preprod (which fails):

"preprod": {
            "user": "node",
            "host": "10.3.2.16",
            "ref": "origin/preproduction",
            "repo": "git@github.com:[something].git",
            "path": "/opt/www",
            "post-deploy": "npm install && pm2 reload pm2settings.json --env preprod"
        }

Note that I have the same config for the master and the production branch as well (to different servers), master works, preproduction and production fails with the message above.

Any tips?

phra commented 7 years ago

This has been fixed in #120 Waiting for a npm publish

VladimirCores commented 7 years ago

I have the same problem, so what is the status of this?

vmarchaud commented 7 years ago

Should have been fixed @DQvsRA, try update your pm2 version

flandrade commented 7 years ago

I still have the same issue with pm2 @ 2.4.2. What version should we try?

Maxim-Chugaev commented 7 years ago

same issue on 2.4.6

phra commented 7 years ago

@Maxim-Chugaev instead of just random downvoting things, maybe can you provide more details? did you upgrade pm2 before getting error? did you change the branch in the ecosystem without a full redeploy?

raj454raj commented 7 years ago

Same issue with pm2 @ 2.5.0 - any updates ? (When I see the code in the node_modules of pm2, I see the https://github.com/Unitech/pm2-deploy/pull/120 fix already deployed).

phra commented 7 years ago

@raj454raj can you please answer to the questions above?

is the problem present when doing the initial setup from scratch? does it happens after some deploys? did you upgrade pm2 before getting error? did you change the branch in the ecosystem without a full redeploy?

thanks.

taktran commented 7 years ago

I had the same problem, and I found the issue was because when pm2 sets up the git repository, it sets the git fetch config to be restricted only to the first branch that it was set up with. If you change this on subsequent deploys, it gives the git reset failed error above.

I found, on my local machine my .git/config had

[remote "origin"]
    url = git@github.com:repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*

However on the pm2 deployed server in [deploy path]/current/.git/config, the fetch was restricted to the dev branch, which was the first branch it was setup with.

[remote "origin"]
    url = git@github.com:repo.git
    fetch = +refs/heads/dev:refs/remotes/origin/dev

SSH'ing into the server and changing the .git/config to a more permissive fetch = +refs/heads/*:refs/remotes/origin/* fixes the issue.

I'm not sure why it is set to such a restrictive config during setup - maybe security?

AdamFerguson commented 7 years ago

@taktran Thanks, that fixed the issue for me. I know that at least in my case I'm hoping to be able to switch branches on the deployments as I see fit. Would be great if there was an option to setup the git repo on the server with access to all branches like you highlighted.

phra commented 7 years ago

@taktran thanks for the detailed description. i've changed the way to setup the repo in order to retrieve the last version of one branch and so to speed up the cloning phase when working with projects with a big git history.

@AdamFerguson i'm quite sure that in the past it wasn't possible to update the branch after the setup phase, besides the previous full clone.

these are my commits related to this: https://github.com/Unitech/pm2-deploy/commit/832f725be480c608c19f51a56ed51d73a2986758 https://github.com/Unitech/pm2-deploy/commit/603a4b274b6aa698db30c1a1acac4ff991cfc630

you can try to remove the --depth=5 --branch $branch and try to reproduce the issue. if you can confirm to me that without that parameters you can dynamically change the branch i will work on a way to opt-out from this optimization.

AdamFerguson commented 7 years ago

@phra I'm sure you're right, only recently started using pm2 and decided to try using it for deployments instead of capistrano on my current project. So, only ran into the limitation around not changing the branch recently. Just saying, it would be convenient if there was an option to override that behavior for developers that want the ability to switch between branches.

lpaulmp commented 7 years ago

@phra I think is very restrictive put the branch into the git/config, for production environment probably make sense but for testing environments probably no. This verification should take place at pm2-deploy level so this will give major flexibility for testing environments.

phra commented 7 years ago

@lpaulmp which use cases of your workflow are being affected by this restriction? as i said above also in the past changing branch after the setup wasn't working so in case let's open a feature request or an issue for that. my change was just an optimization without altering the actual behavior.

lpaulmp commented 7 years ago

@phra I was in the position that I had to check how my feature outside of my box, since my machine has development components that are different than a real Staging/QA server which suppose to be similar to production, for this cases I had to tweak git/config fetch = +refs/heads/*:refs/remotes/origin/*, allow deploy from different branch ecosystem.config.js

test: {
      user: "user",
      host: "test.myapp.com",
      ref: process.env.BRANCH || "origin/master",
      repo: "git@github.com:myrepo/myproject.git",
      path: "/path/test",
      "post-deploy": "yarn install && npm run post-deploy && pm2 gracefulReload ecosystem.config.js"
    }

And then deploy my branch BRANCH=origin/mybranch pm2 deploy ecosystem.config.js test

ravivit9 commented 7 years ago

@taktran I have the same configuration as you suggested but I am still getting the error when I try to do npm install a specific tagged version from a remote repo.

$ git config --list --show-origin file:.git/config core.repositoryformatversion=0 file:.git/config core.filemode=false file:.git/config core.bare=false file:.git/config core.logallrefupdates=true file:.git/config core.symlinks=false file:.git/config core.ignorecase=true file:.git/config remote.origin.url=ssh://git@repo.domain.co.uk:9999/proj/project.git file:.git/config remote.origin.fetch=+refs/heads/:refs/remotes/origin/ file:.git/config branch.master.remote=origin file:.git/config branch.master.merge=refs/heads/master file:.git/config branch.dev.remote=origin file:.git/config branch.dev.merge=refs/heads/dev

When I do npm install i get he following error: npm ERR! git rev-list -n1 v0.1.2: fatal: ambiguous argument 'v0.1.2': unknown revision or path not in the working tree. npm ERR! git rev-list -n1 v0.1.2: Use '--' to separate paths from revisions, like this: npm ERR! git rev-list -n1 v0.1.2: 'git [...] -- [...]'

Appreciate any help on this.

lpaulmp commented 7 years ago

@ravivit9 can you paste your .git/conf this should prertty similar like this https://github.com/Unitech/pm2-deploy/issues/121#issuecomment-320237746

ravivit9 commented 7 years ago

Would you mind to point me from which path i can get the .git/conf in Windows 10?

lpaulmp commented 7 years ago

@ravivit9 fetch = +refs/heads/*:refs/remotes/origin/*

phra commented 7 years ago

i will work to make this optimization an opt-in with a "fast": true property in the ecosystem json file. if after this is not possible yet to change dynamically branch we have to work on a dedicated feature request for that.

EDIT: the property is called "fetch": "fast".

ravivit9 commented 7 years ago

@lpaulmp please find .git/config entries of my local repo

[core] repositoryformatversion = 0 filemode = false bare = false logallrefupdates = true symlinks = false ignorecase = true [remote "origin"] url = ssh://git@doamin.co.uk:9999/proj_dir/proj.git fetch = +refs/heads/:refs/remotes/origin/ [branch "master"] remote = origin merge = refs/heads/master [branch "dev"] remote = origin merge = refs/heads/dev

I haven't edited the above config before and I can see the fetch property has the entry as suggested.

phra commented 7 years ago

cheers! :beers: @vmarchaud

https://github.com/Unitech/pm2-deploy/pull/146

ravivit9 commented 7 years ago

I am not sure whether fast=true will make my npm install works. As I tried and got the same error.

phra commented 7 years ago

if you have to opt out you can use the default ecosystem.. only when setting "fetch": "fast" the optimization will be enabled. if you still can't change dynamically the branch please open a dedicated issue because it's not related to my optimization. thanks.

ravivit9 commented 7 years ago

ok sure. I will wait for @taktran or @lpaulmp response as this ticket 121 tilte is the exact problem I am facing.

phra commented 7 years ago

also to have this PR shipped to npm we have to wait for a merge and a bump of the main pm2 repository. @Unitech

vmarchaud commented 7 years ago

Shipped under 0.3.8

phra commented 7 years ago

@vmarchaud thanks for the merge! if you want you can add me to the pm2 team, I would like to contribute more to the project! :100:

vmarchaud commented 7 years ago

@phra Could you pm me on slack.pm2.io ? So we can discuss about it :)

phra commented 7 years ago

sure!

lpaulmp commented 7 years ago

@vmarchaud @phra thanks for adding this feature.

lpaulmp commented 7 years ago

@ravivit9 Your change should be in the fetch you have to replace the branch_name by *: fetch = +refs/heads/*:refs/remotes/origin/*

And your config should be like this.

[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "origin"]
url = ssh://git@doamin.co.uk:9999/proj_dir/proj.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[branch "dev"]
remote = origin
merge = refs/heads/dev

But this new feature is in place you can try it out.

ravivit9 commented 7 years ago

After updating the above suggestion I did "npm install" but I got the same error message. npm ERR! git rev-list -n1 v0.1.0: fatal: ambiguous argument 'v0.1.0': unknown revision or path not in the working tree. npm ERR! git rev-list -n1 v0.1.0: Use '--' to separate paths from revisions, like this: npm ERR! git rev-list -n1 v0.1.0: 'git [...] -- [...]'

phra commented 7 years ago

@ravivit9 can you try to delete your node_modules directory and redo the install? the semver is still compatible so you have to upgrade it manually i think.

ravivit9 commented 7 years ago

@phra, I git cloned my remote repository to a new temp directory then did npm install, but the same error again. Please note I am not using anything related pm2-deploy, it's a normal npm install which installs some of our own remote repository as a separate git repo which is specified in package.json.

But the .git/config is same as specified above

lpaulmp commented 7 years ago

@ravivit9 Can you try git pull orgit checkout myPRToMerge... if this works in your remote hosts? then your issue could be in your dependencies in your package.json also could you point out your git version?

ravivit9 commented 7 years ago

Hi @lpaulmp, thanks for your comment. I am not sure about your suggestion. All I am trying to get installed is the following from one of our remote git repo for which I have access too.

One of the entry in package.json which is failing to install.

    "remote-module": "git+ssh://git@repo.domain.co.uk:9999/proj-name/remote-module.git#0.2.0"

The above command fails. Via stash I can see the above module clone or download but can't do the following: npm install git+ssh://git@repo.domain.co.uk:9999/proj-name/remote-module.git#0.2.0

lpaulmp commented 7 years ago

@ravivit9 Looks your issue is not related with what this thread is about. but did you tried to change your branch in your remote host? Did your remote host has access to your private repo?