Wednesday, March 2, 2022

[FIXED] GitHub Actions - Composer failing because of PHP Version constraint

Issue

I'm trying to get my unit-tests to run via GitHub Actions on a pull_request.

I can see the actions running when I update my PR but the PHP version doesn't update when it tries to composer install my project.

Command: composer install --no-interaction --no-suggest --no-progress
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - This package requires php ~7.3.0 but your PHP version (7.4.10) does not satisfy that requirement.

I've tried multiple different workflow files, and this was the one that gave me the most control, but it still seems to be running on php 7.4.

name: Run Tests

on:
  pull_request:
    branches:
      - master

jobs:
  build:
    runs-on: ${{ matrix.operating-system }}
    strategy:
      matrix:
        operating-system: [ubuntu-latest]
        php-versions: ['7.3']

    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2
      - uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php-versions }}
      - uses: php-actions/composer@v2
      - uses: php-actions/phpunit@v8

Finally, here is the snippet from my composer json that shows the constraint:

"require": {
    "php": "~7.3.0",
    "guzzlehttp/guzzle": "^7.0"
},
"require-dev": {
    "phpunit/phpunit": "^8"
}

Solution

You can force the PHP version for the composer action by adding in your .github/workflows/YOURACTION.yaml

  - name: Build Composer
    uses: php-actions/composer@v5
    with:
      ssh_key: ${{ secrets.ssh_key }}
      ssh_key_pub: ${{ secrets.ssh_key_pub }}
      php_version: 7.2

Notice I did it on the Composer step and not on the shivammathur/setup-php@v2 Try removing this step all together, u might not need it at all.

See more here: https://github.com/marketplace/actions/composer-php-actions



Answered By - Itay Moav -Malimovka

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.