在Travis中跳过npm postinstall脚本

问题描述 投票:1回答:1

我有一个在AWS和Cloud66上部署的应用程序(出于某种原因)。为了为aws构建应用程序,我们必须在travis.yml中编写脚本并部署到Cloud66,我们必须在postInstall中编写构建命令。

发生的事情是,当travis被触发时,它会运行postScript同时运行构建应用程序的npm install,同样在yml脚本中也会构建应用程序构建。因此,每次触发更改时都会发生两次构建,这是不必要的。

我尝试通过在travis中添加环境var并运行预安装脚本来删除postInstall脚本,该脚本将删除包中的postInstall脚本。这会删除postInstall脚本,但它不会改变任何内容,postInstall脚本会以某种方式运行。

package.json的片段

"preinstall": "node setEnvironment.js",
"postinstall": "npm run build-production"

travis.yml的片段

script:
- if [ "$TRAVIS_BRANCH" = "prod" ]; then npm run build-production && gulp
  copy-deploy-configs --type=prod; else echo "not an prod branch"; fi

setEnvironment.js

const package = require('./package.json');
const fs = require('fs');

const environment = process.env.APP_ENVIRONMENT;

if (environment === 'travis') {
    delete package.scripts.postinstall;
} else {
    package.scripts["postinstall"] = `npm run build-${environment}`;
}

fs.writeFileSync('package.json', JSON.stringify(package, null, 4));
npm continuous-integration travis-ci npm-install npm-scripts
1个回答
0
投票

在您的package.json中,您可以执行以下操作:

"postinstall": "if [ -z \"$TRAVIS_BRANCH\" ]; then npm run build-production; fi"

只有在未设置$TRAVIS_BRANCH时才应执行此操作,这只应在Cloud66上进行。

© www.soinside.com 2019 - 2024. All rights reserved.