git-deploy icon indicating copy to clipboard operation
git-deploy copied to clipboard

=== ERROR: Pushed branch does not match BRANCH ===

Open edo2313 opened this issue 6 years ago • 21 comments

I can't understand why it is giving me the error... I successfully pulled manually using git, but the script gives me this error everytime i run it. Any help?

edo2313 avatar Jul 29 '18 12:07 edo2313

Same here

vatsh95 avatar Jul 30 '18 06:07 vatsh95

¿Do you have more info about this?

vicenteguerra avatar Jul 30 '18 23:07 vicenteguerra

Hi Edo/Vincenteguerra

I've found the error in the script and solved it, the error happens at line 60 of deploy.php as we're unable to reference the branch properly.

As it looks like JSON Structure from bitbucket/git has changed and cannot read response properly from branch.

Try replacing the whole IF section with following code:

foreach ($json["push"]["changes"] as $change) {
	if (
		(
			isset($change["old"]["type"])
			&& isset($change["old"]["name"])
			&& $change["old"]["type"] == "branch"
			&& $change["old"]["name"] == "master"
		)
		|| (
			isset($change["new"]["type"])
			&& isset($change["new"]["name"])
			&& $change["new"]["type"] == "branch"
			&& $change["new"]["name"] == "master"
		)
	) {
		exec("git pull https://YOUR_GIT_USERNAME:[email protected]/YOUR_GIT_USER/GIT_NAME.git YOUR_BRANCH_NAME");
		die("Complete.");
	}
}

you can structure your code as per vicenteguerra script as well but i've reduced my script code as i'm just going to pull from specific branch when webhook is called

vatsh95 avatar Jul 31 '18 01:07 vatsh95

Same here with bitbucket

3pepe3 avatar Aug 20 '18 13:08 3pepe3

Hi @vatsh95 can you send your complete deploy.php?

I have the error"Unexpected 'else' (74, 3)" and "syntax error, unexpected 'else' (T_ELSE) (74, 9)"

ShadowFM avatar Aug 26 '18 12:08 ShadowFM

As all changes should relate to the same branch I would think, maybe a check like $json['push']['changes'][0]['new']['name'] === BRANCH is sufficient for bitbucket, as long as BRANCH is set to the raw branch name like 'master'.

renehamburger avatar Oct 05 '18 21:10 renehamburger

@vicenteguerra do you have any plans to support merge request events? With gitlab the property to compare is "target_branch" instead of "ref".

aanderse avatar Apr 09 '19 18:04 aanderse

@aanderse can you open a new issue for that?

JacobDB avatar Apr 12 '19 16:04 JacobDB

@JacobDB certainly: https://github.com/vicenteguerra/git-deploy/issues/33

aanderse avatar Apr 12 '19 16:04 aanderse

I am using Bitbucket, and I still get an error. Only time is recorded in the log file. 17-04-2019 (03:38:59)

Which part is the problem? :(

##deployer.php // Check for a GitHub signature if (!empty(TOKEN) && isset($_SERVER["HTTP_X_HUB_SIGNATURE"]) && $token !== hash_hmac($algo, $content, TOKEN)) { forbid($file, "X-Hub-Signature does not match TOKEN"); // Check for a GitLab token } elseif (!empty(TOKEN) && isset($_SERVER["HTTP_X_GITLAB_TOKEN"]) && $token !== TOKEN) { forbid($file, "X-GitLab-Token does not match TOKEN"); // Check for a $_GET token } elseif (!empty(TOKEN) && isset($_GET["token"]) && $token !== TOKEN) { forbid($file, "$_GET["token"] does not match TOKEN"); // if none of the above match, but a token exists, exit } elseif (!empty(TOKEN) && !isset($_SERVER["HTTP_X_HUB_SIGNATURE"]) && !isset($_SERVER["HTTP_X_GITLAB_TOKEN"]) && !isset($_GET["token"])) { forbid($file, "No token detected"); } else { // check if pushed branch matches branch specified in config foreach ($json["push"]["changes"] as $change) { if ( ( isset($change["old"]["type"]) && isset($change["old"]["name"]) && $change["old"]["type"] == "branch" && $change["old"]["name"] == "master" ) || ( isset($change["new"]["type"]) && isset($change["new"]["name"]) && $change["new"]["type"] == "branch" && $change["new"]["name"] == "master" ) ) { exec("git pull origin master"); die("Complete."); } } }

rocker76 avatar Apr 17 '19 03:04 rocker76

I just tried to set this up for a GitHub repository and it's failing. Printing out the array $json returns nothing. Am I missing anything?

jordantrizz avatar Apr 17 '19 22:04 jordantrizz

I just tried to set this up for a GitHub repository and it's failing. Printing out the array $json returns nothing. Am I missing anything?

This is because I didn't RTFM. Forgot to change the Content Type to json on the Webhooks screen in the repository. Shwoopsie!

jordantrizz avatar Apr 18 '19 17:04 jordantrizz

Any status updates on this issue? It's still broken for Bitbucket.

DarrellBrogdon avatar Dec 10 '19 03:12 DarrellBrogdon

Add this line in deployer.php on line 10:

if (isset($json['push']['changes'][0]['new']['name'])&&empty($json['ref'])) $json['ref']=$json['push']['changes'][0]['new']['name'];

That worked for me.

pragmas avatar Feb 25 '20 10:02 pragmas

Add this line in deployer.php on line 10:

if (isset($json['push']['changes'][0]['new']['name'])&&empty($json['ref'])) $json['ref']=$json['push']['changes'][0]['new']['name'];

That worked for me.

Could you be more specific with this? On line 10 in deployer.php I see this:

// retrieve the token

What version do you have?

konkhra avatar Mar 31 '21 07:03 konkhra

Add this line in deployer.php on line 10: if (isset($json['push']['changes'][0]['new']['name'])&&empty($json['ref'])) $json['ref']=$json['push']['changes'][0]['new']['name']; That worked for me.

Could you be more specific with this? On line 10 in deployer.php I see this:

// retrieve the token

What version do you have?

I added it before the comment:

$DIR = preg_match("//$/", DIR) ? DIR : DIR . "/";

// bitbucket ref issue https://github.com/vicenteguerra/git-deploy/issues/23 if (isset($json['push']['changes'][0]['new']['name'])&&empty($json['ref'])) $json['ref']=$json['push']['changes'][0]['new']['name'];

// retrieve the token if (!$token && isset($_SERVER["HTTP_X_HUB_SIGNATURE"])) { list($algo, $token) = explode("=", $_SERVER["HTTP_X_HUB_SIGNATURE"], 2) + array("", "");

pragmas avatar Mar 31 '21 12:03 pragmas

hey , i was getting the same error , and after a few hours i was trying different changes in the code to figure out what was the problem (fun fact it was me) I realized that i was having the Content type from the github webhook settings set to application/x-www-form-urlencoded. So the "fix" for me was to set the Content type to application/json

Serversapollo avatar Apr 05 '21 11:04 Serversapollo

hey , i was getting the same error , and after a few hours i was trying different changes in the code to figure out what was the problem (fun fact it was me) I realized that i was having the Content type from the github webhook settings set to application/x-www-form-urlencoded. So the "fix" for me was to set the Content type to application/json

Are you using bitbucket or github ?

konkhra avatar Apr 05 '21 12:04 konkhra

hey , i was getting the same error , and after a few hours i was trying different changes in the code to figure out what was the problem (fun fact it was me) I realized that i was having the Content type from the github webhook settings set to application/x-www-form-urlencoded. So the "fix" for me was to set the Content type to application/json

Are you using bitbucket or github ?

Github

Serversapollo avatar Apr 05 '21 13:04 Serversapollo

Hi,

Same here with gitlab

=== ERROR: Pushed branch does not match BRANCH ===

nvjacobo avatar May 22 '21 06:05 nvjacobo

Hi Edo/Vincenteguerra

I've found the error in the script and solved it, the error happens at line 60 of deploy.php as we're unable to reference the branch properly.

As it looks like JSON Structure from bitbucket/git has changed and cannot read response properly from branch.

Try replacing the whole IF section with following code:

foreach ($json["push"]["changes"] as $change) {
	if (
		(
			isset($change["old"]["type"])
			&& isset($change["old"]["name"])
			&& $change["old"]["type"] == "branch"
			&& $change["old"]["name"] == "master"
		)
		|| (
			isset($change["new"]["type"])
			&& isset($change["new"]["name"])
			&& $change["new"]["type"] == "branch"
			&& $change["new"]["name"] == "master"
		)
	) {
		exec("git pull https://YOUR_GIT_USERNAME:[email protected]/YOUR_GIT_USER/GIT_NAME.git YOUR_BRANCH_NAME");
		die("Complete.");
	}
}

you can structure your code as per vicenteguerra script as well but i've reduced my script code as i'm just going to pull from specific branch when webhook is called

Are you talking about deployer.php?

obhishekbatif avatar Jun 13 '21 07:06 obhishekbatif