Skip to content

Automatically deploy a git repository to a server after pushing with minimal configuration.

License

Notifications You must be signed in to change notification settings

patinthehat/permafrost-git-deploy

Repository files navigation

Permafrost Git Deploy


Automatically deploy a git repository to a server after pushing with minimal configuration.


#####Basis##### Permafrost Git Deploy is based on v1.3.1 of the "Simple PHP Git Deploy" script, which can be found at https://github.com/markomarkovic/simple-php-git-deploy/.


####Initial Installation####

Simply clone this repository into a web-accessable directory.


Local Configuration


Make a copy of deploy-sample.php and rename it (for example), deploy.php. The PGD \Deploy class requires an instance of configuration class \RepositoryInfo to be passed in when executing the deploy (see below for an example).

The \RepositoryInfo class has the following properties that need to be configured:

  • name: Repository Name
  • remoteRepository: ssh or https link to the remote repository to be used for cloning/fetching. If an https link is used in the format "https://user:password@...", the username and password are hidden from the output during deploy.
  • branch: the branch name that we're deploying
  • targetDirectory: the absolute path to the location to deploy the repository on the remote server. Make sure to include a trailing slash.
  • tempDirectory: the absolute path to the temporary directory on the server used for staging the repository during deploy. Make sure to include a trailing slash.
  • excludedDirectories: array of relative directory names that are excluded from deployment, backups, and file deletion.
  • versionFile: the absolute path to a VERSION file that PGD requires to keep track of the latest commit hash. A good value is:
     $ri->tempDirectory.'VERSION';
  • backupDirectory: the absolute path to the directory to store repository backups -- backups are performed of the repository before the deploy. Make sure to include a trailing slash.
  • deleteFiles: set to true to delete files in the target directory before deployment. Use of this setting can result in massive data loss if you're not careful!
  • cleanUp: set to true to clean up files created in the temporary directory. Setting this to false is recomended, as it enables only the changed files to be fetched from the remote repository during deployment.
  • backUp: set to true to enable creating a backup archive of the repository before deployment.
  • useComposer: set to true to enable composer support -- a composer install command will be run after staging the repository.
  • deployTimer: set to true to enable a deployment timer, which, after the deployment completes, will output the length of time in seconds the deployment took.
Sample Configuration & Execution
<?php
  include('autoload.php');
  $ri = new \PGD\RepositoryInfo();
  $ri->name = "test repo";
  ...
  //see above for config properties--all properties must be configured!
  $deployer = new \PGD\Deploy(new \PGD\Output\BrowserOutputHtml());
  $deployer->performDeploy($ri);

see deploy-sample.php for a complete deploy script example

Authentication

To enable basic authentication for the deploy script, you must create an AuthenticationDriver and pass a class that implements the Authentication abstract class to it.

Example using the AuthenticationSecretKey class:

<?php
  $browserOutput = new \PGD\Output\BrowserOutputHtml();
  $auth = new \PGD\Authentication\AuthenticationDriver(
    $browserOutput, 
    new \PGD\Authentication\AuthenticationSecretKey("mypassword", "sk", 'get')
  );

  if ($auth->authenticate() === true) {
    $d = new \PGD\Deploy($browserOutput);
    $d->performDeploy($ri);    
  }

For the deploy to execute, the following url must be called, assuming it is named deploy.php: deploy.php?sk=mypassword

Multiple Repository Deploy Scripts

You probably have multiple repositories and would like to be able to easily deploy any of them from the same script. This can be done using the \PGD\RepositoryInfoCollection and \PGD\MultiRepositoryController classes:

<?php
  include('autoload.php');
  
  $ri1 = new \PGD\RepositoryInfo();
  $ri1->name = 'test1';
  //...config here...
  $ri2 = new \PGD\RepositoryInfo();
  $ri2->name = 'test2';
  //...config here...

  $repositories = new \PGD\RepositoryInfoCollection();
  $repositories->add($ri1);
  $repositories->add($ri2);

  $controller     = new \PGD\MultiRepositoryController($repositories);
  $auth           = new \PGD\Authentication\AuthenticationSecretKey("mypassword", "sk", 'get');
  $browserOutput  = new \PGD\Output\BrowserOutputHtml();
  $authDriver     = new \PGD\Authentication\AuthenticationDriver($browserOutput, $auth);

  if ($authDriver->authenticate() === true) {
    if ($controller->validRequest()) {
      $r = $controller->getRequestedRepository();
      $d = new \PGD\Deploy($browserOutput);
      $d->performDeploy($r);
      exit();
    } else {
      $browserOutput->writeQueued('Requested repository not found.');
    }
  }
  
  $browserOutput->writeRaw('<html><body><pre>');
  $browserOutput->writeQueue();
  $browserOutput->writeRaw('</pre></body></html>');

The previous script would be executed by requesting deploy.php?sk=mypassword&repository=test1 -- this would cause the repository configuration named "test1" to deploy. Similarly, the repository parameter could be changed to test2 to deploy the "test2" repository.


Repository Host Configuration


  • You must configure webhooks to pull the url of your deploy.php post-push.

  • #####Github##### @todo ---

  • #####BitBucket##### @todo ---


Output


The \Deploy class will output the user it is running as, virtual host, and version strings of the required binaries.
Next, it outputs the remote repository, branch name and the target deployment directory. Finally, it will output all commands it executes, as it executes them, along with the output from those commands. Output buffering is used to allow for real-time command output.

If the deployTimer setting is enabled, it will output the length of time it took for the deploy to complete (in seconds) at the end of deployment execution.

HTML output styling is done entirely with CSS -- see permafrost-deploy.css.

Note: If your remote repository is an https site with a user:pass in it, the Deploy class will automatically mask both the username and password during output.


Requirements


The server you run Permafrost Git Deploy on must have the following installed:

  • PHP 5.4+
  • whoami
  • which
  • git
  • rsync

And optionally, depending on your configuration:

  • tar: used to create backups.
  • composer: used if the useComposer property is set to true.

@todo


  • Implement a plugin system to allow for things like HipChat integration, mailing output logs, etc. I'd also like to convert the DeployTimer into a plugin.

  • Create a basic authentication class that requires a secret key or something similar to be passed to the URL for deployment to occur.

  • Create a basic authentication class that implements RFC 2617 HTTP Basic and/or Digest Authentication.

  • Create a class that loads multiple repository configurations and can execute deployment for any of them based on a URL parameter.

  • Create a BrowserOutputText class that outputs just text and a content type of text/plain.

  • Change the BrowserOutput requirement to just Output or something similar -- allow for default output not just to the browser, but log files, database, etc.

  • README - Repository host-specific configurations (github, etc.).

  • README - Generic repository configuration using hooks.

  • Unit Tests

  • Greater than 80% code coverage


License


Permafrost Git Deploy is open source software, available under the MIT license.

About

Automatically deploy a git repository to a server after pushing with minimal configuration.

Resources

License

Stars

Watchers

Forks

Packages

No packages published