cancel
Showing results for 
Search instead for 
Did you mean: 

How to pass arguments / parameters to the npm-script using npmExecuteScripts?

MikeB
Contributor
0 Kudos

I'm trying to run the npm-script from the Jenkins pipeline via the SAP Project Piper's npmExecuteScripts:

npmExecuteScripts:
    runScripts: ["testScript"]

That works! Now, I want to pass some arguments to my script.
According to the Project Piper documentation, there is a property scriptOptions, which cares about passing arguments to the called script:

Options are passed to all runScripts calls separated by a '--'. './piper npmExecuteScripts --runScripts ci-e2e --scriptOptions '--tag1' will correspond to 'npm run ci-e2e -- --tag1'

Unfortunately, I can't figure out what is the proper syntax for that command.
I've tried several combinations of using scriptOptions, e.g.:

scriptOptions: ["myArg"]
scriptOptions: ["myArg=myVal"]

and many others, but still no desired outcome!

How can I call an npm-script and pass arguments / parameters to the script using the Project Piper's npmExecuteScripts?

Accepted Solutions (1)

Accepted Solutions (1)

jamie_cawley
Advisor
Advisor
0 Kudos

Did you try adding dashes?

scriptOptions: ['--myArg']

Regards,

Jamie

MikeB
Contributor
0 Kudos

Hey, jamie.cawley !

On the Jenkins side, I've tried:

npmExecuteScripts:
  runScripts: ["testScript"]
  scriptOptions: ["--montreal"]

And on the npm-script side, I've tried various options:

"testParams": "echo '*** My city: '$PIPER_scriptOptions' ***'",
"testParams": "echo '*** My city: '{$PIPER_scriptOptions}' ***'",
"testParams": "echo '*** My city: '${PIPER_scriptOptions}' ***'",
"testParams": "echo '*** My city: $PIPER_scriptOptions ***'",
"testParams": "echo '*** My city: {$PIPER_scriptOptions} ***'",
"testParams": "echo '*** My city: ${PIPER_scriptOptions} ***'",
"testParams": "echo '*** My city: '$npm_config_PIPER_scriptOptions' ***'",
"testParams": "echo '*** My city: '{$npm_config_PIPER_scriptOptions}' ***'",
"testParams": "echo '*** My city: '${npm_config_PIPER_scriptOptions}' ***'",
"testParams": "echo '*** My city: $npm_config_PIPER_scriptOptions ***'",
"testParams": "echo '*** My city: {$npm_config_PIPER_scriptOptions} ***'",
"testParams": "echo '*** My city: ${npm_config_PIPER_scriptOptions} ***'",

But none of them returns the desired result:

*** My city: montreal ***

jamie_cawley
Advisor
Advisor

What do you see in the log files when npm is running? Have you tried using $npm_config_montreal when it has a value?

Regards,

Jamie

MikeB
Contributor
0 Kudos

jamie.cawley, I've tried $npm_config_montreal and the result:

*** My city: *** --montreal

The log file:

info  npmExecuteScripts - run-script testParams
info  npmExecuteScripts - running command: npm run testParams -- --montreal
info  npmExecuteScripts - 
info  npmExecuteScripts - > myApp@1.0.0 testParams /var/apphome/.../.jenkins/workspace/myApp_master@2
info  npmExecuteScripts - > echo '*** My city: '${npm_config_montreal}' ***' "--montreal"
info  npmExecuteScripts - 
info  npmExecuteScripts - *** My city:  *** --montreal
jamie_cawley
Advisor
Advisor
0 Kudos

Try setting the param with a value.

--montreal=test

Regards,

Jamie

MikeB
Contributor
0 Kudos

jamie.cawley, I've tried that already, unfortunately, no wanted outcome.

Jenkins Pipeline configuration:

npmExecuteScripts:
	runScripts: ["testParams1", "testParams2", "testParams3", "testParams4", "testParams5", "testParams6"]
	scriptOptions: ["--city=Montreal"]

package.json:

"scripts": {
	"testParams1": "echo \"*** My city: '$city' ***\"",
	"testParams2": "echo \"*** My city: '${city}' ***\"",
	"testParams3": "echo \"*** My city: '$npm_config_city' ***\"",
	"testParams4": "echo \"*** My city: '${npm_config_city}' ***\"",
	"testParams5": "echo \"*** My city: '{$npm_config_city}' ***\"",
	"testParams6": "echo \"*** My city: '{npm_config_city}' ***\""
}

Unfortunately, none of the scripts above provided the desired outcome: *** My city: 'Montreal' ***.

The used SAP Project Piper version: 1.147.0

jamie_cawley
Advisor
Advisor

Ok I think because there is a proceeding "--" they are not set to the config values. Can you just use them in the script you are trying to run?

scriptOptions: ["city=Montreal"]

in the script

const myArgs = process.argv.slice(2);
console.log(myArgs[0]);

Regards,

Jamie

MikeB
Contributor
0 Kudos

Bingo!

The problem was that I assumed, that the arguments mapping is performed by the SAP Project Piper and a script becomes the arguments just like in case of the regular CLI-call, e.g. npm_config_*.

Answers (0)