justinwhall / wp-buildhook-deploy

WordPress plugin to trigger build hooks and deploy your static site.
https://justinwhall.com
163 stars 28 forks source link

Netlify build hook gets called twice #16

Open cniklas opened 4 years ago

cniklas commented 4 years ago

I really love this plugin due to its simplicity. But there is one problem I can't solve.

I set generated a build hook in Netlify. I entered that build hook in the plugin's settings. I created a page in WordPress and set "Publish to: production" to be true. Now whenever I update this page in WordPress (edit something and click "Update"), Netlify builds my page twice (see screenshot). In the deployment section I can see that the deploy was triggered by the build hook twice within a few seconds. This happens everytime.

Netlify deploy log

Why is that so? How can I fix it?

bybyers commented 2 years ago

Any updates on this?

justinwhall commented 2 years ago

I assume you are using Gutenberg as it sends two requests to save a document, thus firing that webhook request twice. That action likely needs to be updated with new logic. PRs welcome.

cniklas commented 2 years ago

Yes, I (have to) use Gutenberg. Thanks for pointing out where the problem is.

Here is my workaround: The build hook doesn't trigger Netlify directly but instead a PHP app. Every time that app is called it writes a new entry to a log file and fires the real Netlify build hook – but only if the log file had not been changed within the last 5 seconds. That's quite an ugly solution but helped me solving the problem with creating too many build requests.

$File = new File(TMP . 'logs' . DS . 'build.log');
if (!$File->exists() || $File->lastChange() < strtotime('-5 seconds')) {
    CakeLog::write('netlify', 'go');

    $HttpSocket = new HttpSocket();
    $HttpSocket->post('https://api.netlify.com/build_hooks/MY_BUILD_HOOK', '{}');
}
bybyers commented 2 years ago

@cniklas Where did you add this to?

cniklas commented 2 years ago

@cniklas Where did you add this to?

It's a separate PHP installation located on the same domain as the WordPress site., and made with CakePHP 2 (which is outdated, but you can use any framework you like or even do it with plain PHP). There I have a super simple controller with a single action:

App::uses('AppController', 'Controller');
App::uses('HttpSocket', 'Network/Http');
App::uses('File', 'Utility');

class JobsController extends AppController {

    public function iijdiyqhtmucsnayzkbkttkc()
    {
        $this->request->allowMethod('post');

        $File = new File(TMP . 'logs' . DS . 'build.log');
        if (!$File->exists() || $File->lastChange() < strtotime('-5 seconds')) {
            $HttpSocket = new HttpSocket();
            $HttpSocket->post('https://api.netlify.com/build_hooks/blablabla', '{}');
        }

        $this->autoRender = false;
    }
}

Then I point the WP Buildhook to: https://mydomain/directory-of-cake2app/jobs/iijdiyqhtmucsnayzkbkttkc and that's it.