>  기사  >  php教程  >  通过PHP脚本自动部署GIT项目

通过PHP脚本自动部署GIT项目

PHP中文网
PHP中文网원래의
2016-05-23 16:38:572404검색

deploy.php

<?php
/**
 * Simple PHP Git deploy script
 *
 * Automatically deploy the code using PHP and Git.
 *
 * @version 1.3.1
 * @link  https://github.com/476552238li/deploy
 */
 
// =========================================[ Configuration start ]===
 
/**
 * It&#39;s preferable to configure the script using `deploy-config.php` file.
 *
 * Rename `deploy-config.example.php` to `deploy-config.php` and edit the
 * configuration options there instead of here. That way, you won&#39;t have to edit
 * the configuration again if you download the new version of `deploy.php`.
 */
if (file_exists(basename(__FILE__, &#39;.php&#39;).&#39;-config.php&#39;)) require_once basename(__FILE__, &#39;.php&#39;).&#39;-config.php&#39;;
 
/**
 * Protect the script from unauthorized access by using a secret access token.
 * If it&#39;s not present in the access URL as a GET variable named `sat`
 * e.g. deploy.php?sat=Bett...s the script is not going to deploy.
 *
 * @var string
 */
if (!defined(&#39;SECRET_ACCESS_TOKEN&#39;)) define(&#39;SECRET_ACCESS_TOKEN&#39;, &#39;6a604a35d5a7c3fd8786f5ee94991a8c&#39;);
 
/**
 * The address of the remote Git repository that contains the code that&#39;s being
 * deployed.
 * If the repository is private, you&#39;ll need to use the SSH address.
 *
 * @var string
 */
if (!defined(&#39;REMOTE_REPOSITORY&#39;)) define(&#39;REMOTE_REPOSITORY&#39;, &#39;https://github.com/476552238li/php-sql-parser.git&#39;);
 
/**
 * The branch that&#39;s being deployed.
 * Must be present in the remote repository.
 *
 * @var string
 */
if (!defined(&#39;BRANCH&#39;)) define(&#39;BRANCH&#39;, &#39;master&#39;);
 
/**
 * The location that the code is going to be deployed to.
 * Don&#39;t forget the trailing slash!
 *
 * @var string Full path including the trailing slash
 */
if (!defined(&#39;TARGET_DIR&#39;)) define(&#39;TARGET_DIR&#39;, &#39;/var/www/php-sql-parser&#39;);
 
/**
 * Whether to delete the files that are not in the repository but are on the
 * local (server) machine.
 *
 * !!! WARNING !!! This can lead to a serious loss of data if you&#39;re not
 * careful. All files that are not in the repository are going to be deleted,
 * except the ones defined in EXCLUDE section.
 * BE CAREFUL!
 *
 * @var boolean
 */
if (!defined(&#39;DELETE_FILES&#39;)) define(&#39;DELETE_FILES&#39;, false);
 
/**
 * The directories and files that are to be excluded when updating the code.
 * Normally, these are the directories containing files that are not part of
 * code base, for example user uploads or server-specific configuration files.
 * Use rsync exclude pattern syntax for each element.
 *
 * @var serialized array of strings
 */
if (!defined(&#39;EXCLUDE&#39;)) define(&#39;EXCLUDE&#39;, serialize(array(
  &#39;.git&#39;,
  )));
 
/**
 * Temporary directory we&#39;ll use to stage the code before the update. If it
 * already exists, script assumes that it contains an already cloned copy of the
 * repository with the correct remote origin and only fetches changes instead of
 * cloning the entire thing.
 *
 * @var string Full path including the trailing slash
 */
if (!defined(&#39;TMP_DIR&#39;)) define(&#39;TMP_DIR&#39;, &#39;/tmp/spgd-&#39;.md5(REMOTE_REPOSITORY).&#39;/&#39;);
 
/**
 * Whether to remove the TMP_DIR after the deployment.
 * It&#39;s useful NOT to clean up in order to only fetch changes on the next
 * deployment.
 */
if (!defined(&#39;CLEAN_UP&#39;)) define(&#39;CLEAN_UP&#39;, true);
 
/**
 * Output the version of the deployed code.
 *
 * @var string Full path to the file name
 */
if (!defined(&#39;VERSION_FILE&#39;)) define(&#39;VERSION_FILE&#39;, TMP_DIR.&#39;VERSION&#39;);
 
/**
 * Time limit for each command.
 *
 * @var int Time in seconds
 */
if (!defined(&#39;TIME_LIMIT&#39;)) define(&#39;TIME_LIMIT&#39;, 30);
 
/**
 * OPTIONAL
 * Backup the TARGET_DIR into BACKUP_DIR before deployment.
 *
 * @var string Full backup directory path e.g. `/tmp/`
 */
if (!defined(&#39;BACKUP_DIR&#39;)) define(&#39;BACKUP_DIR&#39;, false);
 
/**
 * OPTIONAL
 * Whether to invoke composer after the repository is cloned or changes are
 * fetched. Composer needs to be available on the server machine, installed
 * globaly (as `composer`). See http://getcomposer.org/doc/00-intro.md#globally
 *
 * @var boolean Whether to use composer or not
 * @link http://getcomposer.org/
 */
if (!defined(&#39;USE_COMPOSER&#39;)) define(&#39;USE_COMPOSER&#39;, false);
 
/**
 * OPTIONAL
 * The options that the composer is going to use.
 *
 * @var string Composer options
 * @link http://getcomposer.org/doc/03-cli.md#install
 */
if (!defined(&#39;COMPOSER_OPTIONS&#39;)) define(&#39;COMPOSER_OPTIONS&#39;, &#39;--no-dev&#39;);
 
/**
 * OPTIONAL
 * The COMPOSER_HOME environment variable is needed only if the script is
 * executed by a system user that has no HOME defined, e.g. `www-data`.
 *
 * @var string Path to the COMPOSER_HOME e.g. `/tmp/composer`
 * @link https://getcomposer.org/doc/03-cli.md#composer-home
 */
if (!defined(&#39;COMPOSER_HOME&#39;)) define(&#39;COMPOSER_HOME&#39;, false);
 
/**
 * OPTIONAL
 * Email address to be notified on deployment failure.
 *
 * @var string Email address
 */
if (!defined(&#39;EMAIL_ON_ERROR&#39;)) define(&#39;EMAIL_ON_ERROR&#39;, false);
 
// ===========================================[ Configuration end ]===
 
// If there&#39;s authorization error, set the correct HTTP header.
if (!isset($_GET[&#39;sat&#39;]) || $_GET[&#39;sat&#39;] !== SECRET_ACCESS_TOKEN || SECRET_ACCESS_TOKEN === &#39;6a604a35d5a7c3fd8786f5ee94991a8c&#39;) {
  header(&#39;HTTP/1.0 403 Forbidden&#39;);
}
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="robots" content="noindex">
  <title>Simple PHP Git deploy script</title>
  <style>
    body { padding: 0 1em; background: #222; color: #fff; }
    h2, .error { color: #c33; }
    .prompt { color: #6be234; }
    .command { color: #729fcf; }
    .output { color: #999; }
  </style>
</head>
<body>
  <?php
  if (!isset($_GET[&#39;sat&#39;]) || $_GET[&#39;sat&#39;] !== SECRET_ACCESS_TOKEN) {
    die(&#39;<h2>ACCESS DENIED!</h2>&#39;);
  }
  if (SECRET_ACCESS_TOKEN === &#39;BetterChangeMeNowOrSufferTheConsequences&#39;) {
    die("<h2>You&#39;re suffering the consequences!<br>Change the SECRET_ACCESS_TOKEN from it&#39;s default value!</h2>");
  }
  ?>
  <pre class="brush:php;toolbar:false">
 
    Checking the environment ...
 
    Running as <b><?php echo trim(shell_exec(&#39;whoami&#39;)); ?></b>.
 
    <?php
    // Check if the required programs are available
    $requiredBinaries = array(&#39;git&#39;, &#39;rsync&#39;);
    if (defined(&#39;BACKUP_DIR&#39;) && BACKUP_DIR !== false) {
      $requiredBinaries[] = &#39;tar&#39;;
      if (!is_dir(BACKUP_DIR) || !is_writable(BACKUP_DIR)) {
        die(sprintf(&#39;<div>BACKUP_DIR `%s` does not exists or is not writeable.</div>&#39;, BACKUP_DIR));
      }
    }
    if (defined(&#39;USE_COMPOSER&#39;) && USE_COMPOSER === true) {
      $requiredBinaries[] = &#39;composer --no-ansi&#39;;
    }
    foreach ($requiredBinaries as $command) {
      $path = trim(shell_exec(&#39;which &#39;.$command));
      if ($path == &#39;&#39;) {
        die(sprintf(&#39;<div><b>%s</b> not available. It needs to be installed on the server for this script to work.</div>&#39;, $command));
      } else {
        $version = explode("\n", shell_exec($command.&#39; --version&#39;));
        printf(&#39;<b>%s</b> : %s&#39;."\n"
          , $path
          , $version[0]
          );
      }
    }
    ?>
 
    Environment OK.
 
    Deploying <?php echo REMOTE_REPOSITORY; ?> <?php echo BRANCH."\n"; ?>
    to        <?php echo TARGET_DIR; ?> ...
 
    <?php
    // The commands
    $commands = array();
 
    // ========================================[ Pre-Deployment steps ]===
 
    if (!is_dir(TMP_DIR)) {
      // Clone the repository into the TMP_DIR
      $commands[] = sprintf(
        &#39;git clone --depth=1 --branch %s %s %s&#39;
        , BRANCH
        , REMOTE_REPOSITORY
        , TMP_DIR
        );
    } else {
      // TMP_DIR exists and hopefully already contains the correct remote origin
      // so we&#39;ll fetch the changes and reset the contents.
      $commands[] = sprintf(
        &#39;git --git-dir="%s.git" --work-tree="%s" fetch origin %s&#39;
        , TMP_DIR
        , TMP_DIR
        , BRANCH
        );
      $commands[] = sprintf(
        &#39;git --git-dir="%s.git" --work-tree="%s" reset --hard FETCH_HEAD&#39;
        , TMP_DIR
        , TMP_DIR
        );
    }
 
    // Update the submodules
    $commands[] = sprintf(
      &#39;git submodule update --init --recursive&#39;
      );
 
    // Describe the deployed version
    if (defined(&#39;VERSION_FILE&#39;) && VERSION_FILE !== &#39;&#39;) {
      $commands[] = sprintf(
        &#39;git --git-dir="%s.git" --work-tree="%s" describe --always > %s&#39;
        , TMP_DIR
        , TMP_DIR
        , VERSION_FILE
        );
    }
 
    // Backup the TARGET_DIR
    // without the BACKUP_DIR for the case when it&#39;s inside the TARGET_DIR
    if (defined(&#39;BACKUP_DIR&#39;) && BACKUP_DIR !== false) {
      $commands[] = sprintf(
        "tar --exclude=&#39;%s*&#39; -czf %s/%s-%s-%s.tar.gz %s*"
        , BACKUP_DIR
        , BACKUP_DIR
        , basename(TARGET_DIR)
        , md5(TARGET_DIR)
        , date(&#39;YmdHis&#39;)
        , TARGET_DIR // We&#39;re backing up this directory into BACKUP_DIR
      );
    }
 
    // Invoke composer
    if (defined(&#39;USE_COMPOSER&#39;) && USE_COMPOSER === true) {
      $commands[] = sprintf(
        &#39;composer --no-ansi --no-interaction --no-progress --working-dir=%s install %s&#39;
        , TMP_DIR
        , (defined(&#39;COMPOSER_OPTIONS&#39;)) ? COMPOSER_OPTIONS : &#39;&#39;
        );
      if (defined(&#39;COMPOSER_HOME&#39;) && is_dir(COMPOSER_HOME)) {
        putenv(&#39;COMPOSER_HOME=&#39;.COMPOSER_HOME);
      }
    }
 
    // ==================================================[ Deployment ]===
 
    // Compile exclude parameters
    $exclude = &#39;&#39;;
    foreach (unserialize(EXCLUDE) as $exc) {
      $exclude .= &#39; --exclude=&#39;.$exc;
    }
 
    // Deployment command
    $commands[] = sprintf(
      &#39;rsync -rltgoDzvO %s %s %s %s&#39;
      , TMP_DIR
      , TARGET_DIR
      , (DELETE_FILES) ? &#39;--delete-after&#39; : &#39;&#39;
      , $exclude
    );
 
    // =======================================[ Post-Deployment steps ]===
 
    // Remove the TMP_DIR (depends on CLEAN_UP)
    if (CLEAN_UP) {
      $commands[&#39;cleanup&#39;] = sprintf(
        &#39;rm -rf %s&#39;
        , TMP_DIR
        );
    }
 
    // =======================================[ Run the command steps ]===
    $output = &#39;&#39;;
    foreach ($commands as $command) {
      set_time_limit(TIME_LIMIT); // Reset the time limit for each command
      if (file_exists(TMP_DIR) && is_dir(TMP_DIR)) {
        chdir(TMP_DIR); // Ensure that we&#39;re in the right directory
      }
      $tmp = array();
      exec($command.&#39; 2>&1&#39;, $tmp, $return_code); // Execute the command
      // Output the result
      printf(&#39;
        <span>$</span> <span>%s</span>
        <div>%s</div>
        &#39;
        , htmlentities(trim($command))
        , htmlentities(trim(implode("\n", $tmp)))
        );
      $output .= ob_get_contents();
      ob_flush(); // Try to output everything as it happens
 
      // Error handling and cleanup
      if ($return_code !== 0) {
        printf(&#39;
          <div>
            Error encountered!
            Stopping the script to prevent possible data loss.
            CHECK THE DATA IN YOUR TARGET DIR!
          </div>
          &#39;
          );
        if (CLEAN_UP) {
          $tmp = shell_exec($commands[&#39;cleanup&#39;]);
          printf(&#39;
            Cleaning up temporary files ...
 
            <span>$</span> <span>%s</span>
            <div>%s</div>
            &#39;
            , htmlentities(trim($commands[&#39;cleanup&#39;]))
            , htmlentities(trim($tmp))
            );
        }
        $error = sprintf(
          &#39;Deployment error on %s using %s!&#39;
          , $_SERVER[&#39;HTTP_HOST&#39;]
          , __FILE__
          );
        error_log($error);
        if (EMAIL_ON_ERROR) {
          $output .= ob_get_contents();
          $headers = array();
          $headers[] = sprintf(&#39;From: Simple PHP Git deploy script <simple-php-git-deploy@%s>&#39;, $_SERVER[&#39;HTTP_HOST&#39;]);
          $headers[] = sprintf(&#39;X-Mailer: PHP/%s&#39;, phpversion());
          mail(EMAIL_ON_ERROR, $error, strip_tags(trim($output)), implode("\r\n", $headers));
        }
        break;
      }
    }
    ?>
  Done.
  
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:php 账号互联다음 기사:判断是否是身份证号