Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
edo2313 opened this issue Jul 29, 2018 · 21 comments
Open

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

edo2313 opened this issue Jul 29, 2018 · 21 comments

Comments

@edo2313
Copy link

edo2313 commented Jul 29, 2018

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?

@vatsh95
Copy link

vatsh95 commented Jul 30, 2018

Same here

@vicenteguerra
Copy link
Owner

¿Do you have more info about this?

@vatsh95
Copy link

vatsh95 commented Jul 31, 2018

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:YOUR_GIT_PASSWORD@bitbucket.org/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

@3pepe3
Copy link

3pepe3 commented Aug 20, 2018

Same here with bitbucket

@ShadowFM
Copy link

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)"

@renehamburger
Copy link

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'.

@aanderse
Copy link

aanderse commented Apr 9, 2019

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

@JacobDB
Copy link
Collaborator

JacobDB commented Apr 12, 2019

@aanderse can you open a new issue for that?

@aanderse
Copy link

@JacobDB certainly: #33

@rocker76
Copy link

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.");
}
}
}

@jordantrizz
Copy link

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
Copy link

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!

@DarrellBrogdon
Copy link

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

@pragmas
Copy link

pragmas commented Feb 25, 2020

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.

@konkhra
Copy link

konkhra commented Mar 31, 2021

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?

@pragmas
Copy link

pragmas commented Mar 31, 2021

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 #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("", "");

@Serversapollo
Copy link

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

@konkhra
Copy link

konkhra commented Apr 5, 2021

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 ?

@Serversapollo
Copy link

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

@nvjacobo
Copy link

Hi,

Same here with gitlab

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

@obhishekbatif
Copy link

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:YOUR_GIT_PASSWORD@bitbucket.org/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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests