在GitHub Actions中使用其他Docker容器运行Playwright

9nvpjoqh  于 2023-05-06  发布在  Docker
关注(0)|答案(1)|浏览(138)

TL;DR

我试图在我的GitHub Actions工作流中运行Playwright,该工作流涉及多个容器,包括MongoDB和Firebase。连接和剧作家跟踪不工作。

问题

使用下面的代码,我无法在CI管道中运行测试(我认为),因为容器无法连接。GitHub Actions中的错误意味着测试没有启动:在设置代码和Running 0 tests using 0 workers中停止。
当我尝试通过Playwright跟踪查看启动时可能发生的情况时,跟踪文件无效。尝试上传Playwright Trace时出错:
Could not load trace from setup-trace.zip. Make sure to upload a valid Playwright trace.
Playwright可能需要mcr.microsoft.com/playwright:v1.31.1-focal容器,但如果我在该容器中运行它,则docker无法用于其他步骤。

GitHub操作出错

Run PLAYWRIGHT_SERVER="http://localhost:3000" npx playwright test

Running 0 tests using 0 workers

page.fill: Timeout 30000ms exceeded.
=========================== logs ===========================
waiting for locator('input[name="email"]')
============================================================

   at signup-page.ts:23

  21 |
  22 |   async signUp({ email, password }: UserInfo, ensureSuccess?: boolean) {
> 23 |     await this.page.fill('input[name="email"]', email);
     |                     ^
  24 |     await this.page.fill('input[name="password"]', password);
  25 |     await this.page.fill('input[name="repeatPassword"]', password);
  26 |     await this.page.click('button[type="submit"]');

    at SignupPage.signUp (/home/runner/work/<REPOSITORY>/e2e/signup-page.ts:23:21)
    at initApplicant01 (/home/runner/work/<REPOSITORY>/e2e/global-setup.ts:31:20)
    at globalSetup (/home/runner/work/<REPOSITORY>/e2e/global-setup.ts:66:7)

Error: Process completed with exit code 1.

代码

.github/workflows/build.yml

name: Build

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build_job:
    name: Build Dev
    runs-on: ubuntu-latest
    permissions:
      contents: 'read'
      id-token: 'write'
    outputs:
      release_tag_dev: ${{ steps.get-tag-dev.outputs.release_tag }}
    steps:
    - name: Setup
      uses: actions/checkout@v3
    - uses: actions/setup-node@v3
    - run: npm install
    - run: npm run build

    - name: Build and upload
      uses: docker/setup-buildx-action@v2
    - uses: docker/build-push-action@v4
      with:
        context: .
        tags: specific-build:latest
        outputs: type=docker,dest=./repository-image.tar
    - uses: actions/upload-artifact@v3
      with:
        name: repository-artifact
        path: ./repository-image.tar

    - name: Get Tag
      id: get-tag-dev
      run: echo "release_tag=$(date +%s%3N)-$(git rev-parse --short=8 HEAD)-prod" >> $GITHUB_OUTPUT

  test_job:
    name: Run Tests
    needs: [ build_job ]
    runs-on: ubuntu-latest
    permissions:
      contents: 'read'
      id-token: 'write'
    # container: mcr.microsoft.com/playwright:v1.31.1-focal
    steps:
    - uses: actions/checkout@v3

    - name: Download Repository TAR
      id: auth
      uses: google-github-actions/auth@v0
      with:
        token_format: access_token
        workload_identity_provider: <CREDENTIALS>
        service_account: <ACCOUNT>
        access_token_lifetime: 300s
    - uses: docker/login-action@v2
      with:
        registry: us-west1-docker.pkg.dev
        username: <USERNAME>
        password: ${{ steps.auth.outputs.access_token }}
    - uses: actions/download-artifact@v3
      with:
        name: repository-artifact
    - run: docker load --input ./repository-image.tar
    - run: RELEASE_TAG=${{ needs.build_job.outputs.release_tag_dev }} docker compose up -d

    - name: Run Tests
      uses: actions/setup-node@v3
    - run: npm ci --cache .npm --prefer-offline
    - run: npx playwright install
    - run: npx playwright test  
    # Everything below is just to view the Playwright Trace      
      continue-on-error: true
      env:
          PLAYWRIGHT_SERVER: "http://localhost:3000"
    - uses: actions/upload-artifact@v3
      with:
        name: upload-trace
        path: ./e2e/artifacts/setup-trace.zip

docker-compose.yml

version: "1"

services:
  mongodb:
    image: "mongo"
    ports:
      - "27017:27017"
    command: mongod --bind_ip 0.0.0.0

  firebase:
    image: <PRIVATE_IMAGE>
    ports:
      - "4000:4000"
      - "4400:4400"
      - "4500:4500"
      - "9099:9099"
      - "9199:9199"
    command: --project=<REPOSITORY> emulators:start

  <REPOSITORY_IN_TEST>:
    image: "specific-build:latest"
    ports:
      - "3000:3000"
    environment:
      - DB_CONNECTION_STRING=mongodb://<PRIVATE_STRING>
      - DISABLE_DB_SSL=true
      - CORS_ORIGINS=http://localhost:3000
      - FIREBASE_AUTH_EMULATOR_HOST=firebase:9099
      - NEXT_PUBLIC_FIREBASE_AUTH_EMULATOR_HOST=firebase:9099
      - NEXT_PUBLIC_SENTRY_ENVIRONMENT=ci
      - <PLAID, SENTRY, and OTHER CONTEXT>
epfja78i

epfja78i1#

我解决了这个问题。有几件事配置错误:

  1. build.yml中的build_job需要NEXT_PUBLIC_FIREBASE_AUTH_EMULATOR_HOSTlocalhost:9099,以匹配docker-compose.yml中的mongo
  2. docker-compose.yml中的mongo需要MONGO_INITDB_ROOT_USERNAMEMONGO_INITDB_ROOT_PASSWORD
  3. docker-compose.yml中的<REPOSITORY_IN_TEST>需要NEXT_PUBLIC_FIREBASE_AUTH_EMULATOR_HOSTlocalhost:9099,而不是firebaseL:9099,以匹配build.yml中的build_job
    文件的正确版本如下:
    .github/workflows/build.yml
name: Build

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build_job:
    name: Build Dev
    runs-on: ubuntu-latest
    permissions:
      contents: 'read'
      id-token: 'write'
    env:
      NEXT_PUBLIC_FIREBASE_AUTH_EMULATOR_HOST: localhost:9099
    outputs:
      release_tag_dev: ${{ steps.get-tag-dev.outputs.release_tag }}
    steps:
    - name: Setup
      uses: actions/checkout@v3
    - uses: actions/setup-node@v3
    - run: npm install
    - run: npm run build

    - name: Build and upload
      uses: docker/setup-buildx-action@v2
    - uses: docker/build-push-action@v4
      with:
        context: .
        tags: specific-build:latest
        outputs: type=docker,dest=./repository-image.tar
    - uses: actions/upload-artifact@v3
      with:
        name: repository-artifact
        path: ./repository-image.tar

    - name: Get Tag
      id: get-tag-dev
      run: echo "release_tag=$(date +%s%3N)-$(git rev-parse --short=8 HEAD)-prod" >> $GITHUB_OUTPUT

  test_job:
    name: Run Tests
    needs: [ build_job ]
    runs-on: ubuntu-latest
    environment: dev
    permissions:
      contents: 'read'
      id-token: 'write'
    steps:
    - uses: actions/checkout@v3

    - name: Download Repository TAR
      id: auth
      uses: google-github-actions/auth@v0
      with:
        token_format: access_token
        workload_identity_provider: <CREDENTIALS>
        service_account: <ACCOUNT>
        access_token_lifetime: 300s
    - uses: docker/login-action@v2
      with:
        registry: us-west1-docker.pkg.dev
        username: <USERNAME>
        password: ${{ steps.auth.outputs.access_token }}
    - uses: actions/download-artifact@v3
      with:
        name: repository-artifact
    - run: docker load --input ./repository-image.tar
    - run: RELEASE_TAG=${{ needs.build_job.outputs.release_tag_dev }} docker compose up -d

    - name: Run Tests
      uses: actions/setup-node@v3
    - run: npm ci --cache .npm --prefer-offline
    - run: npx playwright install
    - run: npx playwright test
      env:
          PLAYWRIGHT_SERVER: "http://localhost:3000"

docker-compose.yml

version: "1"

services:
  mongodb:
    image: "mongo"
    environment:
      MONGO_INITDB_ROOT_USERNAME: <FOUND IN DB_CONNECTION_STRING BELOW>
      MONGO_INITDB_ROOT_PASSWORD: <FOUND IN DB_CONNECTION_STRING BELOW>
    ports:
      - "27017:27017"
    command: mongod --bind_ip 0.0.0.0

  firebase:
    image: <PRIVATE_IMAGE>
    ports:
      - "4000:4000"
      - "4400:4400"
      - "4500:4500"
      - "9099:9099"
      - "9199:9199"
    command: --project=<REPOSITORY> emulators:start

  <REPOSITORY_IN_TEST>:
    image: "specific-build:latest"
    ports:
      - "3000:3000"
    environment:
      - DB_CONNECTION_STRING=mongodb://<PRIVATE_STRING>
      - DISABLE_DB_SSL=true
      - CORS_ORIGINS=http://localhost:3000
      - FIREBASE_AUTH_EMULATOR_HOST=firebase:9099
      - NEXT_PUBLIC_FIREBASE_AUTH_EMULATOR_HOST=localhost:9099
      - NEXT_PUBLIC_SENTRY_ENVIRONMENT=ci
      - <PLAID, SENTRY, and OTHER CONTEXT>

相关问题