在终端运行以下命令: “` 同时运行 Firebase 模拟器和 Vitest “`

huangapple go评论69阅读模式
英文:

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&#39;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&#39;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: &#39;npm&#39;
      - 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: &#39;npm&#39;
      - 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.

在终端运行以下命令:
“`
同时运行 Firebase 模拟器和 Vitest
“`

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?

在终端运行以下命令:
“`
同时运行 Firebase 模拟器和 Vitest
“`


EDIT

These are the two executed scripts that are in my package.json:

&quot;test&quot;: &quot;vitest&quot;,
&quot;emulators&quot;: &quot;firebase emulators:start --project celebrityfanalizer --import emulatorData&quot;

答案1

得分: 1

在你的工作流中,job1 从未完成,因为模拟器在其中运行,这就是为什么当你将 job2 添加到它的依赖项上时,即 needs: job1job2 从未运行。

你需要将两个工作合并为一个:

  1. 设置环境
  2. 在后台启动模拟器
  3. 等待模拟器启动
    • 添加一些延迟(同时将其标准输出/标准错误转储到文件中,并持续检查其内容以获取成功日志,例如 “Emulator hub running”
  4. 最后运行测试

以下是在后台启动模拟器后延迟 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:

  1. set up the environment
  2. start the emulators in the background
  3. 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. &quot;Emulator hub running&quot;)
  4. 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: &#39;npm&#39;

    - name: Start the emulators and run tests
      env:
        VITE_RELEASE_STAGE: testing
      run: |
        npm ci
        npm install --save firebase-tools
        nohup npm run emulators &amp;&gt; emulators.log &amp;
        sleep 1m
        npm run test        

Relevant:


Apart from that, you can use jobs.&lt;job_id&gt;.services and run the emulators in a container if it's available.

huangapple
  • 本文由 发表于 2023年2月18日 23:10:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定