brefphp / laravel-bridge

Package to use Laravel on AWS Lambda with Bref
https://bref.sh/docs/frameworks/laravel.html
MIT License
319 stars 63 forks source link

issues when deploying via CI vs locally #20

Closed aknosis closed 3 years ago

aknosis commented 3 years ago

I've got a barebones Laravel 8 project, setup per instructions at https://bref.sh/docs/frameworks/laravel.html. (The only difference is I'm using HTTP API).

When I deploy from my local machine the default welcome view renders without issue. When I deploy via CI I get this error with the welcome view: There is no existing directory at "/var/task/storage/logs" and it could not be created: Read-only file system.

I don't believe there is any issue with the serverless portion of this as comparing the debug output (with SLS_DEPLOY=1) didn't show many differences.

I compared the output of my .serverless/project.zip that was created on my machine vs in CI and the only noticeable differences I saw was my CI version was missing:

bootstrap/cache/packages.php
bootstrap/cache/services.php

My ci config is as such:

curl -sS https://getcomposer.org/installer | php
php composer.phar install --no-dev --no-progress --prefer-dist
serverless deploy -v -s staging

Any thoughts on what I might be doing wrong?

mnapoli commented 3 years ago

Are you sure that the .env or environment variables are not different? Because it seems that Laravel tries to log to file (stack driver), which should not happen on Lambda (it doesn't work).

That's why we define the driver to stderr (https://github.com/brefphp/laravel-bridge/blob/master/src/BrefServiceProvider.php#L43-L47).

aknosis commented 3 years ago

I figured it out! We were both very close with our initial thoughts, the missing bootstrap/cache I noticed was the problem.

In GitLab CI I'm doing a multi stage build where my project is built in a PHP 7.4 image (composer steps listed above) and then it rolls into a node:alpine image that does the serverless deploy. Turns out that when it switches to that node image it re-fetches the code from git and the package discovery is never done so it doesn't know about any of the services (BrefServerProvider is never loaded).

I plan to add my example CI build to the examples repo. I will have this Laravel requirement listed in the notes somewhere.

For posterity, here's a simple example of what I'm using:

stages:
  - test
  - build
  - deploy

cache:
  paths:
    - vendor/
    - bootstrap/cache/

test:
  stage: test
  image: php:7.4
  artifacts:
    reports:
      junit: junit.xml
  script:
    - curl -sS https://getcomposer.org/installer | php
    - php composer.phar install --no-progress --prefer-dist
    - vendor/bin/phpunit --coverage-text --colors=never --log-junit=junit.xml

build:
  stage: build
  image: php:7.4
  script:
    - curl -sS https://getcomposer.org/installer | php
    - php composer.phar install --no-dev --no-progress --prefer-dist

deploy:
  stage: deploy
  image: node:alpine
  script:
    - npm install -g serverless
    - stage=$([ "$CI_COMMIT_REF_NAME" == "master" ] && echo "prod" || echo "staging")
    - cp .env.$stage .env
    - serverless deploy -v -s $stage