<?php/** * Deploys commits to the file-system */functiondeployChangeSet($postData){global$CONFIG,$DEPLOY,$DEPLOY_BRANCH;global$processed;global$rmdirs;$o=json_decode($postData);if(!$o){// could not parse ?echo" ! Invalid JSON file\n";returnfalse;}// determine the destination of the deploymentif(array_key_exists($o->repository->name,$DEPLOY)){$deployLocation=$DEPLOY[$o->repository->name].(substr($DEPLOY[$o->repository->name],-1)==DIRECTORY_SEPARATOR?'':DIRECTORY_SEPARATOR);}else{// unknown repository ?echo" ! Repository not configured for sync: {$o->repository->name}\n";returnfalse;}// determine from which branch to get the dataif(isset($DEPLOY_BRANCH)&&array_key_exists($o->repository->name,$DEPLOY_BRANCH)){$deployBranch=$DEPLOY_BRANCH[$o->repository->name];}else{// use the default branch$deployBranch=$CONFIG['deployBranch'];}// build URL to get the updated files$baseUrl='https://bitbucket.org';# https://bitbucket.org$apiUrl='/api/1.0/repositories';# /api/1.0/repositories$repoUrl=strtolower($o->repository->full_name);# /user/repo/$rawUrl='raw/';# raw/$branchUrl=$deployBranch.'/';# branch/// prepare to get the files$pending=array();// loop through commitsforeach($o->push->changes->commitsas$commit){// check if the branch is known at this steploginfo(" > Change-set: ".trim($commit->message)."\n");if(!empty($commit->branch)||!empty($commit->branches)){// if commit was on the branch we're watching, deploy changesif($commit->branch==$deployBranch||(!empty($commit->branches)&&array_search($deployBranch,$commit->branches)!==false)){// if there are any pending files, merge them in$files=array_merge($pending,$commit->files);// get a list of filesforeach($filesas$file){if($file->type=='modified'||$file->type=='added'){if(empty($processed[$file->file])){$processed[$file->file]=1;// mark as processed$contents=getFileContents($baseUrl.$apiUrl.$repoUrl.$rawUrl.$branchUrl.$file->file);if($contents=='Not Found'){// try one more time, BitBucket gets weirdo sometimes$contents=getFileContents($baseUrl.$apiUrl.$repoUrl.$rawUrl.$branchUrl.$file->file);}if($contents!='Not Found'&&$contents!==false){if(!is_dir(dirname($deployLocation.$file->file))){// attempt to create the directory structure firstmkdir(dirname($deployLocation.$file->file),0755,true);}file_put_contents($deployLocation.$file->file,$contents);loginfo(" - Synchronized $file->file\n");}else{echo" ! Could not get file contents for $file->file\n";flush();}}}elseif($file->type=='removed'){unlink($deployLocation.$file->file);$processed[$file->file]=0;// to allow for subsequent re-creating of this file$rmdirs[dirname($deployLocation.$file->file)]=dirname($file->file);loginfo(" - Removed $file->file\n");}}}// clean pending files, if any$pending=array();}else{// unknown branch for now, keep these files$pending=array_merge($pending,$commit->files);}}returntrue;}
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.