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

Enhancement: Run phpstan on GitHub Actions #28

Merged
merged 1 commit into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,39 @@ on:
- master

jobs:
hello:
name: Hello
static-analysis:
name: Static Analysis

runs-on: ubuntu-latest

strategy:
matrix:
php-version:
Copy link
Contributor Author

@localheinz localheinz Aug 3, 2020

Choose a reason for hiding this comment

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

By introducing a matrix strategy here, we can reference the resolved values using ${{ matrix.php-version }}.

Since we only run the static analysis for a single PHP version, it might seem that it does not make a lot of sense.

However, in my experience, it's quite useful, as we can reference the ${{ matrix.php-version }} in multiple places without having to hard-code the actual PHP version.

Also note how the matrix values are appended to the job name:

Screen Shot 2020-08-03 at 19 11 27

💁‍♂️ For reference, see:

- 7.2
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we decide to run the static analysis on PHP 7.4, we only need to change the value here.


steps:
- name: Echo a greeting
run: echo 'Hello, GitHub Actions!'
- name: Checkout
uses: actions/checkout@v2
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In order to do something with the code, we need to check out the repository first.

💁‍♂️ For reference, see:


- name: Install PHP
uses: shivammathur/setup-php@v2
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could use one of the PHP binaries that are natively available instead.

However, these are currently limited to:

  • php (PHP 7.4.8)
  • php7.1 (PHP 7.1.33)
  • php7.2 (PHP 7.2.32)
  • php7.3 (PHP 7.3.20)
  • php7.4 (PHP 7.4.8)

Additionally, a lot of extensions are installed, but not all (!) of them.

Instead, a lot of people use shivammathur/setup-php to set up a wider (!) range of PHP versions, along with code coverage (if need be) and desired extensions.

Shivam Mathur does a great job in maintaining this action, and is very responsive.

💁‍♂️ For reference, see:

with:
coverage: none
php-version: ${{ matrix.php-version }}

- name: Determine composer cache directory
id: determine-composer-cache-directory
run: echo "::set-output name=directory::$(composer config cache-dir)"
Copy link
Contributor Author

@localheinz localheinz Aug 3, 2020

Choose a reason for hiding this comment

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

Steps can have outputs - for example, it is possible to define output parameters (like here) or environment variables.

Here we determine the composer cache directory, instead of hard-coding it, so we can reference it later.

💁‍♂️ For reference, see:


- name: Cache dependencies installed with composer
uses: actions/cache@v2
Copy link
Contributor Author

Choose a reason for hiding this comment

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

With actions/cache we can cache arbitrary directories or files between jobs.

💁‍♂️ For reference, see:

with:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using the with key, we can specify a map of parameter names to values that are accepted by an action.

💁‍♂️ For reference, see:

path: ${{ steps.determine-composer-cache-directory.outputs.directory }}
Copy link
Contributor Author

@localheinz localheinz Aug 3, 2020

Choose a reason for hiding this comment

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

Here we pick up the output parameter again to specify a value for the path input parameter.

Notice how we use the step identifier determine-composer-cache-directory to reference the value of the directory output of the corresponding step.

💁‍♂️ For reference, see:

key: php-${{ matrix.php-version }}-composer-${{ hashFiles('composer.json') }}
Copy link
Contributor Author

@localheinz localheinz Aug 3, 2020

Choose a reason for hiding this comment

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

Here we specify a value for the key input parameter, the cache key to use for caching the contents of the composer cache directory.

Notice how we use the ${{ matrix.php-version }} expression which resolves to the corresponding value in the build matrix. Here it will resolve to 7.2.

Also, we use the hashfiles() function to calculate a hash from composer.json as part of the cache key.

💁‍♂️ For reference, see:

restore-keys: php-${{ matrix.php-version }}-composer-
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here we can list one or more cache keys.

Note that the inputs for an action need to be strings. If we want to specify more items, we need to use a multi-line string:

diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml
index e7b00a0..07b7e54 100644
--- a/.github/workflows/build.yaml
+++ b/.github/workflows/build.yaml
@@ -38,7 +38,9 @@ jobs:
         with:
           path: ${{ steps.determine-composer-cache-directory.outputs.directory }}
           key: php-${{ matrix.php-version }}-composer-${{ hashFiles('composer.json') }}
-          restore-keys: php-${{ matrix.php-version }}-composer-
+          restore-keys: |
+            php-${{ matrix.php-version }}-composer-
+            foo-bar-baz

       - name: Install highest dependencies from composer.json
         run: composer install --no-interaction

Also note that the value for the key input is automatically used as a key, so here we only specify additional, optional keys (or prefixes).

💁‍♂️ For reference, see:


- name: Install dependencies
run: composer install --no-interaction

- name: Run phpstan/phpstan
run: vendor/bin/phpstan analyse
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ script: ./vendor/bin/phpunit

jobs:
include:
- stage: Static analysis
script: vendor/bin/phpstan analyse

- stage: Coding standards
script: ./vendor/bin/php-cs-fixer fix -v --diff --dry-run

Expand Down