Overview: Cloudflare + Bitbucket + Magento 2 CI/CD Pipeline

By | October 15, 2025
Spread the love

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:

  1. Go to Repository Settings → Pipelines → Enable Pipelines.

  2. Add a bitbucket-pipelines.yml file in your repo root.


🧩 2. Example Pipeline for Magento 2 + Cloudflare

Here’s a practical example:

image: php:8.1

pipelines:
  branches:
    main:
      - step:
          name: Deploy to Production
          caches:
            - composer
          script:
            # 1️⃣ Install dependencies
            - apt-get update && apt-get install -y rsync git unzip ssh

            # 2️⃣ Set up SSH
            - mkdir -p ~/.ssh
            - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
            - chmod 600 ~/.ssh/id_rsa
            - ssh-keyscan -H $SERVER_HOST >> ~/.ssh/known_hosts

            # 3️⃣ Sync files to the production server
            - rsync -avz --delete --exclude='.git' ./ $SERVER_USER@$SERVER_HOST:$SERVER_PATH

            # 4️⃣ Run Magento commands on the server
            - ssh $SERVER_USER@$SERVER_HOST << 'EOF'
                cd $SERVER_PATH
                php bin/magento maintenance:enable
                php bin/magento setup:upgrade
                php bin/magento setup:di:compile
                php bin/magento setup:static-content:deploy -f
                php bin/magento cache:flush
                php bin/magento maintenance:disable
              EOF

            # 5️⃣ Purge Cloudflare cache
            - curl -X POST "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/purge_cache" \
                -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
                -H "Content-Type: application/json" \
                --data '{"purge_everything":true}'

 

⚙️ 3. Add Environment Variables (Repository Settings → Pipelines → Variables)

VariableExampleDescription
SSH_PRIVATE_KEY(your private key contents)For deployment via SSH
SERVER_HOSTexample.comYour Magento server hostname
SERVER_USERdeploySSH user
SERVER_PATH/var/www/htmlMagento root directory
CLOUDFLARE_API_TOKEN(Cloudflare token)Scoped to cache purge
CLOUDFLARE_ZONE_IDabcd1234efgh5678Your Cloudflare zone ID

🔐 4. Create Cloudflare API Token

  1. Go to Cloudflare Dashboard → My Profile → API Tokens

  2. Create a custom token with:

    • Permissions:

      • Zone.Cache PurgeEdit

    • Zone Resources:

      • Include → your specific domain

  3. 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:

curl -X POST "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/purge_cache" \
  -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  -H "Content-Type: application/json" \
  --data '{"files":["https://example.com/static/version123/frontend.css","https://example.com/js/script.js"]}'

You can automate this by detecting changed files in Bitbucket using:

git diff --name-only $BITBUCKET_COMMIT_RANGE

⚡ 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.