Skip to content

#6031 Add the sls:stage variable - #9296

Merged
medikoo merged 1 commit into
serverless:masterfrom
mnapoli:stage-alias
Apr 20, 2021
Merged

medikoo merged 1 commit into
serverless:masterfrom
mnapoli:stage-alias

Conversation

@mnapoli

@mnapoli mnapoli commented Apr 13, 2021 •

Copy link
Copy Markdown
Contributor

${sls:stage} is a shortcut to:

${opt:stage, self:provider.stage, 'dev'}

This is a new attempt at solving #6031, replacing older PR #6049.

As a start, I'm only introducing ${sls:stage} for now, and opening this PR as WIP. Once we're happy with the implementation, I'll add region in this PR.

Closes: #6031

Note: npm run lint shows errors all over the codebase, so I'm ignoring it for now.

@codecov

codecov Bot commented Apr 13, 2021 •

Copy link
Copy Markdown

Codecov Report

Merging #9296 (cd66817) into master (8aa2757) will increase coverage by 0.06%.
The diff coverage is 90.87%.

❗ Current head cd66817 differs from pull request most recent head 3e64167. Consider uploading reports for the commit 3e64167 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #9296      +/-   ##
==========================================
+ Coverage   86.86%   86.93%   +0.06%     
==========================================
  Files         316      316              
  Lines       11765    11827      +62     
==========================================
+ Hits        10220    10282      +62     
  Misses       1545     1545              
Impacted Files Coverage Δ
commitlint.config.js 0.00% <ø> (ø)
lib/classes/Variables.js 99.73% <ø> (ø)
.../cli/commands-schema/common-options/aws-service.js 100.00% <ø> (ø)
lib/cli/resolve-configuration-path.js 96.42% <ø> (ø)
lib/plugins/aws/customResources/index.js 98.57% <ø> (ø)
lib/plugins/aws/deploy/lib/extendedValidate.js 100.00% <ø> (ø)
lib/plugins/aws/deploy/lib/uploadArtifacts.js 90.00% <ø> (ø)
...ib/plugins/aws/package/lib/generateCoreTemplate.js 95.34% <ø> (ø)
...ib/plugins/aws/package/lib/saveCompiledTemplate.js 100.00% <ø> (ø)
lib/plugins/aws/package/lib/saveServiceState.js 100.00% <ø> (ø)
... and 47 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8aa2757...3e64167. Read the comment docs.

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mnapoli great thanks for working on that!

What's exactly the status of it? Why exactly it's submitted as draft PR ?

@mnapoli

mnapoli commented Apr 14, 2021

Copy link
Copy Markdown
Contributor Author

Just wanted to make sure this is what you expected ^^

Since you seem to be good with this I'll add the region.

@medikoo

medikoo commented Apr 14, 2021 •

Copy link
Copy Markdown
Contributor

Since you seem to be good with this I'll add the region.

@mnapoli region feels more AWS specific, so we decided to handle it with: #9005 as aws:region (for which PR is also very welcome!)

@mnapoli

mnapoli commented Apr 14, 2021

Copy link
Copy Markdown
Contributor Author

Oh 😄 This is why it was in draft. I'll remove the commit I just pushed then.

@mnapoli
mnapoli marked this pull request as ready for review April 14, 2021 11:17
@mnapoli

mnapoli commented Apr 14, 2021

Copy link
Copy Markdown
Contributor Author

OK all good, ready for review, there's just ${sls:stage} now.

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @mnapoli looks very good! I've just suggested few improvements to tests.

Comment thread docs/providers/aws/guide/variables.md Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's technically a shortcut for ${opt:stage, self:provider.stage, "dev"}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's update the signature to (configExt, options). It'll be more up to convention in other test files, and more portable for eventual future addition.

configExt should be a simply configuration extension to be applied via _.merge(config, configExt), and options, should be CLI options.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is invoked prior every test, even one which invokes initializeServerless() on it's own. So that's not clean.

Let's remove this, and simply invoke initializeServerless in context of it's

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can reword to should resolve ${sls.instanceId}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do one it("should resolve ${sls:stage}") and in it's context simply test one by one, all three variants

@mnapoli

mnapoli commented Apr 19, 2021

Copy link
Copy Markdown
Contributor Author

@medikoo I have pushed changes that should address all your comments.

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank @mnapoli ! That looks great. I've just proposed few style simplifications, so code is simpler

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's simplify this to if (configExt) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's simplify to options || {}

@medikoo

medikoo commented Apr 20, 2021

Copy link
Copy Markdown
Contributor

@mnapoli one more thing. I think it'll be nice to also update examples in our documentation, which use stage resolution (as e.g. ${opt:stage, self:provider.stage, 'dev'}, so those are replaced with simply ${sls:stage}.

I think those can easily be found by searching for opt:stage or self:provider:stage

This variable is a shortcut to:

```
${opt:stage, self:provider.stage, "dev"}
```
@mnapoli

mnapoli commented Apr 20, 2021

Copy link
Copy Markdown
Contributor Author

@medikoo good idea! I just pushed again.

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @mnapoli that's great!

@medikoo
medikoo merged commit ef91ae1 into serverless:master Apr 20, 2021
@mnapoli
mnapoli deleted the stage-alias branch April 20, 2021 14:50
@razor-x

razor-x commented Apr 21, 2021

Copy link
Copy Markdown

Does opt:stage also cover the env var SERVERLESS_STAGE as well? E.g., I've been using ${opt:stage, env:SERVERLESS_STAGE, self:provider.stage}.

@mnapoli

mnapoli commented Apr 21, 2021

Copy link
Copy Markdown
Contributor Author

@razor-x a quick search in the codebase shows no result for SERVERLESS_STAGE, I don't think this is an official feature?

I also read here https://forum.serverless.com/t/process-env-serverless-stage-in-1-0/144:

In v0.5 I would be able to get the current stage during runtime with process.env.SERVERLESS_STAGE. This no longer appears to work in v1.0. Is there another way to do this?

It seems like it's no longer a feature indeed. I would say that no, this variable is not taken into account. I might be wrong though.

@medikoo

medikoo commented Apr 21, 2021

Copy link
Copy Markdown
Contributor

@razor-x as @mnapoli pointed SERVERLESS_STAGE is not supported by Framework since v1.0.0.

So you can safely replace your notation with sls:stage

@nikhilprakash15

Copy link
Copy Markdown

I understand that ${sls:stage} is a shortcut to ${opt:stage, self:provider.stage, 'dev'}. But if we want the default to be a stage other than dev, can we define that, and if so, how?

@mnapoli

mnapoli commented Jul 4, 2022

Copy link
Copy Markdown
Contributor Author

@nikhilpr23 please open a different discussion.

FYI you can set the default stage via provider.stage (see https://www.serverless.com/framework/docs/providers/aws/guide/services#serverlessyml). Let's continue the discussion elsewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Variables: Introduce access to resolved stage via sls:stage

4 participants