如何让GitHub Actions在PR评论后针对Staging分支运行Cypress测试?
问题
我有一个GitHub Actions工作流,会在Pull Request(PR)收到评论后运行Cypress测试。目前的问题是,测试似乎是针对master分支运行的,我希望测试能针对staging分支执行。请问如何实现PR评论后针对staging分支运行测试?
当前的YAML配置文件如下:
name: Cypress Staging Tests on: issue_comment: types: [created] jobs: identify-staging-deploy: runs-on: ubuntu-latest name: deploy to staging comment on pr if: contains(github.event.comment.body, 'Successful Gordon Deploy') steps: - name: Github API Request id: gh_request uses: octokit/request-action@v2.0.2 with: route: GET /repos/:repository/pulls/:issue_id repository: ${{ github.repository }} issue_id: ${{ github.event.issue.number }} env: GITHUB_TOKEN: github secret - name: Get PR information id: pr_info run: | echo "::set-output name=branch::${{ fromJson(steps.gh_request.outputs.data).head.ref }}" echo "::set-output name=repo_name::${{ fromJson(steps.gh_request.outputs.data).head.repo.full_name }}" echo "::set-output name=pr_title::${{ fromJson(steps.gh_request.outputs.data).title }}" - name: Checkout repo uses: actions/checkout@v2.3.4 with: token: dispatch token repository: ${{ steps.pr_info.outputs.repo_name }} ref: ${{ steps.pr_info.outputs.branch }} run-cypress-tests: needs: identify-staging-deploy runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v2 - name: Setup Node uses: actions/setup-node@v3 with: node-version: 12 - name: Install dependencies run: yarn ci:setup - name: Cypress Run id: cypress-results uses: cypress-io/github-action@v4 with: browser: chrome record: true env: CYPRESS_fileConfig: staging - name: Post PR Comment if: always() run: | gh api \ --method POST \ -H "Accept: application/vnd.github+json" \ /repos/${{ github.repository_owner }}/repo_name/issues/${{ github.event.issue.number }}/comments \ -f body='#### Cypress Tests: ${{ steps.cypress-results.outcome }}. See results at ${{ steps.cypress-results.outputs.dashboardUrl }}' env: GITHUB_TOKEN: dispatch token
解决方案
问题根源在于run-cypress-tests任务的Checkout步骤——你没指定要检出的分支,默认会拉取仓库的默认分支(也就是master)。要让测试针对staging分支运行,按以下方式修改:
方案一:直接拉取固定的staging分支
如果你的staging分支是固定命名(比如就叫staging),直接修改run-cypress-tests里的Checkout步骤,指定ref参数:
- name: Checkout uses: actions/checkout@v2 with: ref: staging token: dispatch token # 确保这个token有权限拉取staging分支
方案二:关联PR对应的代码分支(如果PR已合并到staging)
如果Successful Gordon Deploy评论表示当前PR的代码已经合并到staging,或者你需要测试PR本身的代码(对应staging环境),可以把identify-staging-deploy中获取的分支信息传递给测试任务:
- 在
identify-staging-deploy任务添加输出配置,把分支和仓库名传出去:
jobs: identify-staging-deploy: runs-on: ubuntu-latest name: deploy to staging comment on pr if: contains(github.event.comment.body, 'Successful Gordon Deploy') outputs: branch: ${{ steps.pr_info.outputs.branch }} repo_name: ${{ steps.pr_info.outputs.repo_name }} # 原有的步骤保持不变
- 在
run-cypress-tests的Checkout步骤中使用传递过来的参数:
run-cypress-tests: needs: identify-staging-deploy runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v2 with: repository: ${{ needs.identify-staging-deploy.outputs.repo_name }} ref: ${{ needs.identify-staging-deploy.outputs.branch }} token: dispatch token # 原有的其他步骤保持不变
额外修正:修复PR评论中的仓库名错误
你在Post PR Comment步骤里写死了repo_name,应该用${{ github.repository }}动态获取当前仓库,避免硬编码出错:
/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/comments
内容的提问来源于stack exchange,提问作者Rinkala
相关产品推荐
相关产品推荐

