You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Azure Pipelines YML实现Playwright仅单次安装复用?

解决方案:让Playwright安装仅执行一次并复用依赖

要实现npx playwright install --with-deps仅执行一次,后续DEV/STAGE/PROD任务复用已安装的Playwright,你可以将安装步骤拆分为独立的前置作业,通过流水线工件共享已安装的依赖目录。以下是调整后的Azure Pipelines YAML配置:

variables:
  - group: variablesPlaywrightTests

jobs:
# 前置作业:仅执行一次的Playwright依赖安装
- job: SetupPlaywright
  displayName: 'Setup Playwright Dependencies'
  pool:
    vmImage: 'ubuntu-latest'
  steps:
  - task: NodeTool@0
    inputs:
      versionSpec: '20.x'
    displayName: 'Install Node.js'
  
  - script: npm ci
    displayName: 'Install npm dependencies'

  - script: npx playwright install --with-deps
    displayName: 'Install Playwright Browsers and System Dependencies'

  # 发布node_modules和Playwright浏览器目录作为流水线工件
  - task: PublishPipelineArtifact@1
    inputs:
      targetPath: 'node_modules/'
      artifactName: 'node_modules'
      publishLocation: 'pipeline'

  - task: PublishPipelineArtifact@1
    inputs:
      targetPath: '.playwright/'
      artifactName: 'playwright-browsers'
      publishLocation: 'pipeline'

# 测试作业:复用前置作业的依赖,并行执行各环境测试
- job: PlaywrightTests
  displayName: 'Playwright Tests'
  pool:
    vmImage: 'ubuntu-latest'
  dependsOn: SetupPlaywright # 依赖前置安装作业
  strategy:
    matrix:
      DEV:
        TEST_ENV: 'DEV'
        RUN_ENV: $(RunDEV)
      STAGE:
        TEST_ENV: 'STAGE'
        RUN_ENV: $(RunSTAGE)
      PROD:
        TEST_ENV: 'PROD'
        RUN_ENV: $(RunPROD)
  steps:
  - task: NodeTool@0
    inputs:
      versionSpec: '20.x'
    displayName: 'Install Node.js'

  # 下载前置作业发布的依赖工件
  - task: DownloadPipelineArtifact@2
    inputs:
      artifact: 'node_modules'
      path: $(System.DefaultWorkingDirectory)

  - task: DownloadPipelineArtifact@2
    inputs:
      artifact: 'playwright-browsers'
      path: $(System.DefaultWorkingDirectory)

  - script: |
      if [ "$(RUN_ENV)" == "true" ]; then
        echo "Running $(TEST_ENV) Tests"
        if [ "$(TEST_ENV)" == "DEV" ]; then
          export DEV_PASSWORD=$(DEV_PASSWORD)
          export DEV_USERNAME=$(DEV_USERNAME)
        elif [ "$(TEST_ENV)" == "STAGE" ]; then
          export STAGE_PASSWORD=$(STAGE_PASSWORD)
          export STAGE_USERNAME=$(STAGE_USERNAME)
        elif [ "$(TEST_ENV)" == "PROD" ]; then
          export PROD_PASSWORD=$(PROD_PASSWORD)
          export PROD_USERNAME=$(PROD_USERNAME)
        fi
        export TEST_ENV=$(TEST_ENV)
      else
        echo "Skipping $(TEST_ENV) tests as RUN_ENV is not set to 'true'."
        exit 0
      fi
    displayName: 'Setup Environment Variables'

  - script: npx playwright test --project=$(TEST_ENV)
    displayName: 'Run Playwright Tests for $(TEST_ENV)'
    condition: eq(variables['RUN_ENV'], 'true')  

  - script: |
      if [ ! -d "playwright-report" ]; then
        mkdir -p playwright-report
      fi
    displayName: 'Create playwright-report directory if it does not exist'
    condition: eq(variables['RUN_ENV'], 'true') 

  - task: PublishPipelineArtifact@1
    condition: eq(variables['RUN_ENV'], 'true') 
    inputs:
      targetPath: 'playwright-report/'
      artifactName: 'playwright-report-$(TEST_ENV)'
      publishLocation: 'pipeline'

关键调整说明

  1. 拆分独立安装作业:新增SetupPlaywright作业,集中执行所有仅需一次的安装操作(Node.js、npm依赖、Playwright浏览器)
  2. 共享依赖工件:通过PublishPipelineArtifact将node_modules和.playwright目录发布为流水线工件,供后续作业复用
  3. 依赖控制:测试作业通过dependsOn关键字确保前置安装作业完成后才启动
  4. 移除重复步骤:测试作业中删除了重复的npm ci和npx playwright install命令,改为下载已发布的工件
  5. 保持原有逻辑:保留了各环境的变量配置、测试执行条件和报告发布逻辑

内容的提问来源于stack exchange,提问作者kwakwa98

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 15:43:12