Skip to content

Instantly share code, notes, and snippets.

@thinkstylestudio
Created September 8, 2012 23:26
Show Gist options
  • Select an option

  • Save thinkstylestudio/3680991 to your computer and use it in GitHub Desktop.

Select an option

Save thinkstylestudio/3680991 to your computer and use it in GitHub Desktop.

Revisions

  1. thinkstylestudio revised this gist Sep 8, 2012. No changes.
  2. thinkstylestudio created this gist Sep 8, 2012.
    51 changes: 51 additions & 0 deletions deploy.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    <?php

    // Set these dependant on your BB credentials

    $username = 'username';
    $password = 'password';

    // Grab the data from BB's POST service and decode
    $json = stripslashes($_POST['payload']);
    $data = json_decode($json);

    // Set some parameters to fetch the correct files

    $uri = $data->repository->absolute_url;
    $node = $data->commits[0]->node;
    $files = $data->commits[0]->files;

    // Foreach through the files and curl them over

    foreach ($files as $file) {

    if ($file->type == "removed") {

    unlink($file->file);

    } else {

    $url = "https://api.bitbucket.org/1.0/repositories".$uri."raw/".$node."/".$file->file;
    $path = $file->file;

    $dirname = dirname($path);
    if (!is_dir($dirname)){
    mkdir($dirname, 0775, true);
    }

    $fp = fopen($path, 'w');

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_FILE, $fp);

    $data = curl_exec($ch);

    curl_close($ch);
    fclose($fp);

    }

    }