wordpressw3-total-cachewp-cli

Install plugin with wp-cli in a CI environment


I'm trying to install the w3-total-cache plugin in a wordpress docker image with the wp-cli, but it seems to try to connect to the database, even tough I don't try to activate it. In the Dockerfile I install other themes/plugins with composer and everything seems fine, but the only package I found for the w3tc plugin is https://packagist.org/packages/finaldream/w3-total-cache that isn't official and also doesn't seem to be up-to-date (version 0.9.4.6 while w3tc is version 0.14.4).

I can install it with the wp-cli in a machine in the final environment because it has the database, but it seems I can't install in a CI environment to create a docker image with pre-installed plugins, and I haven't found other questions and solutions about it.

Is there a way to install the plugin in a CI environment, without a database? (in the final environment, be it dev, staging, or production, I would only call wp-cli to activate the plugins, as I already do for plugins installed with composer).


Solution

  • I ended up considering that the best option in this case is to curl the zip file from wordpress (because in the end wordpress plugins are mainly directories inside the plugins folder).

    I added the following instructions in the dockerfile:

    ENV W3TC_VERSION 0.14.4
    
    RUN mkdir -p /var/www/html/web/app/plugins \
     && curl -L https://downloads.wordpress.org/plugin/w3-total-cache.${W3TC_VERSION}.zip \
        -o /tmp/w3-total-cache.zip \
     && unzip /tmp/w3-total-cache.zip -d /var/www/html/web/app/plugins \
     && rm /tmp/w3-total-cache.zip \
     && chown -R www-data:www-data /var/www/html/web/app/plugins \
     && cp /var/www/html/web/app/plugins/w3-total-cache/wp-content/advanced-cache.php \
        /var/www/html/web/app/advanced-cache.php \
     && chown www-data:www-data /var/www/html/web/app/advanced-cache.php \
     && mkdir -p /var/www/html/web/app/cache \
     && chown www-data:www-data /var/www/html/web/app/cache \
     && mkdir -p /var/www/html/web/app/w3tc-config \
     && chown www-data:www-data /var/www/html/web/app/w3tc-config
    

    Update (2020-08-20)

    I did as leymannx suggested in the comments and included a instruction in a post install instruction in the composer.json file to call a shell script with the following commands:

    #!/bin/bash
    set -eou pipefail
    
    echo "$(date '+%F %X') Custom install - Start"
    
    W3TC_VERSION=0.14.4
    APP_DIR=/var/www/html/web/app
    PLUGINS_DIR="$APP_DIR/plugins"
    W3TC_URL="https://downloads.wordpress.org/plugin/w3-total-cache.${W3TC_VERSION}.zip"
    
    if [ ! -d "$PLUGINS_DIR/w3-total-cache" ]; then
        mkdir -p "$PLUGINS_DIR"
    
        curl -L "$W3TC_URL" -o /tmp/w3-total-cache.zip
    
        unzip /tmp/w3-total-cache.zip -d "$PLUGINS_DIR"
        rm /tmp/w3-total-cache.zip
    
        chown -R www-data:www-data "$PLUGINS_DIR"
    
        cp "$PLUGINS_DIR"/w3-total-cache/wp-content/advanced-cache.php \
            "$APP_DIR"/advanced-cache.php
        chown www-data:www-data "$APP_DIR"/advanced-cache.php
    
        mkdir -p "$APP_DIR"/cache
        chown www-data:www-data "$APP_DIR"/cache
    
        mkdir -p "$APP_DIR"/w3tc-config
        chown www-data:www-data "$APP_DIR"/w3tc-config
    fi
    
    echo "$(date '+%F %X') Custom install - End"
    

    (I also removed those commands from the Dockerfile)

    This allowed to make the whole process more transparent for whoever uses this wordpress environment.