如何在GitHub Actions工作流中获取多个部署URL?
问题:GitHub Actions中获取Pull Request关联的两个部署URL
我需要在每个Pull Request上运行Playwright测试的GitHub Actions工作流中,获取该PR关联的两个部署URL。当前工作流尝试访问github.event.deployments数组但返回null,使用github.event.deployment_status.target_url只能拿到最新的单个部署URL,且不想使用GitHub API。
原工作流代码:
name: Playwright Tests on: deployment_status: workflow_dispatch: inputs: environment: description: 'Environment to run tests against' type: environment required: true jobs: test: timeout-minutes: 60 runs-on: ubuntu-latest if: github.event.deployment_status.state == 'success' steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 with: node-version: '18.x' - name: Install dependencies run: yarn install - name: Go to Dashboard directory run: cd apps/dashboard - name: Install Playwright run: npx playwright install --with-deps - name: Get deployment URLs run: | deployment_urls=() for deployment in "${{ toJson(github.event.deployments) }}"; do target_url=$(echo $deployment | jq -r '.target_url') deployment_urls+=($target_url) done echo "Deployment URLs: ${deployment_urls[@]}" id: get_urls - name: Run Playwright tests run: npx playwright test env: DEPLOYMENT_URL1: ${{ steps.get_urls.outputs.deployment_urls[0] }} DEPLOYMENT_URL2: ${{ steps.get_urls.outputs.deployment_urls[1] }} - name: Upload Playwright reports uses: actions/upload-artifact@v3 with: name: playwright-report path: playwright-report
更新: 已将github.deployments替换为github.deployment,但仍只能获取到最新的单个部署信息。
错误原因
deployment_status事件的GitHub上下文不存在github.event.deployments数组,该事件仅携带当前触发它的单个部署的信息(对应github.event.deployment对象),因此原代码中遍历github.event.deployments会返回null。
解决方案(无需API)
利用GitHub Actions的缓存功能,在两次部署触发工作流时分别收集URL,待两个URL都收集完成后再运行Playwright测试。具体实现如下:
name: Playwright Tests on: deployment_status: workflow_dispatch: inputs: environment: description: '环境名称' type: environment required: true jobs: collect-deployment-urls: runs-on: ubuntu-latest if: github.event.deployment_status.state == 'success' outputs: deployment_urls: ${{ steps.merge-urls.outputs.deployment_urls }} steps: - name: 提取当前部署URL和PR编号 id: extract-info run: | PR_NUMBER=${{ github.event.deployment.pull_requests[0].number }} CURRENT_URL=${{ github.event.deployment_status.target_url }} echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT echo "current_url=$CURRENT_URL" >> $GITHUB_OUTPUT - name: 读取缓存中的已有URL id: read-cache uses: actions/cache@v3 with: path: deployment-urls.txt key: deployment-urls-pr-${{ steps.extract-info.outputs.pr_number }} restore-keys: deployment-urls-pr-${{ steps.extract-info.outputs.pr_number }} - name: 合并并去重部署URL id: merge-urls run: | # 初始化URL列表 urls=() # 读取缓存中的已有URL if [ -f deployment-urls.txt ]; then while read -r url; do urls+=("$url") done < deployment-urls.txt fi # 添加当前URL(避免重复) CURRENT_URL=${{ steps.extract-info.outputs.current_url }} if [[ ! " ${urls[@]} " =~ " ${CURRENT_URL} " ]]; then urls+=("$CURRENT_URL") fi # 写入缓存文件 printf "%s\n" "${urls[@]}" > deployment-urls.txt # 输出逗号分隔的URL列表 echo "deployment_urls=$(IFS=,; echo "${urls[*]}")" >> $GITHUB_OUTPUT - name: 更新缓存 uses: actions/cache@v3 with: path: deployment-urls.txt key: deployment-urls-pr-${{ steps.extract-info.outputs.pr_number }}-${{ steps.merge-urls.outputs.deployment_urls }} run-tests: needs: collect-deployment-urls runs-on: ubuntu-latest # 仅当收集到至少两个URL时触发测试 if: ${{ contains(needs.collect-deployment-urls.outputs.deployment_urls, ',') }} steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 with: node-version: '18.x' - name: 安装依赖 run: yarn install - name: 进入Dashboard目录 run: cd apps/dashboard - name: 安装Playwright依赖 run: npx playwright install --with-deps - name: 设置部署URL环境变量 run: | IFS=',' read -r -a urls <<< "${{ needs.collect-deployment-urls.outputs.deployment_urls }}" echo "DEPLOYMENT_URL1=${urls[0]}" >> $GITHUB_ENV echo "DEPLOYMENT_URL2=${urls[1]}" >> $GITHUB_ENV - name: 运行Playwright测试 run: npx playwright test env: DEPLOYMENT_URL1: ${{ env.DEPLOYMENT_URL1 }} DEPLOYMENT_URL2: ${{ env.DEPLOYMENT_URL2 }} - name: 上传测试报告 uses: actions/upload-artifact@v3 with: name: playwright-report path: playwright-report - name: 清理PR关联的缓存 uses: actions/cache@v3 with: path: deployment-urls.txt key: deployment-urls-pr-${{ github.event.deployment.pull_requests[0].number }} action: delete
方案说明
- 收集URL的Job:每次部署成功时触发,将当前部署URL存入与PR编号绑定的缓存中,自动去重后输出合并的URL列表。
- 运行测试的Job:依赖收集URL的Job,仅当URL列表包含至少两个URL时才执行测试,避免提前触发。
- 缓存清理:测试完成后自动清理当前PR的缓存,避免干扰后续的PR或部署。
内容的提问来源于stack exchange,提问作者cdhiraj40
相关产品推荐
相关产品推荐

