How to clear the OPcache on a BasicAuth protected environment with typo3/surf?

243 views Asked by At

On a TYPO3 project I'm working the Production/Staging (or Production/Dev, or any other) environment is protected by HTTP BasicAuth (basic access authentication).

The instance get's deployed via typo3/surf.

  1. At some point typo3/surf must create a temporary php file, which can be accessed
  2. later: after the switch was done and the new deployment is reachable via the frontend.

How can I configure typo3/surf to access the previously generated OPcache clearing script via the frontend on a BasicAuth protected environment?

1

There are 1 answers

0
Josef Glatz On

Configuring typo3/surf to reset the PHP OPcache1

Four steps are basically necessary to configure the OPcache clearing/reset script:

  1. Set task options for \TYPO3\Surf\Task\Php\WebOpcacheResetCreateScriptTask
  2. Add task \TYPO3\Surf\Task\Php\WebOpcacheResetCreateScriptTask the an early stage (e.g. package but definitely before transfer)
  3. Set task options for \TYPO3\Surf\Task\Php\WebOpcacheResetExecuteTask
  4. Add task \TYPO3\Surf\Task\Php\WebOpcacheResetExecuteTask after stage switch

Here are the necessary snippets for your onInitialize function2 within your deployment configuration script:

Set task options for the "Create Script Task":

Since the "Respect WebDirectory" patch, the path to the script must not to be configured manually as it automatically uses the right WebDirectory path (which is set via options beforhand).

If you are using an older typo3/surf version or you have any special requirement you can set option scriptBasePath to set the absolute path to the resulting file:

# In this example, I have to set the absolute path for the resulting php file.
# Since the deployment run in GitLab CI I get the path to the root of the project's GIT
# repository via the environment variable `CI_PROJECT_DIR`. Since the path to the webDirectory
# inside the GIT repository is `<GitRepoRootFOlder>/app/web` I add it manually and concatenate
# it as final string for the option `scriptBasePath`:
$workflow->setTaskOptions(\TYPO3\Surf\Task\Php\WebOpcacheResetCreateScriptTask::class, [
    'scriptBasePath' => \TYPO3\Flow\Utility\Files::concatenatePaths([getenv('CI_PROJECT_DIR'), '/app/web']),
]);

Set task options for the "Execute Task":

At this point, we provide username and password

$workflow->setTaskOptions('TYPO3\\Surf\\Task\\Php\\WebOpcacheResetExecuteTask', [
    'baseUrl' => $application->getOption('baseUrl'),
    'stream_context' => [
        'http' => [
            'header' => 'Authorization: Basic '.base64_encode("username:password"),
        ],
    ],
]);

Activate both Tasks:

$workflow->beforeStage('transfer', \TYPO3\Surf\Task\Php\WebOpcacheResetCreateScriptTask::class, $application)
    ->afterStage('switch', \TYPO3\Surf\Task\Php\WebOpcacheResetExecuteTask::class, $application);

This answer shows only the necessary parts for the OPcache reset process!

Please check also the TYPO3 CMS deployment configuration example in the official documentation.


Footnotes

1 This answer is based on typo3/surf GIT branch dev-master, version 2.x

2 Example where to place the mentioned snippets:

$deployment->onInitialize(function () use ($deployment, $application) {
    /** @var SimpleWorkflow $workflow */
    $workflow = $deployment->getWorkflow();

    # the mentioned snippets have to be placed next to your existing configuration

});