skip to Main Content

I am trying to run testJob in GitLab CI/CD for my (Laravel 11 + React Typescript with Inertiajs) and I keep ending up with the below error. I have ensured that the APP_KEY is set in the CI/CD variables and echoed it to see if it’s consistent with my expectation, and it was. I don’t know where I am missing it.

I did read around related issues here, here, documentation, etc… but I have not availed a solution Can anyone help with this issue?

// Test Job in .gitlab-ci.yml

testJob:
    stage: test
    image: php:latest
    services:
        - name: mysql:latest
    before_script:
        - test -d vendor || { apt-get update && apt-get install -y curl; }
        - test -d vendor || curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
        - test -d vendor || composer install
        - test -d node_modules || { curl -sL https://deb.nodesource.com/setup_16.x | bash - && apt-get install -y nodejs; }
        - test -d node_modules || npm install -g npm@latest
        - touch database/database.sqlite
        - cp .env.example .env
        - echo "APP_ENV=testing" >> .env
        - echo "APP_KEY=$APP_KEY" >> .env
        - php artisan config:clear
        - php artisan migrate
        - php artisan migrate:refresh 
    script:
        - echo "Running PHP Unit Tests"
        - ./vendor/bin/phpunit
        - echo "Running React Tests"
        - npm test
    dependencies:
        - buildJob

// Error and stack trace below:

$ ./vendor/bin/phpunit
PHPUnit 11.1.3 by Sebastian Bergmann and contributors.
Runtime:       PHP 8.3.6
Configuration: /builds/group-kse/indupendo/phpunit.xml
.SFF..SFF.SF.SSSFFFFFFFFF.F.FFSFFFFEFFFFF.F                       43 / 43 (100%)
Time: 00:01.553, Memory: 56.50 MB
There was 1 error:
1) TestsFeatureTwoFactorAuthenticationSettingsTest::test_recovery_codes_can_be_regenerated
IlluminateContractsEncryptionDecryptException: The payload is invalid.
/builds/my-project/vendor/laravel/framework/src/Illuminate/Encryption/Encrypter.php:236
/builds/my-project/vendor/laravel/framework/src/Illuminate/Encryption/Encrypter.php:158
/builds/my-project/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:393
/builds/my-project/vendor/laravel/fortify/src/TwoFactorAuthenticatable.php:37
/builds/my-project/tests/Feature/TwoFactorAuthenticationSettingsTest.php:47

2

Answers


  1. Since the APP_KEY value is assumed to exist somewhere above in your .env.example file, it is being detected first, and as a result, it fails to find the appropriate value. Modify the lines as follows:

    - sed -i '/^APP_KEY=/d' .env
    - echo "APP_KEY=$APP_KEY" >> .env
    

    Or you can take different approaches, such as not having this value in the .env.example file at all.

    Login or Signup to reply.
  2. The other solution that @cengsemihsahin posted is also valid, I will share one that does not require you to have a key at all.

    I am not sure why you want to use an existing key, because you are not using an existing database with data, so you can directly run php artisan key:generate instead of manipulating the .env file, this is just another solution

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search