weixu365 / serverless-scriptable-plugin

Adding script support to Serverless 1.x which enables you to customize Serverless behavior without writing a plugin.
MIT License
112 stars 11 forks source link

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

Closed cnuss closed 2 months ago

cnuss commented 6 months ago

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 commented 6 months ago

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

cnuss commented 5 months ago

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

weixu365 commented 5 months ago

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

parserOptions:
  ecmaVersion: 2020
cnuss commented 5 months ago

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

weixu365 commented 5 months ago

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

parserOptions:
  ecmaVersion: 2020
cnuss commented 5 months ago

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

cnuss commented 5 months ago

@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 commented 5 months ago

I'll fix the codecov failure asap

weixu365 commented 5 months ago

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

github-actions[bot] commented 2 months ago

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.