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

CircleCI中如何将流水线参数正确传递至Cypress Orb?

问题:如何将CircleCI流水线参数传递至Orb中?

我参照相关教程,尝试通过API调用在staging部署阶段运行E2E测试,流水线依赖Cypress Orb,完整配置如下:

version: 2.1
parameters:
  triggering-pipeline-id:
    type: string
    default: ""
orbs:
  discord: antonioned/discord@0.1.0
  cypress: cypress-io/cypress@3
executor: cypress/default


commands:
  approve-job-in-triggering-pipeline:
    steps:
      - run:
          name: Ping CircleCI API and approve the pending job
          command: |
            echo << pipeline.parameters.triggering-pipeline-id >>
            if ! [ -z "<< pipeline.parameters.triggering-pipeline-id >>" ] 
            then
              workflow_id=$(curl --request GET \
                --url https://circleci.com/api/v2/pipeline/<< pipeline.parameters.triggering-pipeline-id >>/workflow \
                --header "Circle-Token: $CIRCLECI_API_KEY" \
                --header "content-type: application/json" \
              | jq -r '.items[0].id')

              echo $workflow_id
              
              waiting_job_id=$(curl --request GET \
                --url https://circleci.com/api/v2/workflow/$workflow_id/job \
                --header "Circle-Token: $CIRCLECI_API_KEY" \
                --header "content-type: application/json" \
              | jq -r '.items[] | select(.name == "wait-for-triggered-pipeline").id')

              echo $waiting_job_id

              curl --request POST \
                --url https://circleci.com/api/v2/workflow/$workflow_id/approve/$waiting_job_id \
                --header "Circle-Token: $CIRCLECI_API_KEY" \
                --header "content-type: application/json"

            fi
          when: always       

workflows:
 e2e-tests:
    jobs:
    - cypress/run:
        parameters: { "triggering-pipeline-id": << pipeline.parameters.triggering-pipeline-id >> }
        name: "Firefox"
        cypress-command: npm run test:firefox
        install-browsers: true
        cypress-cache-key: custom-cypress-cache-v1-{{ arch }}-{{ checksum "package.json" }}
        cypress-cache-path: ~/.cache/custom-dir/Cypress
        working-directory: /home/circleci/project
        post-steps:
          - approve-job-in-triggering-pipeline
        context:
          - webapp-e2e

运行流水线时出现如下错误:

Error calling workflow: 'e2e-tests'
Error calling job: 'cypress/run'
Error calling command: 'approve-job-in-triggering-pipeline'
Unknown variable(s): triggering-pipeline-id

根据CircleCI文档说明:

Pipeline parameters are not available in orbs, including orbs declared locally in your config.yml file

请问如何将pipeline parameters正确传递至Orb中?


解决方案

核心问题是流水线参数无法直接在Orb(包括本地自定义命令、Orb Job的post-steps)中访问,需要通过自定义Job作为中间层传递参数:

  1. 给自定义命令添加参数定义,接收外部传入的值
  2. 创建自定义Job,接收流水线参数,在Job内部调用Cypress Orb的run命令,并将参数传递给自定义命令
  3. 在Workflow中调用自定义Job时,传入流水线参数

修改后的完整配置如下:

version: 2.1
parameters:
  triggering-pipeline-id:
    type: string
    default: ""
orbs:
  discord: antonioned/discord@0.1.0
  cypress: cypress-io/cypress@3
executor: cypress/default

commands:
  approve-job-in-triggering-pipeline:
    # 给命令添加参数定义
    parameters:
      triggering-pipeline-id:
        type: string
        default: ""
    steps:
      - run:
          name: Ping CircleCI API and approve the pending job
          command: |
            # 使用命令自身的参数,而非直接访问流水线参数
            echo << parameters.triggering-pipeline-id >>
            if ! [ -z "<< parameters.triggering-pipeline-id >>" ] 
            then
              workflow_id=$(curl --request GET \
                --url https://circleci.com/api/v2/pipeline/<< parameters.triggering-pipeline-id >>/workflow \
                --header "Circle-Token: $CIRCLECI_API_KEY" \
                --header "content-type: application/json" \
              | jq -r '.items[0].id')

              echo $workflow_id
              
              waiting_job_id=$(curl --request GET \
                --url https://circleci.com/api/v2/workflow/$workflow_id/job \
                --header "Circle-Token: $CIRCLECI_API_KEY" \
                --header "content-type: application/json" \
              | jq -r '.items[] | select(.name == "wait-for-triggered-pipeline").id')

              echo $waiting_job_id

              curl --request POST \
                --url https://circleci.com/api/v2/workflow/$workflow_id/approve/$waiting_job_id \
                --header "Circle-Token: $CIRCLECI_API_KEY" \
                --header "content-type: application/json"

            fi
          when: always       

# 创建自定义Job,作为中间层传递参数
jobs:
  e2e-firefox:
    parameters:
      triggering-pipeline-id:
        type: string
        default: ""
    executor: cypress/default
    steps:
      - cypress/run:
          name: "Firefox"
          cypress-command: npm run test:firefox
          install-browsers: true
          cypress-cache-key: custom-cypress-cache-v1-{{ arch }}-{{ checksum "package.json" }}
          cypress-cache-path: ~/.cache/custom-dir/Cypress
          working-directory: /home/circleci/project
          context:
            - webapp-e2e
      # 调用自定义命令时,传递Job的参数
      - approve-job-in-triggering-pipeline:
          triggering-pipeline-id: << parameters.triggering-pipeline-id >>

workflows:
 e2e-tests:
    jobs:
    # 调用自定义Job,传入流水线参数
    - e2e-firefox:
        triggering-pipeline-id: << pipeline.parameters.triggering-pipeline-id >>

关键修改说明

  • 自定义命令新增triggering-pipeline-id参数,不再直接访问pipeline.parameters
  • 新增自定义Jobe2e-firefox,接收流水线参数后,传递给自定义命令
  • Workflow中改为调用自定义Job,完成流水线参数的传递链路

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 11:07:11