jacob-meacham / serverless-plugin-git-variables

:zap: Expose git variables to serverless
MIT License
87 stars 32 forks source link

Approached unrecognized configuration variable sources: "git". #71

Closed Wurstnase closed 2 years ago

Wurstnase commented 3 years ago

With the latest serverless i've got:

Serverless: Deprecation warning: Approached unrecognized configuration variable sources: "git".
        From a next major this will be communicated with a thrown error.
        Set "variablesResolutionMode: 20210326" in your service config, to adapt to new behavior now
        More Info: https://www.serverless.com/framework/docs/deprecations/#NEW_VARIABLES_RESOLVER
Wurstnase commented 3 years ago

With the latest resolver, a plugin should the pattern: https://www.serverless.com/framework/docs/providers/aws/guide/plugins#custom-variable-types

A replacement could look like:

diff --git a/src/index.js b/src/index.js
index a5f1a50..33edef8 100644
--- a/src/index.js
+++ b/src/index.js
@@ -21,15 +21,15 @@ export default class ServerlessGitVariables {
   constructor(serverless, options) {
     this.serverless = serverless
     this.resolvedValues = {}
-    const delegate = serverless.variables.getValueFromSource.bind(serverless.variables)

-    serverless.variables.getValueFromSource = (variableString) => {
-      if (variableString.startsWith(`${GIT_PREFIX}:`)) {
-        const variable = variableString.split(`${GIT_PREFIX}:`)[1]
-        return this._getValue(variable)
+    this.configurationVariablesSources = {
+      [GIT_PREFIX]: {
+        resolve: async ({ address, params, resolveConfigurationProperty, options }) => {
+          return {
+            value: await this._getValue(address)
+          }
+        }
       }
-
-      return delegate(variableString)
     }
     this.hooks = {
       'after:package:initialize': this.exportGitVariables.bind(this),
Wurstnase commented 3 years ago

Unfortunately I wasn't able to change the test in lag of serverless documentation and my rough js skills.

henhal commented 3 years ago

@Wurstnase I tried to pick up on your work, but something seems strange - I'm not sure the tests simply need to be adapted? Each call now rejects with Invalid variable reference syntax for variable git:describe. You can only reference env vars, options, & files. You can check our docs for more info.. But it should be possible to use custom variables with the resolver syntax you detailed. Did you get the same result or what changes to the tests were you referring to?

Wurstnase commented 3 years ago

You may need to update the package.json also. I've updated the complete package.json to the latest packages. Maybe it will work with less and I don't know how to handle the "peerDependencies" correctly.

However, with this change I was able to use the git plugin within a minimal serverless.yml.

service: test

provider: aws
custom:
  value: ${git:sha1}
plugins:
  - ./lib
diff --git a/package.json b/package.json
index c5832d7..51603f7 100644
--- a/package.json
+++ b/package.json
@@ -42,30 +42,30 @@
   },
   "devDependencies": {
     "@ava/babel": "1.0.1",
-    "@babel/cli": "7.12.1",
-    "@babel/core": "7.12.3",
-    "@babel/preset-env": "7.12.1",
-    "@babel/register": "7.12.1",
-    "ava": "3.13.0",
+    "@babel/cli": "7.13.16",
+    "@babel/core": "7.14.2",
+    "@babel/preset-env": "7.14.2",
+    "@babel/register": "7.13.16",
+    "ava": "3.15.0",
     "babel-plugin-add-module-exports": "1.0.4",
     "coveralls": "3.1.0",
-    "cross-env": "7.0.2",
-    "eslint": "7.12.1",
-    "eslint-config-standard": "16.0.1",
-    "eslint-plugin-ava": "11.0.0",
+    "cross-env": "7.0.3",
+    "eslint": "7.26.0",
+    "eslint-config-standard": "16.0.2",
+    "eslint-plugin-ava": "12.0.0",
     "eslint-plugin-import": "2.22.1",
     "eslint-plugin-node": "11.1.0",
-    "eslint-plugin-promise": "4.2.1",
-    "eslint-plugin-standard": "4.0.2",
-    "fs-extra": "9.0.1",
+    "eslint-plugin-promise": "5.1.0",
+    "eslint-plugin-standard": "4.1.0",
+    "fs-extra": "10.0.0",
     "nyc": "15.1.0",
     "rimraf": "3.0.2",
-    "serverless": "1.17.0",
+    "serverless": ">=2.43.1",
     "tmp": "0.2.1"
   },
   "modules": true,
   "peerDependencies": {
-    "serverless": ">=1.16.0"
+    "serverless": ">=2.43.1"
   },
   "ava": {
     "require": [
henhal commented 3 years ago

@Wurstnase Updating package.json didn't help, it's part of #76 and I verified serverless 2.43.1 was used.

Wurstnase commented 3 years ago

I think, the tests itself are a little bit hacky/undocumented stuff using serverless somehow you shouldn't use it, maybe. When I use the changes and create a demo project using the {git:xyz} it is working without any warning from serverless. So I guess the change itself is correct.

jacob-meacham commented 2 years ago

This should now be fixed and work for sls v1, v2, and v3. Thanks!