4

I am literally copying what the Laravel Vapor documentation states but to no avail. So to keep it short, I am unable to access the ASSET_URL environment variable that Taylor says is injected during the build step.

Now note, the same environment variable is being injected into the application which is why the assets() and mix() helpers in blade on index.blade.php work just fine. However, my problem is accessing the ASSET_URL inside webpack.mix.js.

This is from my Github action below. You can see that ASSET_URL is empty when logged.

> @ production /github/workspace/.vapor/build/app
> cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js

ASSET URL IS: 
 DONE  Compiled successfully in 26754ms11:23:07 PM

webpack.mix.js.

// To deal with Laravel Vapor
if (mix.inProduction()) {
    const ASSET_URL = process.env.ASSET_URL;
    console.log("ASSET URL IS: " + ASSET_URL);

    mix.webpackConfig(webpack => {
        return {
            plugins: [
                new webpack.DefinePlugin({
                    "process.env.ASSET_PATH": JSON.stringify(ASSET_URL)
                })
            ],
            output: {
                publicPath: ASSET_URL
            }
        };
    });

Proposed solutions on Laracasts forums

  1. Paul Marshall suggests that he found a workaround by using window.__ASSET_URL__ = '{{ env('ASSET_URL') }}'; but I do not like that approach, unless its the last resort.
  2. @fylzero suggests that I do MIX_ASSET_URL="${ASSET_URL}" in my environment files to see if Mix can read that. It doesn't.
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
Ali Gajani
  • 14,762
  • 12
  • 59
  • 100
  • Did you ever solve this? Having a similar issue trying to deploy on vapor over a github action. During the build step, a `.env` file is created, the `ASSET_URL` is properly injected, but it just won't source the file (even `export $(xargs < .env)` at the beginnging of the build step won't work). Using the `ASSET_URL` will always yield an empty ENV. – k0pernikus Aug 31 '21 at 14:52
  • It may be caused by a security feature for the github action. An env variable may be set in a step, but can only be accessed in the next step. https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable – k0pernikus Aug 31 '21 at 15:09

1 Answers1

0

In our case, we think it relates to github not sourcing the .env file. The env file, it contains the right content, but even manually sourcing the .env file does not work, as apparently the env variables only become available in the next step.

We have fallen back on manually parsing the env variable out of the .env file, we used this grep approach:

$(grep -oP "ASSET_URL=\K.*$" .env)
k0pernikus
  • 60,309
  • 67
  • 216
  • 347