【问题标题】:Skip npm postinstall script in Travis在 Travis 中跳过 npm postinstall 脚本
【发布时间】:2019-04-23 10:21:25
【问题描述】:

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

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

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

package.json

sn-p

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

sn-p

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】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2021-11-22
      • 2014-12-14
      • 2021-07-20
      • 1970-01-01
      • 2023-03-31
      • 2018-03-01
      • 2014-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多