将开发代理与 Azure Pipelines 配合使用

将开发代理与 Azure Pipelines 配合使用是在受控环境中测试应用程序的好方法。 执行这些步骤后,可以将开发代理与 Azure Pipelines 配合使用。

注意

在本文中,我们使用适用于 Azure Pipelines 的 Ubuntu 代理。

安装开发代理并缓存它

首先,在代理上安装开发代理,但仅在尚未安装时执行此操作。 若要安装和缓存开发代理,请将以下步骤添加到管道文件:

variables:
- name: DEV_PROXY_VERSION
  value: v0.29.2
- name: DEV_PROXY_CACHE_RESTORED
  value: 'false'

steps:
- task: Cache@2
  inputs:
    key: '"dev-proxy-$(DEV_PROXY_VERSION)"'
    path: ./devproxy
    cacheHitVar: DEV_PROXY_CACHE_RESTORED
  displayName: Cache Dev Proxy

- script: bash -c "$(curl -sL https://aka.ms/devproxy/setup.sh)" $DEV_PROXY_VERSION
  displayName: 'Install Dev Proxy'
  condition: ne(variables.DEV_PROXY_CACHE_RESTORED, 'true')

运行开发代理

在 CI/CD 管道中运行开发代理时,需要 从脚本启动它,以便可以正常关闭它。 准备好脚本后,在管道文件中调用它:

- script: bash ./run.sh
  displayName: 'Run Dev Proxy'

将开发代理日志作为项目上传

在 CI/CD 管道中运行开发代理时,可能需要稍后分析日志。 若要保留开发代理日志,可以将其作为项目上传:

- task: PublishPipelineArtifact@1
  displayName: Upload Dev Proxy logs
  inputs:
    targetPath: $(LOG_FILE)
    artifact: $(LOG_FILE)

上传开发代理报告

如果使用开发代理来分析请求,则可能还希望将报表作为项目上传:

- script: |
    mkdir -p $(Build.ArtifactStagingDirectory)/Reports
    for file in *Reporter*; do
      if [ -f "$file" ]; then
        cp "$file" $(Build.ArtifactStagingDirectory)/Reports
      fi
    done
  displayName: 'Copy reports to artifact directory'

- task: PublishPipelineArtifact@1
  displayName: Upload Dev Proxy reports
  inputs:
    targetPath: '$(Build.ArtifactStagingDirectory)/Reports'
    artifact: 'Reports'

示例管道文件

下面是一个管道文件示例,该文件安装、运行开发代理,并将日志和报表作为项目上传:

trigger:
- main
- dev

pool:
  vmImage: ubuntu-latest

variables:
- name: LOG_FILE
  value: devproxy.log
- name: DEV_PROXY_VERSION
  value: v0.29.2
- name: DEV_PROXY_CACHE_RESTORED
  value: 'false'
- name: PLAYWRIGHT_CACHE_RESTORED
  value: 'false'

steps:

- task: UseNode@1
  inputs:
    version: '20.x'
  displayName: 'Install Node.js'

- script: npm ci
  displayName: 'Install dependencies'

#################################
# Cache + install of Playwright #
#################################
- script: |
    PLAYWRIGHT_VERSION=$(npm ls @playwright/test | grep @playwright | sed 's/.*@//')
    echo "Playwright's Version: $PLAYWRIGHT_VERSION"
    echo "##vso[task.setvariable variable=PLAYWRIGHT_VERSION]$PLAYWRIGHT_VERSION"
  displayName: Store Playwright's Version

- task: Cache@2
  inputs:
    key: '"playwright-ubuntu-$(PLAYWRIGHT_VERSION)"'
    path: '$(HOME)/.cache/ms-playwright'
    cacheHitVar: PLAYWRIGHT_CACHE_RESTORED
  displayName: Cache Playwright Browsers for Playwright's Version

- script: npx playwright install --with-deps
  condition: ne(variables['PLAYWRIGHT_CACHE_RESTORED'], 'true')
  displayName: 'Install Playwright Browsers'

################################
# Cache + install of Dev Proxy #
################################
- task: Cache@2
  inputs:
    key: '"dev-proxy-$(DEV_PROXY_VERSION)"'
    path: ./devproxy
    cacheHitVar: DEV_PROXY_CACHE_RESTORED
  displayName: Cache Dev Proxy

- script: bash -c "$(curl -sL https://aka.ms/devproxy/setup.sh)" $DEV_PROXY_VERSION
  displayName: 'Install Dev Proxy'
  condition: ne(variables.DEV_PROXY_CACHE_RESTORED, 'true')

- script: bash ./run.sh
  displayName: 'Run Dev Proxy'

- task: PublishPipelineArtifact@1
  displayName: Upload Dev Proxy logs
  inputs:
    targetPath: $(LOG_FILE)
    artifact: $(LOG_FILE)

- script: |
    mkdir -p $(Build.ArtifactStagingDirectory)/Reports
    for file in *Reporter*; do
      if [ -f "$file" ]; then
        cp "$file" $(Build.ArtifactStagingDirectory)/Reports
      fi
    done
  displayName: 'Copy reports to artifact directory'

- task: PublishPipelineArtifact@1
  displayName: Upload Dev Proxy reports
  inputs:
    targetPath: '$(Build.ArtifactStagingDirectory)/Reports'
    artifact: 'Reports'