【问题标题】:Github Actions - Run server & frontend, then execute testsGithub Actions - 运行服务器和前端,然后执行测试
【发布时间】:2022-06-24 18:26:43
【问题描述】:

我想使用 Github Actions 进行 CI 并在分支合并之前运行测试。

我有一个存储库,其中包含我的服务器和前端(Nest 和 Angular)。
我正在使用 Cypress/Jest 进行测试。

我需要运行后端服务器才能通过前端 cypress 测试。
目前 GH Actions 没有进入下一步,因为后端进程正在运行——但这就是我需要做的事情......

我应该如何设置才能使用 GH Actions for CI?

name: test
on: [push]
env:
  CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  OTHER_SECRETS: ${{ secrets.otherSecrets }}
jobs:
  cypress-run:
    runs-on: macos-11
    steps:
      # start cypress w/github action: https://github.com/cypress-io/github-action
      - name: Setup Node.js environment
        uses: actions/setup-node@v2.5.0
        with:
          node-version: '16.13.0'
      - name: Checkout
        uses: 'actions/checkout@v2'
      - name: "Start Backend"
        run: |
          cd server &&
          npm install &&
          npm run build &&
          npm run start:prod
      - name: "Start Frontend"
        run: |
          npm install &&
          npm run build &&
          npm run start
      - name: Cypress run
        uses: cypress-io/github-action@v2
        with:
          record: true
          browser: chrome
      - name: "Run Jest Tests"
        run: |
            cd server &&
            npm run test

#note:我已尝试将“&& sleep 10 && curl http://localhost:port -i”选项附加到 npm 命令 - 但它对我不起作用。

#note2:这是我第一次使用 GH Actions,所以也许我遗漏了一些明显的东西!

【问题讨论】:

  • 我还没有通过您分享的链接阅读文档 - 谢谢。对于这个问题,这似乎是一个笨拙的解决方案,尽管它可能有效,也许它是唯一的解决方案。似乎使用这种方法我必须将我的后端服务器容器化,然后将图像托管在 Docker Hub 上——我真的只是想要验证服务器是否已启动并正在运行 - 然后继续下一步或工作。
  • 您也可以使用 docker-compose 来启动多个容器(但您也可以将后端服务器容器化)

标签: jestjs continuous-integration cypress github-actions


【解决方案1】:

#note:我已尝试将“&& sleep 10 && curl http://localhost:port -i”选项附加到 npm 命令 - 但它对我不起作用。

这里有一个小错误,&& 会等待上一个命令完成,如果成功则只运行下一个。& 会在后台运行上一个命令,然后继续运行下一个。因此,由于没有任何东西可以阻止您的服务器,&& 将无法正常工作。

我不确定这是不是最干净的方法,但以下方法应该可行,我已使用等效方法在我的一个项目中运行 UI。

  - name: "Start Backend"
    run: |
      cd server &&
      npm install &&
      npm run build &&
      npm run start:prod &
      sleep 5 &&
      curl http://localhost:port -I
  - name: "Start Frontend"
    run: |
      npm install &&
      npm run build &&
      npm run start &
      sleep 5 &&
      curl http://localhost:port -I

【讨论】:

    猜你喜欢
    • 2020-05-21
    • 1970-01-01
    • 2021-01-29
    • 2021-11-15
    • 2021-06-25
    • 2021-03-20
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多