我想使用 Github Actions for 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,所以我可能遗漏了一些明显的东西!!
最佳答案
#note: I have tried appending the "&& sleep 10 && curl http://localhost:port -i" option to the npm commands - and it hasn't worked for me.
这里有一个小错误,&&
会等待上一条命令完成,只有成功才运行下一条 &
会在中运行上一条命令背景,然后将继续运行下一个。因此,由于没有什么能阻止您的服务器,&&
将不起作用。
我不确定这是最干净的方法,但以下方法应该有效,我在我的一个项目中使用了等效的方法来运行 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
关于jestjs - Github Actions - 运行服务器和前端,然后执行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70231685/