Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare matrix<->Composer versions, maybe exit #73

Conversation

BrianHenryIE
Copy link
Member

@BrianHenryIE BrianHenryIE commented Sep 26, 2023

This exits a workflow run (i.e. an instance of a matrix run) if the PHP version in composer.json is higher than the matrix's PHP version.

I'm not certain what the result will say on the PR runs – the overall workflow has a "success" green tick, and the individual skipped runs have the red failure x. The failure email does not get sent.

That might not work exactly as desired, but there doesn't seem to be an early-exit command – note the 750+ likes for that feature request! The REST API and gh run cancel use github.run_id which is the workflow id rather than github.run_number which is the matrix run instance, so cancelling the PHP 5.6 run cancels all of them.

Screenshot 2023-09-26 at 1 09 37 PM Screenshot 2023-09-26 at 1 09 04 PM
@BrianHenryIE BrianHenryIE requested a review from a team as a code owner September 26, 2023 20:34
@BrianHenryIE
Copy link
Member Author

Note the difference between the failed workflow in dist-archive-command:

Screenshot 2023-09-26 at 1 46 20 PM

.

and the succeeded-with-individual-failures as this behaved:

Screenshot 2023-09-26 at 1 46 59 PM
- name: Get composer.json PHP version
id: parse-composer
run: |
echo "COMPOSER_PHP_VERSION=$(jq '.require.php' composer.json | sed 's/[^0-9.|]//g' | jq -Rc 'split("|") | min | tonumber')" >> $GITHUB_OUTPUT;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thought I had was that we should use composer show or composer check-platform-reqs to have composer reconcile the minimum PHP version from all of the dependencies.

However, we could end up in an (unlikely) scenario where the minimum PHP version is sufficiently high, causes all of the tests to be skipped, and the entire build passes without running any tests.

I kinda feel like we should read a MINIMUM_TEST_PHP_VERSION variable from a .env file so it's:

  1. Explicitly defined.
  2. Entirely outside of Composer.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel strongly about this but since it was already defined in composer.json I used that.
I think the with: ... minimum-php: '7.1' (wp-cli/dist-archive-command@d2adeed) is a better approach than adding a .env file – the minimum-php value can be read before the actions/checkout step, and there is no existing .env file in the repo so I'd hesitate to add it just for this.

Aside, I have been thinking a bit about reading the minimum PHP version from composer.lock, since I often update dependencies while on PHP 8 but have 7.4 in my plugin headers – it'd be a nice automated check before running dist-archive-command.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@schlessera @swissspidy Do either of you have a preference?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I'm open to using the require.php in composer.json. We can treat that as the explicit "minimum PHP version".

- name: Get composer.json PHP version
id: parse-composer
run: |
echo "COMPOSER_PHP_VERSION=$(jq '.require.php' composer.json | sed 's/[^0-9.|]//g' | jq -Rc 'split("|") | min | tonumber')" >> $GITHUB_OUTPUT;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I'm open to using the require.php in composer.json. We can treat that as the explicit "minimum PHP version".

echo "COMPOSER_PHP_VERSION=$(jq '.require.php' composer.json | sed 's/[^0-9.|]//g' | jq -Rc 'split("|") | min | tonumber')" >> $GITHUB_OUTPUT;

- name: Exit early if matrix PHP version lower than composer.json required PHP version
run: if [[ $(bc -l <<< "$COMPOSER_PHP_VERSION > $MATRIX_PHP_VERSION") -eq 1 ]]; then exit 1; fi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
run: if [[ $(bc -l <<< "$COMPOSER_PHP_VERSION > $MATRIX_PHP_VERSION") -eq 1 ]]; then exit 1; fi
run: if [[ $(bc -l <<< "$COMPOSER_PHP_VERSION > $MATRIX_PHP_VERSION") -eq 1 ]]; then exit 0; fi

Is this a horrible idea?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That change would not exit the job (matrix instance run) – return 0 (exit success) is the same as any command being run in that step returning 0, and GitHub Actions continues to the next step on success. We have to return failure to exit the job.

@BrianHenryIE
Copy link
Member Author

Closing in favour of a cleaner approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants