Deploy GIT changeset to 1and1 via sftp (phpseclib)


/ Published in: PHP
Save to your folder(s)

Script per copiar els fitxers canviats en una revisió git al servidor remot via sftp, using PHP Secure Communications Library (phpseclib): http://sourceforge.net/projects/phpseclib/.
Útil per usar amb 1and1.


Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3.  * Script per copiar els fitxers canviats en una revisió al servidor remot via sftp, using phpseclib (v. 0.3.1): http://sourceforge.net/projects/phpseclib/.
  4.  */
  5. define('USERNAME', 'my_username');
  6. define('PWD', 'my_password');
  7. define('REMOTE_SERVER', 'www.myproject.com');
  8.  
  9. define('WORKING_BASE', '/path/to/workspace/project/');
  10. define('REMOTE_BASE', '/project/');
  11.  
  12. if ($argc == 1) {
  13. echo "\nFalta parametre núm. del changeset (obligatori)\n";
  14. echo "> php deploy.php c032de81340 [--force] [--debug]\n\n";
  15. }
  16.  
  17. // Parametres:
  18. // changeset
  19. $changeSet = (string) $argv[1];
  20. // --force
  21. $force = false;
  22. // --debug (opcional)
  23. $debug = false;
  24. for ($i=2; $i < $argc; $i++) {
  25. if ($argv[$i] == '--force') {
  26. $force = true;
  27. }
  28. if ($argv[$i] == '--debug') {
  29. $debug = true;
  30. }
  31. }
  32. define('FORCE', $force);
  33. define('DEBUG', $debug);
  34.  
  35. $output = array();
  36.  
  37. echo "\n[$changeSet]\n";
  38.  
  39. exec("git show --name-only --pretty=oneline {$changeSet}", $output);
  40.  
  41. if (count($output) == 0) {
  42. echo "\nError en git";
  43. die;
  44. }
  45. $description = array_shift($output);
  46.  
  47. $descriptionArray = explode(' ', $description);
  48. $changesetFull = array_shift($descriptionArray);
  49.  
  50. $description = implode(' ', $descriptionArray);
  51.  
  52. echo "\nPublicando changeset [$changesetFull]\n$description\n\n";
  53.  
  54.  
  55. include('Net/SFTP.php');
  56.  
  57. if (DEBUG) {
  58. define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX);
  59. }
  60.  
  61. $sftp = new Net_SFTP(REMOTE_SERVER);
  62. if (!$sftp->login(USERNAME, PWD)) {
  63. exit('Login Failed');
  64. }
  65.  
  66.  
  67. foreach ($output as $fileName) {
  68. deployFile($fileName, $sftp);
  69. }
  70.  
  71. echo "\n";
  72.  
  73. // ----------------------------------------------------------------------------------------
  74. /**
  75.  * Esta función se encarga de copiar el archivo local
  76.  * al servidor remoto.
  77.  *
  78.  */
  79. function deployFile($fileNamePath, $sftp)
  80. {
  81.  
  82. $pathToFileNameArray = explode('/', $fileNamePath);
  83. $fileName = array_pop($pathToFileNameArray);
  84. $pathToFileName = REMOTE_BASE. implode('/', $pathToFileNameArray);
  85.  
  86. $messageIgnored = '';
  87. if ($fileName != 'databases.yml') {
  88. $local = WORKING_BASE . $fileNamePath;
  89. $remote = REMOTE_BASE . $fileNamePath;
  90.  
  91. // copies filename.local to filename.remote on the SFTP server
  92. if (FORCE) {
  93. $ok = $sftp->put($remote, $local, NET_SFTP_LOCAL_FILE);
  94.  
  95. echo (($ok)? "[OK] " : "\nERROR!\n");
  96.  
  97. if (DEBUG) {
  98. echo $sftp->getSFTPLog();
  99. }
  100. }
  101.  
  102. // system($command, $retval);
  103. // echo ' ['.$retval."]\n";
  104. } else {
  105. if (FORCE) {
  106. $messageIgnored = "'$fileName' NO publicat per precaució!\n\n";
  107. } else {
  108. $messageIgnored = "'$fileName' NO es publicarà  per precaució.\n\n";
  109. }
  110. }
  111.  
  112. if (FORCE) {
  113. echo "$fileNamePath -- $fileName => $pathToFileName";
  114. } else {
  115. echo "$fileNamePath";
  116. }
  117. echo "\n";
  118. echo $messageIgnored;
  119. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.