{
  "name": "MZPanel: purge cache after a deploy",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "mzpanel-deploy-done",
        "responseMode": "lastNode",
        "options": {}
      },
      "id": "db197c2e-2f5c-50ae-87a8-874528273a5e",
      "name": "Deploy finished",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        300
      ],
      "webhookId": "4b20ced1-279a-5f13-aeb4-49992a5308ea"
    },
    {
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "657f27a0-c0dd-5a11-b2d2-bf162d6674bd",
              "name": "apiBase",
              "value": "https://api.mzpanel.com",
              "type": "string"
            },
            {
              "id": "42eed253-cfa0-50bf-9668-479799d46d87",
              "name": "deploySecret",
              "value": "REPLACE-WITH-A-LONG-RANDOM-STRING",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "fd71e4d2-ec17-598a-8683-369dc645d199",
      "name": "Settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Your CI calls this webhook after a deploy with the header X-Deploy-Secret and\n// a JSON body {serverId, domain}. Refuse anything without the shared secret.\nconst s = $('Settings').first().json;\nconst req = $input.first().json;\nconst given = req.headers['x-deploy-secret'] || '';\nif (!s.deploySecret || given !== s.deploySecret) throw new Error('bad or missing X-Deploy-Secret');\nconst { serverId, domain } = req.body || {};\nif (!/^[0-9a-f-]{36}$/.test(serverId || '') || !/^[a-z0-9.-]+$/i.test(domain || '')) throw new Error('body must be {serverId, domain}');\nreturn [{ json: { serverId, domain } }];\n"
      },
      "id": "9b96f15d-28f0-53c5-94dc-516c85bfdf50",
      "name": "Check secret",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $('Settings').first().json.apiBase }}/v1/servers/{{ $json.serverId }}/jobs?wait=60",
        "options": {},
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ type: 'wp.cache', domain: $json.domain, action: 'purge' }) }}"
      },
      "id": "7d919dda-8e7b-57c4-83f0-02549210b8e1",
      "name": "Purge page cache",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        660,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "mzpanel-api-token",
          "name": "MZPanel API token"
        }
      }
    }
  ],
  "connections": {
    "Deploy finished": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Settings": {
      "main": [
        [
          {
            "node": "Check secret",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check secret": {
      "main": [
        [
          {
            "node": "Purge page cache",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false,
    "mzpanel": "Webhook your CI calls after a deploy ({serverId, domain} + X-Deploy-Secret); purges the WordPress page cache with POST /v1/servers/{id}/jobs?wait=60 {type: wp.cache, action: purge} (Operate token) and answers the job result to the caller."
  }
}
