Return to Snippet

Revision: 62217
at February 8, 2013 02:25 by marcniko


Updated Code
<?php
/**
 * 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/.
 */
define('USERNAME', 'my_username');
define('PWD', 'my_password');
define('REMOTE_SERVER', 'www.myproject.com');

define('WORKING_BASE', '/path/to/workspace/project/');
define('REMOTE_BASE', '/project/');

if ($argc == 1) {
  echo "\nFalta parametre núm. del changeset (obligatori)\n";
  echo "> php deploy.php c032de81340 [--force] [--debug]\n\n";
  exit;
}

// Parametres:
// changeset
$changeSet = (string) $argv[1];
// --force
$force = false;
// --debug (opcional)
$debug = false;
for ($i=2; $i < $argc; $i++) {
  if ($argv[$i] == '--force') {
    $force = true;
  }
  if ($argv[$i] == '--debug') {
    $debug = true;
  }
}
define('FORCE', $force);
define('DEBUG', $debug);

$output = array();

echo "\n[$changeSet]\n";

exec("git show --name-only --pretty=oneline {$changeSet}", $output);

if (count($output) == 0) {
  echo "\nError en git";
  die;
}
$description = array_shift($output);

$descriptionArray = explode(' ', $description);
$changesetFull = array_shift($descriptionArray);

$description = implode(' ', $descriptionArray);

echo "\nPublicando changeset [$changesetFull]\n$description\n\n";


include('Net/SFTP.php');

if (DEBUG) {
  define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX);
}

$sftp = new Net_SFTP(REMOTE_SERVER);
if (!$sftp->login(USERNAME, PWD)) {
  exit('Login Failed');
}


foreach ($output as $fileName) {
  deployFile($fileName, $sftp);
}

echo "\n";

// ----------------------------------------------------------------------------------------
/**
 * Esta función se encarga de copiar el archivo local
 * al servidor remoto.
 *
 */
function deployFile($fileNamePath, $sftp)
{

  $pathToFileNameArray = explode('/', $fileNamePath);
  $fileName = array_pop($pathToFileNameArray);
  $pathToFileName = REMOTE_BASE. implode('/', $pathToFileNameArray);

  $messageIgnored = '';
  if ($fileName != 'databases.yml') {
    $local  = WORKING_BASE . $fileNamePath;
    $remote  = REMOTE_BASE . $fileNamePath;

    // copies filename.local to filename.remote on the SFTP server
    if (FORCE) {
      $ok = $sftp->put($remote, $local, NET_SFTP_LOCAL_FILE);

      echo (($ok)? "[OK] " : "\nERROR!\n");

      if (DEBUG) {
        echo $sftp->getSFTPLog();
      }
    }

//    system($command, $retval);
//    echo ' ['.$retval."]\n";
  } else {
    if (FORCE) {
      $messageIgnored = "'$fileName' NO publicat per precaució!\n\n";
    } else {
      $messageIgnored = "'$fileName' NO es publicarà  per precaució.\n\n";
    }
  }

  if (FORCE) {
    echo "$fileNamePath -- $fileName => $pathToFileName";
  } else {
    echo "$fileNamePath";
  }
  echo "\n";
  echo $messageIgnored;
}

Revision: 62216
at February 8, 2013 02:23 by marcniko


Updated Code
<?php
/**
 * 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/.
 */
define('USERNAME', 'my_username');
define('PWD', 'my_password');
define('REMOTE_SERVER', 'www.myproject.com');

define('WORKING_BASE', '/path/to/workspace/project/');
define('REMOTE_BASE', '/project/');

if ($argc == 1) {
  echo "\nFalta parametre núm. del changeset (obligatori)\n";
  echo "> php deploy.php c032de81340 [--force] [--debug]\n\n";
  exit;
}

// Parametres:
// changeset
$changeSet = (string) $argv[1];
// --force
$force = false;
// --debug (opcional)
$debug = false;
for ($i=2; $i < $argc; $i++) {
  if ($argv[$i] == '--force') {
    $force = true;
  }
  if ($argv[$i] == '--debug') {
    $debug = true;
  }
}
define('FORCE', $force);
define('DEBUG', $debug);

$output = array();

echo "\n[$changeSet]\n";

exec("git show --name-only --pretty=oneline {$changeSet}", $output);

if (count($output) == 0) {
  echo "\nError en git";
  die;
}
$description = array_shift($output);

$descriptionArray = explode(' ', $description);
$changesetFull = array_shift($descriptionArray);

$description = implode(' ', $descriptionArray);

echo "\nPublicando changeset [$changesetFull]\n$description\n\n";


include('Net/SFTP.php');

if (DEBUG) {
  define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX);
}

$sftp = new Net_SFTP(REMOTE_SERVER);
if (!$sftp->login(USERNAME, PWD)) {
  exit('Login Failed');
}


foreach ($output as $fileName) {
  deployFile($fileName, $sftp);
}

echo "\n";

// ----------------------------------------------------------------------------------------
/**
 * Esta función se encarga de copiar el archivo local
 * al servidor remoto.
 *
 */
function deployFile($fileNamePath, $sftp)
{

  $pathToFileNameArray = explode('/', $fileNamePath);
  $fileName = array_pop($pathToFileNameArray);
  $pathToFileName = REMOTE_BASE. implode('/', $pathToFileNameArray);

  $messageIgnored = '';
  if ($fileName != 'databases.yml') {
    $local  = WORKING_BASE . $fileNamePath;
    $remote  = REMOTE_BASE . $fileNamePath;

    // copies filename.local to filename.remote on the SFTP server
    if (FORCE) {
      $ok = $sftp->put($remote, $local, NET_SFTP_LOCAL_FILE);

      echo (($ok)? "[OK] " : "\nERROR!\n");

      if (DEBUG) {
        echo $sftp->getSFTPLog();
      }
    }

//    system($command, $retval);
//    echo ' ['.$retval."]\n";
  } else {
    if (FORCE) {
      $messageIgnored = "'$fileName' NO publicat per precaució!\n\n";
    } else {
      $messageIgnored = "'$fileName' NO es publicarà per precaució.\n\n";
    }
  }

  if (FORCE) {
    echo "$fileNamePath -- $fileName => $pathToFileName";
  } else {
    echo "$fileNamePath";
  }
  echo "\n";
  echo $messageIgnored;
}

Revision: 62215
at February 8, 2013 02:21 by marcniko


Initial Code
<?php
/**
 * 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/.
 */
define('USERNAME', 'my_username');
define('PWD', 'my_password');
define('REMOTE_SERVER', 'www.myproject.com');

define('WORKING_BASE', '/path/to/workspace/project/');
define('REMOTE_BASE', '/project/');

if ($argc == 1) {
  echo "\nFalta parametre núm. del changeset (obligatori)\n";
  echo "> php deploy.php c032de81340 [--force] [--debug]\n\n";
  exit;
}

// Parametres:
// changeset
$changeSet = (string) $argv[1];
// --force
$force = false;
// --debug (opcional)
$debug = false;
for ($i=2; $i < $argc; $i++) {
  if ($argv[$i] == '--force') {
    $force = true;
  }
  if ($argv[$i] == '--debug') {
    $debug = true;
  }
}
define('FORCE', $force);
define('DEBUG', $debug);

$output = array();

echo "\n[$changeSet]\n";

exec("git show --name-only --pretty=oneline {$changeSet}", $output);

if (count($output) == 0) {
  echo "\nError en git";
  die;
}
$description = array_shift($output);

$descriptionArray = explode(' ', $description);
$changesetFull = array_shift($descriptionArray);

$description = implode(' ', $descriptionArray);

echo "\nPublicando changeset [$changesetFull]\n$description\n\n";


include('Net/SFTP.php');

if (DEBUG) {
  define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX);
}

$sftp = new Net_SFTP(REMOTE_SERVER);
if (!$sftp->login(USERNAME, PWD)) {
  exit('Login Failed');
}


foreach ($output as $fileName) {
  deployFile($fileName, $sftp);
}

echo "\n";

// ----------------------------------------------------------------------------------------
/**
 * Esta función se encarga de copiar el archivo local
 * al servidor remoto.
 *
 */
function deployFile($fileNamePath, $sftp)
{

  $pathToFileNameArray = explode('/', $fileNamePath);
  $fileName = array_pop($pathToFileNameArray);
  $pathToFileName = REMOTE_BASE. implode('/', $pathToFileNameArray);

  $messageIgnored = '';
  if ($fileName != 'databases.yml') {
    $local  = WORKING_BASE . $fileNamePath;
    $remote  = REMOTE_BASE . $fileNamePath;

    // copies filename.local to filename.remote on the SFTP server
    if (FORCE) {
      $ok = $sftp->put($remote, $local, NET_SFTP_LOCAL_FILE);

      echo (($ok)? "[OK] " : "\nERROR!\n");

      if (DEBUG) {
        echo $sftp->getSFTPLog();
      }
    }

//    system($command, $retval);
//    echo ' ['.$retval."]\n";
  } else {
    if (FORCE) {
      $messageIgnored = "'$fileName' NO publicat per precaució!\n\n";
    } else {
      $messageIgnored = "'$fileName' NO es publicarà per precaució.\n\n";
    }
  }

  if (FORCE) {
    echo "$fileNamePath -- $fileName => $pathToFileName";
  } else {
    echo "$fileNamePath";
  }
  echo "\n";
  echo $messageIgnored;
}

Initial URL

                                

Initial Description
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.

Initial Title
Deploy GIT changeset to 1and1 via sftp (phpseclib)

Initial Tags
git

Initial Language
PHP