Goal:
When you push to the main branch in Bitbucket →
Bitbucket Pipelines automatically deploys your Magento 2 code → clears caches → purges Cloudflare cache.
🧱 1. Enable Bitbucket Pipelines
In your Bitbucket repository:
Go to Repository Settings → Pipelines → Enable Pipelines.
Add a
bitbucket-pipelines.ymlfile in your repo root.
🧩 2. Example Pipeline for Magento 2 + Cloudflare
Here’s a practical example:
⚙️ 3. Add Environment Variables (Repository Settings → Pipelines → Variables)
| Variable | Example | Description |
|---|---|---|
SSH_PRIVATE_KEY | (your private key contents) | For deployment via SSH |
SERVER_HOST | example.com | Your Magento server hostname |
SERVER_USER | deploy | SSH user |
SERVER_PATH | /var/www/html | Magento root directory |
CLOUDFLARE_API_TOKEN | (Cloudflare token) | Scoped to cache purge |
CLOUDFLARE_ZONE_ID | abcd1234efgh5678 | Your Cloudflare zone ID |
🔐 4. Create Cloudflare API Token
Create a custom token with:
Permissions:
Zone.Cache Purge→ Edit
Zone Resources:
Include → your specific domain
Copy the token and store it in Bitbucket as
$CLOUDFLARE_API_TOKEN.
🧹 5. Optional: Selective Cache Purge
Instead of purging everything (slower, risky for traffic), you can purge by URL or file type:
You can automate this by detecting changed files in Bitbucket using:
⚡ 6. Optimization Tips
Use Cloudflare Cache Rules to avoid purging everything too often.
Use Magento’s Cloudflare extension if you want admin-triggered purge from backend.
Add a staging branch in Pipelines (deploy to staging server instead of live).
For large static deployments, use rsync + exclude to minimize upload time.
