0

我正在尝试在我的 circleci 工作流程中进行健康检查,以查看应用程序是否编译成功。尽管应用程序构建在 circleci 上,但curl永远无法找到 localhost 端口。

我已经在我的机器上本地尝试过,效果很好。

以下是我的工作流程工作:

  health-check:
    docker:
      - image: image_name
    steps:
      - setup_remote_docker
      - restore_cache:
          key: image-cache-ci-{{ .Environment.CIRCLE_KEY }}
      - run:
          name: start 
          command: |
            docker load < image.tar
            docker run -d graphql-ci:$CIRCLE_KEY "./node_modules/nodemon/bin/nodemon.js index.js"
            docker run graphql-ci:$CIRCLE_KEY curl http://localhost:4000/.well-known/apollo/server-health

该应用程序成功编译。但是,第二行总是返回以下内容:

12354314532412342: Loading layer  1.803MB/1.803MB
Loaded image: graphql-ci:234523451325424
34523453153145235345234523452345234
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed to connect to localhost port 4000: Connection refused

Exited with code exit status 7

我也尝试过使用wait-on-wait-on http://localhost:4000/.well-known/apollo/server-health && curl http://localhost:4000/.well-known/apollo/server-health

我还在构建后将 curl 调用放在单独的运行步骤中

      - run:
          name: check that the server is up
          command: |
            docker load < image.tar
            docker run graphql-ci:$CIRCLE_KEY curl http://localhost:4000/.well-known/apollo/server-health

我也尝试过使用docker exec,但它只是告诉我它找不到我的 docker 容器(然后指定 docker 容器图像哈希)。

如果我删除分离标志-d,那么它将显示它已成功启动。

[nodemon] 2.0.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`

默认 PORT 设置为 4000,我在 docker run 命令中添加了一个标志以包含--env PORT=4000以确认它。没有影响任何东西,但我知道它正在运行。

我还pm2尝试了服务器,因为默认情况下相同的终端将可用。这将运行但没有区别。

真的只是想卷曲我知道在一个circleci工作中加载的服务器。

入口点 CMD [ "node", "index.js" ]

编辑:

我离成功的结果越来越近了。

脚本

    "curl": "if [[ $(curl -o /dev/null -w '%{http_code}' http://localhost:4000/.well-known/apollo/server-health) == 200 ]]; then echo 1; else echo 0; fi",
    "ci": "./node_modules/nodemon/bin/nodemon.js index.js",
    "health": "start-server-and-test ci http-get://localhost:4000/.well-known/apollo/server-health 'npm run curl'",

curl1如果成功则返回(返回 200),0否则返回。我还安装了一个名为的包start-server-and-test,它启动服务器,等待它完成然后运行最终脚本。它通常用于柏树,但也适用于这里。

这是结果

> @bespokemetrics/graphql@ curl /usr/src/app
 
> if [[ $(curl -o /dev/null -w '%{http_code}' http://localhost:3000/.well-known/apollo/server-health) == 200 ]]; then echo 1; else echo 0; fi

 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
 
                                 Dload  Upload   Total   Spent    Left  Speed
 
100    17  100    17    0     0   8500      0 --:--:-- --:--:-- --:--:--  8500
 
1 // this was the returned
 
npm ERR! code ELIFECYCLE
npm ERR! errno 143
npm ERR! package_name: `./node_modules/nodemon/bin/nodemon.js index.js`
npm ERR! Exit status 143

尽管成功了,为什么它会返回 ERR?

4

1 回答 1

0

我还创建了一个 npm 脚本来包含在 1 行上触发测试所需的所有代码。

      - run:
          name: Start and Curl Server
          command: |
            docker load < image.tar
            docker run graphql-ci:$CIRCLE_SHA1 npm run health
    "curl": "if [[ $(curl -o /dev/null -w '%{http_code}' http://localhost:4000/.well-known/apollo/server-health) == 200 ]]; then echo 'Success'; pkill node; else echo 'Failure'; fi"
    
    "ci": "./node_modules/nodemon/bin/nodemon.js index.js"
    
    "health": "start-server-and-test ci http-get://localhost:4000/.well-known/apollo/server-health 'npm run curl'"

我安装了一个名为的软件包start-server-and-test,它允许您启动服务器,等待它构建,然后在 1 行上运行您想要的任何命令。

我的 curl 命令只返回状态码,如果 200 则返回成功消息并终止节点进程。那是之前成功后返回的错误,因为它没有正确关闭。

于 2020-06-30T14:14:37.380 回答