使用GitHub PHP Composer和Bash进行自动部署



让Composer通过PHP在部署服务器上运行时遇到问题。我已经(或将要)使用GitHub设置了一个webhook,它会通知我并接受拉取请求,然后自动部署到适当的服务器。

问题来自bash行(cd ${localFolder} && composer install --no-ansi --no-dev --no-interaction --no-progress --no-scripts --optimize-autoloader),它没有给出任何错误或输出,就好像从未调用过一样。脚本中的每一行都运行良好,即使是在顽固的一行之后的命令也是如此。我还尝试过在没有任何选项的情况下运行该脚本,并使用硬编码的文件夹位置来获得完全相同的结果。

当我尝试通过在shell中执行sudo -H -u www-data bash -c 'echo "./download_project.sh 12345678912345 git_branch git_repo'来测试该命令时,它运行良好并完成。

PHP代码:

$data = json_decode(utf8_encode(file_get_contents("data.json")));
$command = "/var/www/download_project.sh" . " " . uniqid() . " " . $data->deployTable[$deploy]->branch . " " . $data->deployTable[$deploy]->repository;
echo ($command . "</br>");
echo shell_exec($command);

Bash代码:

#! /bin/bash
if [ "$#" -ne 3 ]; then
    echo "Usage: $(basename $0) [guid] [branch] [repository]"
    exit 1
fi
working="/var/www/"
guid=$1
branch=$2
repository=$3
localFolder="${working}temp/"
localFile="${working}temp.zip"
#clone the appropriate branch and repository to the /var/www/temp folder
#result: functions as expected
git clone -b $branch git@github.com:ORGANIZATION/$repository.git $localFolder
#moves the sql folder out of /var/www/temp and onto /var/www to be used for db creation and/or migration
#result: functions as expected
mv "${localFolder}sql" "${working}sql"
#removes /var/www/temp/doc as it is not needed or wanted for production
#result: functions as expected
rm -rf "${localFolder}doc"
#downloads all dependencies  into the /var/www/temp/vedor folder
#result: nothing happens
(cd ${localFolder} && composer install --no-ansi --no-dev --no-interaction --no-progress --no-scripts --optimize-autoloader)

注意:GUID将来将用于存档目的。

我真的建议将cd && composer install从强制它进入的子shell中移出(读取:移除封闭的())。

其次,我会临时修改shebang以读取#!/bin/bash +x,这样您就可以在某种调试模式下运行脚本。

然后向我们展示跑步的结果。

相关内容

  • 没有找到相关文章

最新更新