英文:
Run firebase emulators + vitest at once
问题
我试图同时运行两个作业。
首先我需要运行 `firebase emulators`,当端口打开时,我需要运行 `vitest`。
这是我目前正在做的:
```yml
name: 运行测试
on:
pull_request:
branches: [develop]
workflow_dispatch:
jobs:
job1:
name: 运行模拟器
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: 使用 Node.js 18.14.0
uses: actions/setup-node@v3
with:
node-version: 18.14.0
cache: 'npm'
- name: 执行模拟器
env:
VITE_RELEASE_STAGE: testing
run: |
npm ci
npm install --save firebase-tools
npm run emulators
job2:
name: 运行单元测试
needs: job1
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: 使用 Node.js 18.14.0
uses: actions/setup-node@v3
with:
node-version: 18.14.0
cache: 'npm'
- name: 执行单元测试
env:
VITE_RELEASE_STAGE: testing
run: |
npm ci
npm run test
门打开得很完美,但第二个作业从未运行。
这里是我的疑惑所在。因为如果我不使用 needs
,两者将同时运行,我将在作业2中收到一个错误,因为作业1中的端口不会打开。
我想在模拟器端口打开后立即运行作业2,并且只有在作业2完成后才完成作业1。
有谁知道我如何解决这个问题吗?
编辑
这是我 package.json
中执行的两个脚本:
"test": "vitest",
"emulators": "firebase emulators:start --project celebrityfanalizer --import emulatorData"
<details>
<summary>英文:</summary>
I'm trying to run two jobs at the same time.
First I need to run `firebase emulators` and when the ports are open, I need to run vitest.
This is what I'm doing at the moment:
```yml
name: Run Tests
on:
pull_request:
branches: [develop]
workflow_dispatch:
jobs:
job1:
name: Run Emulator
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js 18.14.0
uses: actions/setup-node@v3
with:
node-version: 18.14.0
cache: 'npm'
- name: Execute Emulator
env:
VITE_RELEASE_STAGE: testing
run: |
npm ci
npm install --save firebase-tools
npm run emulators
job2:
name: Run Unit Tests
needs: job1
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js 18.14.0
uses: actions/setup-node@v3
with:
node-version: 18.14.0
cache: 'npm'
- name: Execute Unit Tests
env:
VITE_RELEASE_STAGE: testing
run: |
npm ci
npm run test
The doors open perfectly, but the second job never runs.
Here lies my doubt. Because if I don't use needs
the two will run at the same time and I will get an error in job2 because the ports will not be open in job 1.
I would like to run job 2 as soon as the emulator ports are open and finish job 1 only after job 2 finishes.
Does anyone know how I can resolve this?
EDIT
These are the two executed scripts that are in my package.json
:
"test": "vitest",
"emulators": "firebase emulators:start --project celebrityfanalizer --import emulatorData"
答案1
得分: 1
在你的工作流中,job1
从未完成,因为模拟器在其中运行,这就是为什么当你将 job2
添加到它的依赖项上时,即 needs: job1
,job2
从未运行。
你需要将两个工作合并为一个:
- 设置环境
- 在后台启动模拟器
- 等待模拟器启动
- 添加一些延迟(同时将其标准输出/标准错误转储到文件中,并持续检查其内容以获取成功日志,例如
“Emulator hub running”
)
- 添加一些延迟(同时将其标准输出/标准错误转储到文件中,并持续检查其内容以获取成功日志,例如
- 最后运行测试
以下是在后台启动模拟器后延迟 1 分钟的示例:
name: 在模拟器中运行测试
on:
pull_request:
branches: [develop]
workflow_dispatch:
jobs:
ci:
runs-on: ubuntu-latest
steps:
- name: 检出
uses: actions/checkout@v3
- name: 设置 NodeJS 18.14.0
uses: actions/setup-node@v3
with:
node-version: 18.14.0
cache: 'npm'
- name: 启动模拟器并运行测试
env:
VITE_RELEASE_STAGE: testing
run: |
npm ci
npm install --save firebase-tools
nohup npm run emulators >> emulators.log &
sleep 1m
npm run test
相关链接:
除此之外,你可以使用 jobs.<job_id>.services
并在容器中运行模拟器(如果可用)。
英文:
In your workflow, the job1
never finishes because the emulators are running in it and that's why the job2
never runs when you add its dependency on job1
i.e. needs: job1
.
You need to combine both jobs in one:
- set up the environment
- start the emulators in the background
- wait for the emulators to start
- add some delay (along with a dump of its STDOUT/STDERR to a file and keep checking its contents for success logs e.g.
"Emulator hub running"
)
- add some delay (along with a dump of its STDOUT/STDERR to a file and keep checking its contents for success logs e.g.
- and, finally, run tests
Here's an example with a delay of 1 minute after starting the emulators in the background:
name: Run Tests in the Emulators
on:
pull_request:
branches: [develop]
workflow_dispatch:
jobs:
ci:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up NodeJS 18.14.0
uses: actions/setup-node@v3
with:
node-version: 18.14.0
cache: 'npm'
- name: Start the emulators and run tests
env:
VITE_RELEASE_STAGE: testing
run: |
npm ci
npm install --save firebase-tools
nohup npm run emulators &> emulators.log &
sleep 1m
npm run test
Relevant:
Apart from that, you can use jobs.<job_id>.services
and run the emulators in a container if it's available.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论