Yoast / yoast-acf-analysis

WordPress plugin that adds the content of all ACF fields to the Yoast SEO score analysis.
https://wordpress.org/plugins/acf-content-analysis-for-yoast-seo/
GNU General Public License v3.0
62 stars 20 forks source link

CI: switch to GitHub Actions #301

Closed jrfnl closed 2 years ago

jrfnl commented 2 years ago

Summary

This PR can be summarized in the following changelog entry:

Relevant technical choices:

👉🏻 This PR will be easiest to review by reviewing each commit individually.

CI: switch to GitHub Actions - step 1: code style

This commit:

Notes:

  1. Builds will run on all pushes and on pull requests, except pushes to master.
  2. Builds can also be manually triggered. Note: manual triggering of builds has to be explicitly allowed. This is not a feature which is enabled by default.
  3. If a previous GH actions run for the same branch hadn't finished yet when the same branch is pushed again, the previous run will be cancelled. In Travis, this was an option on the "Settings" page - "Auto cancellation" -, which was turned on for most, if not all, Yoast repos. The concurrency configuration in the GHA script emulates the same behaviour.
  4. Composer dependency downloads will be cached for faster builds using a predefined GH action specifically created for this purpose. The alternative would be to handle the caching manually, which would add three extra steps to the script. Note: Caching works differently between Travis and GH Actions. On GH Actions, once a cache has been created, it can't be updated. It can only be replaced by a new cache with a different key. As the PHP version, the composer.json and a potential composer.lock hash are all part of the key used by the above mentioned action, this difference should not have a significant impact. Ref: https://docs.github.com/en/actions/advanced-guides/caching-dependencies-to-speed-up-workflows
  5. The CS check will display the results in the actions script output log, as well as display any violations found inline in the GitHub code view using the cs2pr tool.

Differences with the Travis implementation:

CI: switch to GitHub Actions - step 2: security check

This commit:

Notes:

  1. Builds will run on all pushes and on pull requests.
  2. In addition to this, this workflow will run automatically every Monday at 6am against the default branch via a cron job. This is especially relevant for repositories which are not actively receiving PRs every week. Notes about workflows with cron jobs:
    1. If a repository is forked, workflows will also run in the fork. This means that with a cron job, those will also be run on forks. As that is not the intention, I've added a condition to prevent the cron job from running on forks, while still allowing the workflow to run on forks for other events. Note: this only applies to pre-existing forks. For new forks, scheduled jobs are disabled by default.
    2. There is one big downside to using cron jobs in workflows and that is that those workflows will automatically be disabled after 60 days of inactivity in a repo. In this context, an "inactive" repo means that there have been no commits to the repo within the last 60 days. The repo owner (organisation owner) will receive an email notification a few days before this is about to happen and can prevent the workflow from being disabled, but that does mean that for repos with low activity, this workflow will need to be kept active by acting on the email once every two months. If the workflow would still happen to get disabled, it can be re-enabled by anyone with commit access on the "Actions" -> "Security Check" page of the repo. Refs:
  3. Builds can also be manually triggered. Note: manual triggering of builds has to be explicitly allowed. This is not a feature which is enabled by default.
  4. If a previous GH actions run for the same branch hadn't finished yet when the same branch is pushed again, the previous run will be cancelled. In Travis, this was an option on the "Settings" page - "Auto cancellation" -, which was turned on for most, if not all, Yoast repos. The concurrency configuration in the GHA script emulates the same behaviour.
  5. The security check is run via a predefined action from the same maintainers as the security check tool which was previously used. In the documentation, it is suggested to potentially cache the vulnerabilities database, but knowing how caching works on GHA, I'm not so sure that's a good idea as in that case, chances of checking against an outdated database are high. Ref: https://github.com/marketplace/actions/the-php-security-checker

Differences with the Travis implementation:

CI: switch to GitHub Actions - step 3: linting

This commit:

Notes:

  1. Builds will run on:
    • Select pushes using branch filtering similar to before.
    • All pull requests.
    • When manually triggered. Note: manual triggering of builds has to be explicitly allowed. This is not a feature which is enabled by default.
  2. If a previous GH actions run for the same branch hadn't finished yet when the same branch is pushed again, the previous run will be cancelled. In Travis, this was an option on the "Settings" page - "Auto cancellation" -, which was turned on for most, if not all, Yoast repos. The concurrency configuration in the GHA script emulates the same behaviour.
  3. The default ini settings used by the setup-php action are based on the php.ini-production configuration. This means that error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT, display_errors is set to Off and zend.assertions is set to -1 (= do not compile). For the purposes of CI, especially for linting and testing code, I'd recommend running with zend.assertions=-1,error_reporting=-1anddisplay_errors=Onto ensure **all** PHP notices are shown. Note: the defaults will be changed in the next major ofsetup-phpto be based on thephp.ini-develop` configuration, but that may still be a while off. Refs:
    • shivammathur/setup-php#450
    • shivammathur/setup-php#469
  4. While the setup-php action offers the ability to install the PHAR file for Parallel Lint, I've elected not to use that option as it would mean that we would not be able to use the composer lint script in the workflow, which would mean that the CLI arguments would have to be duplicated between the composer.json file and the lint.yml file. IMO, that would make this a typical point of failure where updates would be done in one, but not the other. If, at some point in the future, the Parallel Lint tool would start to support a config file for the CLI arguments, removing this point of failure, this choice can be (and should be) revisited.
  5. Composer dependency downloads will be cached for faster builds using a predefined GH action specifically created for this purpose. The alternative would be to handle the caching manually, which would add three extra steps to the script. Note: Caching works differently between Travis and GH Actions. On GH Actions, once a cache has been created, it can't be updated. It can only be replaced by a new cache with a different key. As the PHP version, the composer.json and a potential composer.lock hash are all part of the key used by the above mentioned action, this difference should not have a significant impact. Ref: https://docs.github.com/en/actions/advanced-guides/caching-dependencies-to-speed-up-workflows
  6. The Linting check will display the results inline in the GitHub code view using the cs2pr tool.

Differences with the Travis implementation:

CI: switch to GitHub Actions - step 4: javascript checks

This commit:

Notes:

  1. Builds will run on:
    • Select pushes using branch filtering similar to before.
    • All pull requests.
    • When manually triggered. Note: manual triggering of builds has to be explicitly allowed. This is not a feature which is enabled by default.
  2. If a previous GH actions run for the same branch hadn't finished yet when the same branch is pushed again, the previous run will be cancelled. In Travis, this was an option on the "Settings" page - "Auto cancellation" -, which was turned on for most, if not all, Yoast repos. The concurrency configuration in the GHA script emulates the same behaviour.
  3. The standard Ubuntu 20 image used by this action comes preloaded with Node, NPM and Yarn, so - in contrast to Travis - those don't need to be set up explicitly via script commands. Ref: https://github.com/actions/virtual-environments/blob/main/images/linux/Ubuntu2004-README.md
  4. The build still uses the setup-node action as it allows for caching of the yarn dependencies for faster builds. As no node-version key is set/passed, the default Node version on the Ubuntu image is used for Node itself. Ref: https://github.com/actions/setup-node
  5. Includes a "debug" step which will show the versions of the underlying tooling used, which should come in handy when the build would fail unexpectedly.

Differences with the Travis implementation:

Additional observations:

CI: switch to GitHub Actions - step 5: PHP unit tests

This commit:

Notes:

  1. Builds will run on:
    • Select pushes using branch filtering similar to before.
    • All pull requests.
    • When manually triggered. Note: manual triggering of builds has to be explicitly allowed. This is not a feature which is enabled by default.
  2. If a previous GH actions run for the same branch hadn't finished yet when the same branch is pushed again, the previous run will be cancelled. In Travis, this was an option on the "Settings" page - "Auto cancellation" -, which was turned on for most, if not all, Yoast repos. The concurrency configuration in the GHA script emulates the same behaviour.
  3. The default ini settings used by the setup-php action are based on the php.ini-production configuration. This means that error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT, display_errors is set to Off and zend.assertions is set to -1 (= do not compile). For the purposes of CI, especially for linting and testing code, I'd recommend running with zend.assertions=-1,error_reporting=-1anddisplay_errors=Onto ensure **all** PHP notices are shown. Note: the defaults will be changed in the next major ofsetup-phpto be based on thephp.ini-develop` configuration, but that may still be a while off. Refs:
    • shivammathur/setup-php#450
    • shivammathur/setup-php#469
  4. While the setup-php action offers the ability to install the PHAR file for PHPUnit, I've elected not to use that option as we need to do a composer install anyway to get the other dependencies, like WP Test Utils.
  5. Composer dependency downloads will be cached for faster builds using a predefined GH action specifically created for this purpose. The alternative would be to handle the caching manually, which would add three extra steps to the script. Note: Caching works differently between Travis and GH Actions. On GH Actions, once a cache has been created, it can't be updated. It can only be replaced by a new cache with a different key. As the PHP version, the composer.json and a potential composer.lock hash are all part of the key used by the above mentioned action, this difference should not have a significant impact. Ref: https://docs.github.com/en/actions/advanced-guides/caching-dependencies-to-speed-up-workflows
  6. As ACF only contains BrainMonkey based unit tests, we can (and should) use the most appropriate PHPUnit/BrainMonkey/Mockery/etc version for the PHP version the tests are being run on. Most notably, this is actually needed to get the tests running on PHP 8.0 and higher. As there is a committed composer.lock file and the PHPUnit version is locked at PHPUnit 5.x, this means we have to do an on-the-fly update of the PHPUnit version depending on the PHP version of the current build. As we still also want to benefit from the Composer caching which the ramsey/composer-install action sets up, I've done some nifty trickery with the options passed to the composer-install action to get that working.
    • The dependency-versions: "highest" basically changes the command used by the action from composer install to composer update, however we don't want to update all dependencies as run-time dependencies should remain locked at their original version.
    • To that end, I'm passing additional composer-options which will limit the update to just and only the test dependencies.
    • This update also explicitly ignores PHP platform requirements as the composer.json file contains an explicit "php": "5.6.40" platform setting, which would otherwise block the update. Alternatively, the platform requirement could be removed on the fly for CI only.
    • This update is only run for PHP 7.3 and higher, which means that the test runs on PHP 7.3 and higher will be run on PHPUnit 9.x.

Differences with the Travis implementation:

Test instructions

These workflows have been extensively tested already and need no further testing.

Aside from the builds run for this PR, you can find the results of various specific tests also on the "Actions" page. For each job, it has been verified that the build(s) will actually show as failed when code has been altered to induce a fail condition.

The commits used to test and demonstrate that the workflows exit as successful and fail as unsuccessful when appropriate, can be examined in this temporary branch: https://github.com/Yoast/yoast-acf-analysis/commits/TEMP/2146981 (That branch will be deleted once this PR has been merged)

diedexx commented 2 years ago

Nice, Thanks @jrfnl!