serverless-scriptable-plugin icon indicating copy to clipboard operation
serverless-scriptable-plugin copied to clipboard

Support for `provider.environment` in `serverless.yml`

Open cnuss opened this issue 1 year ago • 9 comments

I noticed that provider.environment: {} wasn't being pushed down to scripts.

This PR will allow scripts to utilize environment variables defined in provider.environment.

cnuss avatar May 15 '24 22:05 cnuss

hi @weixu365 ! let me know what you think of this PR! thank you!

cnuss avatar May 15 '24 22:05 cnuss

hi @weixu365 could you have a look at this PR? thank you!

cnuss avatar May 19 '24 13:05 cnuss

Just realise that the ecmaVersion need to be changed in eslint config for the optional chaining operator:

parserOptions:
  ecmaVersion: 2020

weixu365 avatar May 21 '24 07:05 weixu365

hi @weixu365 for my use case I have a hook on offline:start which runs database migrations at startup. the table name is dynamic based on Serverless stage and that logic is done using Serverless interpolation on provider.environment, e.g.

serverless.yml

provider:
  environment:
    TABLE_NAME: some-table-${opt:stage, "development"}

this allows TABLE_NAME to be available for the migration script during the hook

cnuss avatar May 21 '24 11:05 cnuss

Can you change the ecmaVersion to 2020 in .eslintrc file? it's required by the optional chaining operator:

parserOptions:
  ecmaVersion: 2020

weixu365 avatar May 21 '24 13:05 weixu365

@weixu365 done! could you allow the unit tests to run?

cnuss avatar May 21 '24 14:05 cnuss

@weixu365 another question... should we create a custom.scriptable.environment[] so that this feature is opt-in?

custom:
  scriptable:
    environment:
      - process # the default
      # opt in for this feature:
      # - serverless
    hooks:
      serverless:offline: echo "env is $(env)"

Or alternatively, bump the major version on this project since this fundamentally changes how "environment" gets set when a process runs?

cnuss avatar May 21 '24 14:05 cnuss

I'll fix the codecov failure asap

cnuss avatar May 21 '24 23:05 cnuss

I think it should be compatible with the current version if we change the code to

      ...(this.serverless?.service?.provider?.environment || {}),
      ...(process.env || {}),

It's also a good idea to use the opt-in feature which explicitly set the environment for the plugin

environment: - process # the default # opt in for this feature: # - serverless

weixu365 avatar May 21 '24 23:05 weixu365

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 20 days. Thank you for your contributions.

github-actions[bot] avatar Aug 20 '24 08:08 github-actions[bot]

I would have also liked this feature.

I'm currently trying to create a workflow where I deploy a web app to a s3 bucket using serverless. I've used hooks to run npm run build prior to serverless-s3-sync uploading the build web app to an s3 bucket. However, I would want my environment variables to be set for the build for the appropriate stage etc.

I think this pull request would have helped me do this.

jamesstidard avatar Oct 08 '25 11:10 jamesstidard