Skip to content

Commit

Permalink
Add support for specifying a SHA hash to reset to
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobDB committed May 23, 2019
1 parent 6e4701f commit d11e059
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ Click "Save" to save your settings, and the script should start working.

![Example screenshot showing Bitbucket webhook settings](https://cloud.githubusercontent.com/assets/1123997/25353602/7aee9cde-28f5-11e7-9baa-eb1e1330017e.png)

## Integration with CI/CD

If you'd prefer to integrate git-deploy with your CI scripts rather than using traditional Webhooks, you can trigger the hook via the following `wget` command.

```sh
wget --quiet --output-document=- --content-on-error --header="Content-Type: application/json" --post-data='{"ref":"refs/heads/master"}' 'https://www.example.com/git-deploy/deploy.php?token=secret-token'
```

Additionally, you can add the parameters `sha=COMMIT_HASH` and `reset=true` to the URL in order to instruct git-deploy to reset to a specific commit. **Note that this will overwrite any local changes you may have made.** This can be useful for integration with things like [GitLab's Environments feature](https://gitlab.com/help/ci/environments).

---

I appreciate the collaboration of @JacobDB
58 changes: 57 additions & 1 deletion deployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
$file = fopen(LOGFILE, "a");
$time = time();
$token = false;
$sha = false;
$DIR = preg_match("/\/$/", DIR) ? DIR : DIR . "/";

// retrieve the token
Expand All @@ -15,6 +16,15 @@
$token = $_GET["token"];
}

// retrieve the checkout_sha
if (isset($json["checkout_sha"])) {
$sha = $json["checkout_sha"];
} elseif (isset($_SERVER["checkout_sha"])) {
$sha = $_SERVER["checkout_sha"];
} elseif (isset($_GET["sha"])) {
$sha = $_GET["sha"];
}

// write the time to the log
date_default_timezone_set("UTC");
fputs($file, date("d-m-Y (H:i:s)", $time) . "\n");
Expand Down Expand Up @@ -71,6 +81,29 @@ function forbid($file, $reason) {
// write to the log
fputs($file, "*** AUTO PULL INITIATED ***" . "\n");

/**
* Attempt to reset specific hash if specified
*/
if (!empty($_GET["reset"]) && $_GET["reset"] === "true") {
// write to the log
fputs($file, "*** RESET TO HEAD INITIATED ***" . "\n");

exec(GIT . " reset --hard HEAD 2>&1", $output, $exit);

// reformat the output as a string
$output = (!empty($output) ? implode("\n", $output) : "[no output]") . "\n";

// if an error occurred, return 500 and log the error
if ($exit !== 0) {
http_response_code(500);
$output = "=== ERROR: Reset to head failed using GIT `" . GIT . "` ===\n" . $output;
}

// write the output to the log and the body
fputs($file, $output);
echo $output;
}

/**
* Attempt to execute BEFORE_PULL if specified
*/
Expand Down Expand Up @@ -114,7 +147,30 @@ function forbid($file, $reason) {
echo $output;

/**
* Attempt to execute BEFORE_PULL if specified
* Attempt to checkout specific hash if specified
*/
if (!empty($sha)) {
// write to the log
fputs($file, "*** RESET TO HASH INITIATED ***" . "\n");

exec(GIT . " reset --hard {$sha} 2>&1", $output, $exit);

// reformat the output as a string
$output = (!empty($output) ? implode("\n", $output) : "[no output]") . "\n";

// if an error occurred, return 500 and log the error
if ($exit !== 0) {
http_response_code(500);
$output = "=== ERROR: Reset failed using GIT `" . GIT . "` and \$sha `" . $sha . "` ===\n" . $output;
}

// write the output to the log and the body
fputs($file, $output);
echo $output;
}

/**
* Attempt to execute AFTER_PULL if specified
*/
if (!empty(AFTER_PULL)) {
// write to the log
Expand Down

0 comments on commit d11e059

Please sign in to comment.