logo
0
0
WeChat Login
docs: 增加官方示例

API Trigger Pipeline Example

badge badge

SAAS version API endpoint: trigger build

Example implementation: index.js

Note

This example demonstrates how to trigger a pipeline via API from outside the CNB system.

If you need to trigger directly in .cnb.yml, we recommend using the official task cnb:trigger.

How to Use

Copy the .cnb.yml content and index.js file to your CNB project for direct usage (requires Node.js to run index.js).

Alternatively, use the following commands to replace index.js:

# Trigger predefined pipeline in .cnb.yml with custom environment variables
curl -X POST \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer $CNB_TOKEN" \
     -d '{ "event": "api_trigger_in_cnb",
           "env": {
              "ENV_PAASD": "env value 1"
            }
        }' \
    $CNB_API_ENDPOINT/$CNB_REPO_SLUG/-/build/start
# Dynamically pass configuration and trigger
curl -X POST \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer $CNB_TOKEN" \
     -d '{
           "event": "api_trigger_in_api",
            "branch": "main",
            "env": {
                "ENV_PAASD": "env value 2"
            },
            "config": "main:\n  api_trigger_in_api:\n    - stages:\n      - name: pipeline config in api\n        script: echo \"pipeline config in api\"\n      - name: the passed environment variables\n        script: echo $ENV_PAASD"
        }' \
    $CNB_API_ENDPOINT/$CNB_REPO_SLUG/-/build/start