首页 文章

PHP webhook脚本(bitbucket)

提问于
浏览
0

我正在尝试使用webhook脚本,以便我可以在本地提交并让脚本触发服务器端并引入任何更改 .

现在,如果我通过SSH登录服务器并运行 php webhook.php ,则会成功触发cript并更新文件 . 所以我知道该文件确实有效 . 但是,如果我对文件进行编辑,提交并推送到master,我没有看到文件更新 .

脚本生成的日志文件表明一切正常,但显然不是 .

我的文件结构是这样的:

var
  www
    my-project
    - webhook.php
       repo-folder

因此文件应该将文件拉入repo-folder,并通过bitbucket控制面板将webhook.php设置为webhook . 如果我在bitbucket中查看日志,那么每次推送提交时它都会显示一个成功的请求 .

剧本:

<?php

date_default_timezone_set('Europe/London');

class Deploy {

    /**
     * A callback function to call after the deploy has finished.
     *
     * @var callback
     */
    public $post_deploy;

    /**
     * The name of the file that will be used for logging deployments. Set to
     * FALSE to disable logging.
     *
     * @var string
     */
    private $_log = 'deployments.log';

    /**
     * The timestamp format used for logging.
     *
     * @link    http://www.php.net/manual/en/function.date.php
     * @var     string
     */
    private $_date_format = 'Y-m-d H:i:sP';

    /**
     * The name of the branch to pull from.
     *
     * @var string
     */
    private $_branch = 'master';

    /**
     * The name of the remote to pull from.
     *
     * @var string
     */
    private $_remote = 'origin';

    /**
     * The directory where your website and git repository are located, can be
     * a relative or absolute path
     *
     * @var string
     */
    private $_directory;

    /**
     * Sets up defaults.
     *
     * @param  string  $directory  Directory where your website is located
     * @param  array   $data       Information about the deployment
     */
    public function __construct($directory, $options = array())
    {
        // Determine the directory path
        $this->_directory = realpath($directory).DIRECTORY_SEPARATOR;

        $available_options = array('log', 'date_format', 'branch', 'remote');

        foreach ($options as $option => $value)
        {
            if (in_array($option, $available_options))
            {
                $this->{'_'.$option} = $value;
            }
        }

        $this->log('Attempting deployment...');
    }

    /**
     * Writes a message to the log file.
     *
     * @param  string  $message  The message to write
     * @param  string  $type     The type of log message (e.g. INFO, DEBUG, ERROR, etc.)
     */
    public function log($message, $type = 'INFO')
    {
        if ($this->_log)
        {
            // Set the name of the log file
            $filename = $this->_log;

            if ( ! file_exists($filename))
            {
                // Create the log file
                file_put_contents($filename, '');

                // Allow anyone to write to log files
                chmod($filename, 0666);
            }

            // Write the message into the log file
            // Format: time --- type: message
            file_put_contents($filename, date($this->_date_format).' --- '.$type.': '.$message.PHP_EOL, FILE_APPEND);
        }
    }

    /**
     * Executes the necessary commands to deploy the website.
     */
    public function execute()
    {
        try
        {
            // Make sure we're in the right directory
            chdir($this->_directory);
            $this->log('Changing working directory... ');

            // Discard any changes to tracked files since our last deploy
            exec('git reset --hard HEAD', $output);
            $this->log('Reseting repository... '.implode(' ', $output));

            // Update the local repository
            exec('git pull '.$this->_remote.' '.$this->_branch, $output);
            $this->log('Pulling in changes... '.implode(' ', $output));

            // Secure the .git directory
            exec('chmod -R og-rx .git');
            $this->log('Securing .git directory... ');

            if (is_callable($this->post_deploy))
            {
                call_user_func($this->post_deploy, $this->_data);
            }

            $this->log('Deployment successful.');
        }
        catch (Exception $e)
        {
            $this->log($e, 'ERROR');
        }
    }

}

// This is just an example
$deploy = new Deploy('/var/www/site-name/repo-name');

$deploy->execute();

?>

3 回答

  • 1

    我正在研究这个,你可以做的最好的方法就像我提到通过构建一个PHP脚本并运行shell_exec('whoami')来找出apache服务器正在使用的用户是什么 . 并在浏览器上运行以查看该用户 .

    然后在您的网站的文档根目录上,每个示例是/ var / www,当您这样做时,您需要为此目录创建shh密钥,同时添加一个包含主机bitbucket的配置文件并引用您创建的密钥 .

    将密钥添加到bitbucket

    那么你需要为apache添加权限来运行git命令运行命令visudo并添加:
    yourapacheuser ALL=(yourapacheuser) NOPASSWD: /usr/bin/

    在我的情况下#the / usr / bin是我安装git的地方所以你需要看看你安装git的目录路径是什么 .

    之后你应该可以毫无问题地运行你的脚本,如果它抱怨requiretty消息,那么再次添加visudo:默认值:yourapacheuser!requiretty

    希望能帮助到你

  • 0

    你是否为bitbucket中使用的服务器配置了SSH密钥,并使用脚本的url添加webhook?

  • 0

    正如user1361389所写,您需要知道用户正在运行不同的进程 . 这就是我在Amazon Ubuntu实例上的做法 .

    我有一个调用bash脚本的php文件: shell_exec("sudo -u ubuntu /home/ubuntu/gitpull.sh");

    为用户ubuntu创建SSH密钥,并将公钥上传到bitbucket .

    此外,请确保服务器上的php文件归正确的用户所有 . 在我的情况下ubuntu .

    然后,您需要在调用php文件时模拟ubuntu以部署代码 . 在sudoer文件中添加此行( >sudo visudo

    www-data ALL=(ubuntu) NOPASSWD: /path/to/gitpull.sh

    然后在bitbucket中将URL添加到您的钩子 .

相关问题