node.js - 带子文件夹的 Heroku Node 部署

标签 node.js reactjs heroku deployment gitlab-ci

我已经尝试了几个小时了,但不知道还剩下什么......也许你有一些:-)

我正在使用react-starter-kit作为我的 react 应用程序和 gitlab 作为我的存储库。我已启用 gitlab-ci 来部署我的应用程序并将其发送到 heroku。

好吧,gitlab-ci 可以工作...是的

但是react-starter-kit使用/build文件夹来提供所有 Assets 和缩小的等应用程序。

我如何通过 gitlab-ci 在heroku上获得可运行的应用程序? 我添加了包含以下内容的 Procfile: 网络: Node ./build/server.js

heroku日志输出如下:

2017-01-02T16:57:54.655686+00:00 heroku[web.1]: State changed from crashed to starting
2017-01-02T16:57:58.721166+00:00 heroku[web.1]: Starting process with command `node ./build/server.js`
2017-01-02T16:58:00.987643+00:00 heroku[web.1]: Process exited with status 1
2017-01-02T16:58:00.995852+00:00 heroku[web.1]: State changed from starting to crashed
2017-01-02T16:58:00.997060+00:00 heroku[web.1]: State changed from crashed to starting
2017-01-02T16:58:00.833548+00:00 app[web.1]: module.js:471
2017-01-02T16:58:00.833561+00:00 app[web.1]:     throw err;
2017-01-02T16:58:00.833562+00:00 app[web.1]:     ^
2017-01-02T16:58:00.833563+00:00 app[web.1]:
2017-01-02T16:58:00.833564+00:00 app[web.1]: Error: Cannot find module '/app/build/server.js'
2017-01-02T16:58:00.833565+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:469:15)
2017-01-02T16:58:00.833566+00:00 app[web.1]:     at Function.Module._load (module.js:417:25)
2017-01-02T16:58:00.833567+00:00 app[web.1]:     at Module.runMain (module.js:604:10)
2017-01-02T16:58:00.833567+00:00 app[web.1]:     at run (bootstrap_node.js:394:7)
2017-01-02T16:58:00.833568+00:00 app[web.1]:     at startup (bootstrap_node.js:149:9)
2017-01-02T16:58:00.833569+00:00 app[web.1]:     at bootstrap_node.js:509:3
2017-01-02T16:58:07.230342+00:00 heroku[web.1]: Starting process with command `node ./build/server.js`
2017-01-02T16:58:10.416437+00:00 heroku[web.1]: State changed from starting to crashed
2017-01-02T16:58:10.398239+00:00 heroku[web.1]: Process exited with status 1
2017-01-02T16:58:10.271567+00:00 app[web.1]: module.js:471
2017-01-02T16:58:10.271583+00:00 app[web.1]:     throw err;
2017-01-02T16:58:10.271584+00:00 app[web.1]:     ^
2017-01-02T16:58:10.271584+00:00 app[web.1]:
2017-01-02T16:58:10.271585+00:00 app[web.1]: Error: Cannot find module '/app/build/server.js'
2017-01-02T16:58:10.271586+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:469:15)
2017-01-02T16:58:10.271586+00:00 app[web.1]:     at Function.Module._load (module.js:417:25)
2017-01-02T16:58:10.271587+00:00 app[web.1]:     at Module.runMain (module.js:604:10)
2017-01-02T16:58:10.271588+00:00 app[web.1]:     at run (bootstrap_node.js:394:7)
2017-01-02T16:58:10.271588+00:00 app[web.1]:     at startup (bootstrap_node.js:149:9)
2017-01-02T16:58:10.271588+00:00 app[web.1]:     at bootstrap_node.js:509:3

为了完成所有信息,我的gitlab-ci内容:

image: node:latest

stages:
  - deploy

before_script:
  - npm install

deploy:
  stage: deploy
  only:
    - master
  script:
    - npm run build -- --release
    - apt-get update -yq
    - apt-get install ruby-dev rubygems -y
    - gem install dpl
    - dpl --provider=heroku --app=nice-app --api-key=$HEROKU_API_KEY

请帮助我!谢谢!

更新:

我的文件夹结构如下所示:

├── /build/
│   ├── /content/
│   ├── /public/
│   ├── /assets.js
│   ├── /package.json
│   ├── /server.js
├── /docs/
├── /node_modules/
├── /public/
├── /src/
│   ├── /components/
│   ├── /core/
│   ├── /data/
│   ├── /routes/
│   ├── /client.js
│   ├── /config.js
│   └── /server.js
├── /test/
├── /tools/
└── package.json

根 package.json 的内容如下所示:

{
  "private": true,
  "engines": {
    "node": ">=6.5",
    "npm": ">=3.10"
  },
  "dependencies": {
    ...
  },
  "devDependencies": {
    ...
  },
  "babel": {
    "presets": [
      "react",
      "node5",
      "stage-0"
    ],
    "env": {
      "test": {
        "plugins": [
          "rewire"
        ]
      }
    }
  },
  "eslintConfig": {
    "parser": "babel-eslint",
    "extends": "airbnb",
    "globals": {
      "__DEV__": true
    },
    "env": {
      "browser": true
    },
    "rules": {
      "arrow-parens": "off",
      "generator-star-spacing": "off",
      "import/extensions": "off",
      "import/no-extraneous-dependencies": "off",
      "react/forbid-prop-types": "off",
      "react/jsx-filename-extension": "off",
      "react/no-danger": "off",
      "react/no-unused-prop-types": "off"
    }
  },
  "stylelint": {
    "extends": "stylelint-config-standard",
    "rules": {
      "string-quotes": "single",
      "property-no-unknown": [
        true,
        {
          "ignoreProperties": [
            "composes"
          ]
        }
      ],
      "selector-pseudo-class-no-unknown": [
        true,
        {
          "ignorePseudoClasses": [
            "global",
            "local"
          ]
        }
      ]
    }
  },
  "scripts": {
    "lint:js": "eslint src tools",
    "lint:css": "stylelint \"src/**/*.{css,less,scss,sss}\"",
    "lint": "npm run lint:js && npm run lint:css",
    "test": "mocha \"src/**/*.test.js\" --require test/setup.js --compilers js:babel-register",
    "test:watch": "npm run test -- --reporter min --watch",
    "clean": "babel-node tools/run clean",
    "copy": "babel-node tools/run copy",
    "bundle": "babel-node tools/run bundle",
    "build": "babel-node tools/run build",
    "deploy": "babel-node tools/run deploy",
    "render": "babel-node tools/run render",
    "start": "babel-node tools/run start"
  }
}

构建文件夹中的 package.json 如下所示:

{
  "private": true,
  "engines": {
    "node": ">=6.5",
    "npm": ">=3.10"
  },
  "dependencies": {
    ...
  },
  "scripts": {
    "start": "node server.js"
  }
}

最佳答案

因此您可以在 gitlab YAML 文件内定义工件。这允许您将构建的文件从一个阶段传递到另一个阶段。

artifacts:
    paths:
    - node_modules/
    - build/

就像这样。但是,当您将其传递给 heroku 的 dpl 库时,我发现它不会插入工件(仍然值得一提,因为您可以将构建阶段工件传递到测试阶段等)。

我对 Node 应用程序所做的是将 "postinstall" 脚本添加到 package.json npm 在 npm install 完成后运行此脚本。

我不确定为什么您有两个 package.json 文件,其中一个位于构建文件夹中。您可以仅使用项目根目录中的脚本并添加这些脚本

"scripts": {
    "start": "node build/server.js",
    "postinstall": "npm run build"
    "dev": "babel-node tools/run start",
    "build": "babel-node tools/run build"
  }

Heroku 在运行 npm install 后会自动运行 npm run build,如果您愿意,您也可以在安装后将多个链接在一起。

"postinstall": "npm run lint && npm run build"不知道如果 linter 返回问题,您是否想放弃构建,但希望您能明白这个想法。

I'm currently struggling with the passing artifacts to heroku angle. If I fix this I'll be back to comment

关于node.js - 带子文件夹的 Heroku Node 部署,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41430800/

相关文章:

javascript - Node.js 请求 - 处理多个 POST 请求

JavaScript - 从数据库中获取值并传递给 res.render

javascript - 滚动 div 代码

node.js - 已部署的镜像未在 Heroku 和 Node.js 上更新

node.js - 你如何让 Node 检查器工作?

javascript - 如何在js中将isi日期转换为iso

reactjs - 错误 发生意外错误 : "EACCES: permission denied

reactjs - "Missing class properties transform",即使安装了相关的 Babel 插件后

ruby-on-rails - 名称错误 : uninitialized constant Dotenv while pushing rails app to heroku

python - Whitenoise 和 django-compressor 导致压缩文件出现 404