0

0

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

PHP中文网

PHP中文网

发布时间:2016-05-23 16:38:57

|

2763人浏览过

|

来源于php中文网

原创

deploy.php

Knowt
Knowt

Knowt是一款AI驱动的在线学习工具

下载
<?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'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't have to edit
 * the configuration again if you download the new version of `deploy.php`.
 */
if (file_exists(basename(__FILE__, '.php').'-config.php')) require_once basename(__FILE__, '.php').'-config.php';
 
/**
 * Protect the script from unauthorized access by using a secret access token.
 * If it'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('SECRET_ACCESS_TOKEN')) define('SECRET_ACCESS_TOKEN', '6a604a35d5a7c3fd8786f5ee94991a8c');
 
/**
 * The address of the remote Git repository that contains the code that's being
 * deployed.
 * If the repository is private, you'll need to use the SSH address.
 *
 * @var string
 */
if (!defined('REMOTE_REPOSITORY')) define('REMOTE_REPOSITORY', 'https://github.com/476552238li/php-sql-parser.git');
 
/**
 * The branch that's being deployed.
 * Must be present in the remote repository.
 *
 * @var string
 */
if (!defined('BRANCH')) define('BRANCH', 'master');
 
/**
 * The location that the code is going to be deployed to.
 * Don't forget the trailing slash!
 *
 * @var string Full path including the trailing slash
 */
if (!defined('TARGET_DIR')) define('TARGET_DIR', '/var/www/php-sql-parser');
 
/**
 * 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'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('DELETE_FILES')) define('DELETE_FILES', 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('EXCLUDE')) define('EXCLUDE', serialize(array(
  '.git',
  )));
 
/**
 * Temporary directory we'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('TMP_DIR')) define('TMP_DIR', '/tmp/spgd-'.md5(REMOTE_REPOSITORY).'/');
 
/**
 * Whether to remove the TMP_DIR after the deployment.
 * It's useful NOT to clean up in order to only fetch changes on the next
 * deployment.
 */
if (!defined('CLEAN_UP')) define('CLEAN_UP', true);
 
/**
 * Output the version of the deployed code.
 *
 * @var string Full path to the file name
 */
if (!defined('VERSION_FILE')) define('VERSION_FILE', TMP_DIR.'VERSION');
 
/**
 * Time limit for each command.
 *
 * @var int Time in seconds
 */
if (!defined('TIME_LIMIT')) define('TIME_LIMIT', 30);
 
/**
 * OPTIONAL
 * Backup the TARGET_DIR into BACKUP_DIR before deployment.
 *
 * @var string Full backup directory path e.g. `/tmp/`
 */
if (!defined('BACKUP_DIR')) define('BACKUP_DIR', 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('USE_COMPOSER')) define('USE_COMPOSER', 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('COMPOSER_OPTIONS')) define('COMPOSER_OPTIONS', '--no-dev');
 
/**
 * 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('COMPOSER_HOME')) define('COMPOSER_HOME', false);
 
/**
 * OPTIONAL
 * Email address to be notified on deployment failure.
 *
 * @var string Email address
 */
if (!defined('EMAIL_ON_ERROR')) define('EMAIL_ON_ERROR', false);
 
// ===========================================[ Configuration end ]===
 
// If there's authorization error, set the correct HTTP header.
if (!isset($_GET['sat']) || $_GET['sat'] !== SECRET_ACCESS_TOKEN || SECRET_ACCESS_TOKEN === '6a604a35d5a7c3fd8786f5ee94991a8c') {
  header('HTTP/1.0 403 Forbidden');
}
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['sat']) || $_GET['sat'] !== SECRET_ACCESS_TOKEN) {
    die('<h2>ACCESS DENIED!</h2>');
  }
  if (SECRET_ACCESS_TOKEN === 'BetterChangeMeNowOrSufferTheConsequences') {
    die("<h2>You're suffering the consequences!<br>Change the SECRET_ACCESS_TOKEN from it's default value!</h2>");
  }
  ?>
  <pre class="brush:php;toolbar:false;">
 
    Checking the environment ...
 
    Running as <b><?php echo trim(shell_exec('whoami')); ?></b>.
 
    <?php
    // Check if the required programs are available
    $requiredBinaries = array('git', 'rsync');
    if (defined('BACKUP_DIR') && BACKUP_DIR !== false) {
      $requiredBinaries[] = 'tar';
      if (!is_dir(BACKUP_DIR) || !is_writable(BACKUP_DIR)) {
        die(sprintf('<div>BACKUP_DIR `%s` does not exists or is not writeable.</div>', BACKUP_DIR));
      }
    }
    if (defined('USE_COMPOSER') && USE_COMPOSER === true) {
      $requiredBinaries[] = 'composer --no-ansi';
    }
    foreach ($requiredBinaries as $command) {
      $path = trim(shell_exec('which '.$command));
      if ($path == '') {
        die(sprintf('<div><b>%s</b> not available. It needs to be installed on the server for this script to work.</div>', $command));
      } else {
        $version = explode("\n", shell_exec($command.' --version'));
        printf('<b>%s</b> : %s'."\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(
        'git clone --depth=1 --branch %s %s %s'
        , BRANCH
        , REMOTE_REPOSITORY
        , TMP_DIR
        );
    } else {
      // TMP_DIR exists and hopefully already contains the correct remote origin
      // so we'll fetch the changes and reset the contents.
      $commands[] = sprintf(
        'git --git-dir="%s.git" --work-tree="%s" fetch origin %s'
        , TMP_DIR
        , TMP_DIR
        , BRANCH
        );
      $commands[] = sprintf(
        'git --git-dir="%s.git" --work-tree="%s" reset --hard FETCH_HEAD'
        , TMP_DIR
        , TMP_DIR
        );
    }
 
    // Update the submodules
    $commands[] = sprintf(
      'git submodule update --init --recursive'
      );
 
    // Describe the deployed version
    if (defined('VERSION_FILE') && VERSION_FILE !== '') {
      $commands[] = sprintf(
        'git --git-dir="%s.git" --work-tree="%s" describe --always > %s'
        , TMP_DIR
        , TMP_DIR
        , VERSION_FILE
        );
    }
 
    // Backup the TARGET_DIR
    // without the BACKUP_DIR for the case when it's inside the TARGET_DIR
    if (defined('BACKUP_DIR') && BACKUP_DIR !== false) {
      $commands[] = sprintf(
        "tar --exclude='%s*' -czf %s/%s-%s-%s.tar.gz %s*"
        , BACKUP_DIR
        , BACKUP_DIR
        , basename(TARGET_DIR)
        , md5(TARGET_DIR)
        , date('YmdHis')
        , TARGET_DIR // We're backing up this directory into BACKUP_DIR
      );
    }
 
    // Invoke composer
    if (defined('USE_COMPOSER') && USE_COMPOSER === true) {
      $commands[] = sprintf(
        'composer --no-ansi --no-interaction --no-progress --working-dir=%s install %s'
        , TMP_DIR
        , (defined('COMPOSER_OPTIONS')) ? COMPOSER_OPTIONS : ''
        );
      if (defined('COMPOSER_HOME') && is_dir(COMPOSER_HOME)) {
        putenv('COMPOSER_HOME='.COMPOSER_HOME);
      }
    }
 
    // ==================================================[ Deployment ]===
 
    // Compile exclude parameters
    $exclude = '';
    foreach (unserialize(EXCLUDE) as $exc) {
      $exclude .= ' --exclude='.$exc;
    }
 
    // Deployment command
    $commands[] = sprintf(
      'rsync -rltgoDzvO %s %s %s %s'
      , TMP_DIR
      , TARGET_DIR
      , (DELETE_FILES) ? '--delete-after' : ''
      , $exclude
    );
 
    // =======================================[ Post-Deployment steps ]===
 
    // Remove the TMP_DIR (depends on CLEAN_UP)
    if (CLEAN_UP) {
      $commands['cleanup'] = sprintf(
        'rm -rf %s'
        , TMP_DIR
        );
    }
 
    // =======================================[ Run the command steps ]===
    $output = '';
    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're in the right directory
      }
      $tmp = array();
      exec($command.' 2>&1', $tmp, $return_code); // Execute the command
      // Output the result
      printf('
        <span>$</span> <span>%s</span>
        <div>%s</div>
        '
        , 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('
          <div>
            Error encountered!
            Stopping the script to prevent possible data loss.
            CHECK THE DATA IN YOUR TARGET DIR!
          </div>
          '
          );
        if (CLEAN_UP) {
          $tmp = shell_exec($commands['cleanup']);
          printf('
            Cleaning up temporary files ...
 
            <span>$</span> <span>%s</span>
            <div>%s</div>
            '
            , htmlentities(trim($commands['cleanup']))
            , htmlentities(trim($tmp))
            );
        }
        $error = sprintf(
          'Deployment error on %s using %s!'
          , $_SERVER['HTTP_HOST']
          , __FILE__
          );
        error_log($error);
        if (EMAIL_ON_ERROR) {
          $output .= ob_get_contents();
          $headers = array();
          $headers[] = sprintf('From: Simple PHP Git deploy script <simple-php-git-deploy@%s>', $_SERVER['HTTP_HOST']);
          $headers[] = sprintf('X-Mailer: PHP/%s', phpversion());
          mail(EMAIL_ON_ERROR, $error, strip_tags(trim($output)), implode("\r\n", $headers));
        }
        break;
      }
    }
    ?>
  Done.
  
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
c语言 数据类型
c语言 数据类型

本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.02.12

雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法
雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法

本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。

2

2026.02.12

豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法
豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法

本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。

46

2026.02.12

PostgreSQL性能优化与索引调优实战
PostgreSQL性能优化与索引调优实战

本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。

8

2026.02.12

Next.js全栈开发与SSR服务端渲染实战
Next.js全栈开发与SSR服务端渲染实战

本专题系统讲解 Next.js 框架在现代全栈开发中的应用,重点解析 SSR、SSG 与 ISR 渲染模式的原理与差异。内容涵盖路由系统、API Routes、数据获取策略、性能优化以及部署实践。通过完整项目示例,帮助开发者掌握高性能 SEO 友好的 React 全栈开发方案。

2

2026.02.12

Kotlin协程编程与Spring Boot集成实践
Kotlin协程编程与Spring Boot集成实践

本专题围绕 Kotlin 协程机制展开,深入讲解挂起函数、协程作用域、结构化并发与异常处理机制,并结合 Spring Boot 展示协程在后端开发中的实际应用。内容涵盖异步接口设计、数据库调用优化、线程资源管理以及性能调优策略,帮助开发者构建更加简洁高效的 Kotlin 后端服务架构。

27

2026.02.12

2026春节习俗大全
2026春节习俗大全

本专题整合了2026春节习俗大全,阅读专题下面的文章了解更多详细内容。

273

2026.02.11

Yandex网页版官方入口使用指南_国际版与俄罗斯版访问方法解析
Yandex网页版官方入口使用指南_国际版与俄罗斯版访问方法解析

本专题全面整理了Yandex搜索引擎的官方入口信息,涵盖国际版与俄罗斯版官网访问方式、网页版直达入口及免登录使用说明,帮助用户快速、安全地进入Yandex官网,高效使用其搜索与相关服务。

850

2026.02.11

虫虫漫画网页版入口与免费阅读指南_正版漫画全集在线查看方法
虫虫漫画网页版入口与免费阅读指南_正版漫画全集在线查看方法

本专题系统整理了虫虫漫画官网及网页版最新入口,涵盖免登录观看、正版漫画全集在线阅读方式,并汇总稳定可用的访问渠道,帮助用户快速找到虫虫漫画官方页面,轻松在线阅读各类热门漫画内容。

98

2026.02.11

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP自制框架
PHP自制框架

共8课时 | 0.6万人学习

手把手带您写PHP框架_设计开发篇
手把手带您写PHP框架_设计开发篇

共18课时 | 3.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号