Github Action: 如何确保服务器正常运行?

7
在我的 GitHub action 的 YAML 文件中,我有两个命令在结尾。第一个是 yarn start(启动服务器),第二个是运行测试文件。
通常我从本地服务器运行 yarn start,等待前端和后端端口运行,然后才从另一个终端运行测试。
但是从 GitHub action 运行时,它会先运行 yarn start 命令,然后立即运行测试脚本,所以当运行测试文件时,服务器没有监听端口。这就是为什么我的测试脚本失败了。如何确保测试脚本在 yarn start 完成后运行?
以下是我的 action.yml 文件:
name: "Github Actions Test"
on:
  push:
    branches:
      - wip/checkout2

jobs:
  test:
    runs-on: ubuntu-latest

    env:
      PRISMA_ENDPOINT: ${{secrets.PRISMA_ENDPOINT}}
      PRISMA_SECRET: ${{secrets.PRISMA_SECRET}}

    steps:
      - uses: actions/checkout@v1
      - name: "Install Node"
        uses: actions/setup-node@v1
        with:
          node-version: "12.x"
      - name: "Install global packages"
        run: npm install -g yarn prisma-cli concurrently mocha
      - name: "Run docker Container"
        run: docker-compose -f docker-compose.yml up --build -d
      - name: "Install deps"
        run: yarn install
      - name: "prisma deploy"
        run: yarn deploy:backend
      - name: "Seed Backend"
        run: yarn seed:backend
      - name: "Build app"
        run: yarn build
      - name: "Start backend and frontend concurrently on background and run tests"
        run: |
          yarn start &
          yarn test


你在 yarn start 中缺少一个额外的和号。 - Edric
3个回答

3

另一个选项是wait-on

./node_modules/.bin/wait-on tcp:3000

2

您需要执行以下操作之一:

选项1:在运行测试之前等待几秒钟:

run: |
  yarn start &
  sleep 10
  yarn test

选项2:使用专门用于此目的的某些工具等待端口打开。也许可以尝试使用wait-port(未经测试)。
选项3:使用本地Linux工具等待端口打开 - 示例1示例2

2

与@DannyB建议的类似,您可以通过等待几秒钟然后使用curl测试网络连接来测试服务器是否正常运行。

例如:

- name: "Start backend and frontend concurrently on background and run tests"
  run: |
    yarn start &
    sleep 10 &&
    curl http://localhost:8000 &&
    yarn test

这样,您就可以检查工作流作业的日志,并在执行测试之前确认服务器已经运行。
如果连接建立,curl http://localhost:<PORT>默认会返回网页内容。您还可以在命令的末尾添加-I,确保只返回请求头,并检查是否具有HTTP/1.0 200 OK状态。

&符号表示程序将在后台运行。那么&&是什么意思? - Misha
@Misha,这个stackoverflow问题有一些非常好的答案: https://dev59.com/Jm855IYBdhLWcg3wJQzT - tokto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接