mvila / serverless-plugin-scripts

Add scripting capabilities to the Serverless Framework
123 stars 15 forks source link

"deploy:finalize" hook do not recognise the stage when passed as a command line argument #11

Closed marknorrapscm closed 3 years ago

marknorrapscm commented 3 years ago

I know this project is in maintenance rather than active development; I am submitting this in the hope that bug fixes are still being implemented.


I am using hooks to run a function after I deploy my application.

custom:
  scripts:
    hooks:
       "deploy:finalize": serverless invoke -f runAfterDeploy

I run serverless deploy and specify the stage with the --stage option. The problem is that the above hook / function call does not seem to recognise whatever it is that I have specified as the stage. If I have set stage using the following serverless.yml config so that it uses whatever is supplied via the --stage parameter:

provider:
  name: aws
  runtime: nodejs12.x
  stage: ${opt:stage}

...then I get the following error when I deploy using serverless deploy --stage prod. The deploy runs correctly and the prod value is used everywhere, but the hook does not recognise it and has undefined as the value:

https://i.imgur.com/ruaaA06.png

This is a major headache if the runAfterDeploy task does something important. Using ${self:opt, "dev"} instead produces a similar result, whereby no matter what you specify via --stage the variable always ends up being "dev".


Here's the full re-producible serverless.yml:

service: serverless-demo

frameworkVersion: "2"

plugins:
  - serverless-offline
  - serverless-plugin-scripts

custom:
  scripts:
    hooks:
       "deploy:finalize": serverless invoke -f runAfterDeploy

provider:
  name: aws
  runtime: nodejs12.x
  region: eu-west-1
  stage: ${opt:stage}
  environment:
    stage: ${self:provider.stage}

functions:
  runAfterDeploy:
    handler: handler.runAfterDeploy
    events:
      - http:
          path: /runAfterDeploy
          method: get
          cors: true
marknorrapscm commented 3 years ago

I've had a closer look at your source code and it seems that most of what this project does it hook into existing functions provided by Serverless Framework itself, in which case this is likely a bug in that project rather than this.

I'm curious to hear your thoughts on this @mvila but I think I should probably re-create this in the main SLS repo.

mvila commented 3 years ago

My code is very simple indeed, and expressions such as {self:service} should be handled by the Serverless Framework.

I am sorry but I don't think I can provide more help, I stopped using the Serverless Framework a long time ago, and I don't remember how it works anymore.