我试图在我的持续集成系统中设置一个Webhook,以便当我在Git上推送内容时,我的部署服务器可以自动部署。
我用Shell脚本来做到这一点,我可以使用Apache的CGI脚本来访问它。
那很好,唯一的问题是我脚本的标准输出在错误日志中被重定向,而不是HTTP响应...
我的脚本位于自定义文件夹中,并且在/var/www/cgi-bin
中有这样的符号链接:
lrwxrwxrwx 1 root root 21 Nov 29 20:52 deploy -> /srv/myapp/deploy.sh
部署脚本包含:
#!/bin/bash
base_path="/srv/myapp"
echo "Content-type: text/plain"
echo ""
cd $base_path
sudo docker-compose stop
sudo docker-compose pull
sudo docker-compose up -d
sudo docker system prune -f
exit 0
我的sudoers文件包含:
apache ALL = NOPASSWD: /usr/bin/docker-compose
错误日志包含:
[Sun Dec 02 11:01:31.748772 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: Stopping myapp_http_1_a3ca0bdbd648 ... \r: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:31.749003 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: Stopping myapp_client_1_cb16b60351f0 ... \r: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:31.749151 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: Stopping myapp_server_1_6c148be61c2c ... \r: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:31.749276 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: Stopping myapp_database_1_2959978817cd ... \r: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:32.532484 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[4A\x1b[2K\rStopping myapp_http_1_a3ca0bdbd648 ... \x1b[32mdone\x1b[0m\r\x1b[4B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:42.705754 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[3A\x1b[2K\rStopping myapp_client_1_cb16b60351f0 ... \x1b[32mdone\x1b[0m\r\x1b[3B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:52.911671 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[2A\x1b[2K\rStopping myapp_server_1_6c148be61c2c ... \x1b[32mdone\x1b[0m\r\x1b[2B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:53.105109 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[1A\x1b[2K\rStopping myapp_database_1_2959978817cd ... \x1b[32mdone\x1b[0m\r\x1b[1B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:53.645366 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: Pulling database ... \r: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:53.645577 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: Pulling server ... \r: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:53.645703 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: Pulling client ... \r: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:53.645808 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: Pulling http ... \r: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:53.931960 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[2A\x1b[2K\rPulling client ... pulling from myaccount/myapp\r\x1b[2B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:53.932480 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[2A\x1b[2K\rPulling client ... digest: sha256:06202040641b409706...\r\x1b[2B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:53.932865 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[2A\x1b[2K\rPulling client ... status: image is up to date for g...\r\x1b[2B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:53.933561 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[2A\x1b[2K\rPulling client ... \x1b[32mdone\x1b[0m\r\x1b[2B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:53.955566 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[4A\x1b[2K\rPulling database ... pulling from myaccount/myapp\r\x1b[4B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:53.955944 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[4A\x1b[2K\rPulling database ... digest: sha256:c63765a78a26f72ec7...\r\x1b[4B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:53.956375 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[4A\x1b[2K\rPulling database ... status: image is up to date for g...\r\x1b[4B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:53.956925 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[4A\x1b[2K\rPulling database ... \x1b[32mdone\x1b[0m\r\x1b[4B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:53.964085 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[1A\x1b[2K\rPulling http ... pulling from myaccount/myapp\r\x1b[1B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:53.964472 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[1A\x1b[2K\rPulling http ... digest: sha256:4bfc963090f942cba9...\r\x1b[1B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:53.964782 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[1A\x1b[2K\rPulling http ... status: image is up to date for g...\r\x1b[1B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:53.965265 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[1A\x1b[2K\rPulling http ... \x1b[32mdone\x1b[0m\r\x1b[1B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:54.071190 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[3A\x1b[2K\rPulling server ... pulling from myaccount/myapp\r\x1b[3B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:54.071609 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[3A\x1b[2K\rPulling server ... digest: sha256:15dcd5ebba6a815342...\r\x1b[3B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:54.071918 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[3A\x1b[2K\rPulling server ... status: image is up to date for g...\r\x1b[3B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:54.073097 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[3A\x1b[2K\rPulling server ... \x1b[32mdone\x1b[0m\r\x1b[3B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:54.658070 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: Starting myapp_database_1_2959978817cd ... \r: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:55.416923 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[1A\x1b[2K\rStarting myapp_database_1_2959978817cd ... \x1b[32mdone\x1b[0m\r\x1b[1B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:55.417759 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: Starting myapp_server_1_6c148be61c2c ... \r: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:55.824527 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[1A\x1b[2K\rStarting myapp_server_1_6c148be61c2c ... \x1b[32mdone\x1b[0m\r\x1b[1B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:55.825299 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: Starting myapp_client_1_cb16b60351f0 ... \r: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:56.497106 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[1A\x1b[2K\rStarting myapp_client_1_cb16b60351f0 ... \x1b[32mdone\x1b[0m\r\x1b[1B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:56.499781 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: Starting myapp_http_1_a3ca0bdbd648 ... \r: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:57.360969 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: \x1b[1A\x1b[2K\rStarting myapp_http_1_a3ca0bdbd648 ... \x1b[32mdone\x1b[0m\r\x1b[1B: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:57.443070 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: sudo: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:57.443239 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: : : /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:57.443360 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: no tty present and no askpass program specified: /var/www/cgi-bin/deploy
[Sun Dec 02 11:01:57.443434 2018] [cgi:error] [pid 25813] [client 12.34.56.78:51316] AH01215: : /var/www/cgi-bin/deploy
HTTP响应完全为空。
答案 0 :(得分:0)
如果您的标准输出出现标准错误,则错误日志中将包含一个条目:Content-type: text/plain
由于不是这种情况,因此docker命令显然首先会写入标准错误。因此,您可能想要的是将标准错误重定向到标准输出,使其最终出现在浏览器中而不是错误日志中。
在脚本顶部添加以下命令:
exec 2>&1
它将其余脚本的标准错误重定向到标准输出。
如果您只想为单个命令重定向它,请在命令末尾添加它,例如:
sudo docker-compose pull 2>&1