将参数从 Jenkins CI 传递到 npm 脚本
Passing parameters from Jenkins CI to npm script
当我 运行 Jenkins 构建时,我想将 COMMIT_HASH
和 BRANCH_NAME
传递到我的 javascript 文件之一:publish.js
,这样我可以删除 tags
和 consumerVersion
.
的硬编码值
这是我的代码:
詹金斯文件
stage('Publish Pacts') {
steps {
script {
sh 'npm run publish:pact -Dpact.consumer.version=${COMMIT_HASH} -Dpact.tag=${env.BRANCH_NAME}'
}
}
}
package.json
"scripts": {
"publish:pact": "node ./src/test/pact/publish.js"
}
./src/test/pact/publish.js
let publisher = require('@pact-foundation/pact-node');
let path = require('path');
let opts = {
providerBaseUrl: `http://localhost:${global.port}`,
pactFilesOrDirs: [path.resolve(process.cwd(), 'pacts')],
pactBroker: 'http://localhost:80',
tags: ["prod", "test"], // $BRANCH_NAME
consumerVersion: "2.0.0" // $COMMIT_HASH
};
publisher.publishPacts(opts).then(() => {
console.log("Pacts successfully published");
done()
});
有人知道怎么做吗?
您可以将 cli 参数传递给最终出现在 process.argv 中的节点脚本。
npm 还通过两个破折号 --
.
传递 cli arguments
为了说明这一点,请考虑以下示例:
詹金斯文件
stage('Publish Pacts') {
steps {
script {
sh 'npm run publish:pact -- ${COMMIT_HASH} ${env.BRANCH_NAME}'
}
}
}
package.json
"scripts": {
"publish:pact": "node ./src/test/pact/publish.js"
}
publish.js
// process.argv[0] = path to node binary
// process.argv[1] = path to script
console.log('COMMIT_HASH:',process.argv[2]);
console.log('BRANCH_NAME:',process.argv[3]);
为简单起见,我省略了 cli 标志。
希望这有帮助
当我 运行 Jenkins 构建时,我想将 COMMIT_HASH
和 BRANCH_NAME
传递到我的 javascript 文件之一:publish.js
,这样我可以删除 tags
和 consumerVersion
.
这是我的代码:
詹金斯文件
stage('Publish Pacts') {
steps {
script {
sh 'npm run publish:pact -Dpact.consumer.version=${COMMIT_HASH} -Dpact.tag=${env.BRANCH_NAME}'
}
}
}
package.json
"scripts": {
"publish:pact": "node ./src/test/pact/publish.js"
}
./src/test/pact/publish.js
let publisher = require('@pact-foundation/pact-node');
let path = require('path');
let opts = {
providerBaseUrl: `http://localhost:${global.port}`,
pactFilesOrDirs: [path.resolve(process.cwd(), 'pacts')],
pactBroker: 'http://localhost:80',
tags: ["prod", "test"], // $BRANCH_NAME
consumerVersion: "2.0.0" // $COMMIT_HASH
};
publisher.publishPacts(opts).then(() => {
console.log("Pacts successfully published");
done()
});
有人知道怎么做吗?
您可以将 cli 参数传递给最终出现在 process.argv 中的节点脚本。
npm 还通过两个破折号 --
.
为了说明这一点,请考虑以下示例:
詹金斯文件
stage('Publish Pacts') {
steps {
script {
sh 'npm run publish:pact -- ${COMMIT_HASH} ${env.BRANCH_NAME}'
}
}
}
package.json
"scripts": {
"publish:pact": "node ./src/test/pact/publish.js"
}
publish.js
// process.argv[0] = path to node binary
// process.argv[1] = path to script
console.log('COMMIT_HASH:',process.argv[2]);
console.log('BRANCH_NAME:',process.argv[3]);
为简单起见,我省略了 cli 标志。 希望这有帮助