How To Trigger A Pipeline By Passing Runtime Variable Through Harness Rest API?

Module

Harness CI
Harness CD

Environment

Infrastructure: Kubernetes

OS: Linux

Issue

I have a Harness Pipeline that I’m unable to run programmatically by passing runtime variables through the REST API. Pipeline API docs

Resolution

Pass a Runtime YAML as the body using Pipeline Execute API

  • Sample Pipeline :
pipeline:
 name: defaultpipeline
 identifier: defaultpipelineid
 projectIdentifier: NgLabs
 orgIdentifier: default
 tags: {}
 stages:
   - stage:
       name: build
       identifier: build
       description: ""
       type: CI
       spec:
         cloneCodebase: true
         infrastructure:
           type: KubernetesDirect
           spec:
             connectorRef: k8conector
             namespace: <+input>
             automountServiceAccountToken: true
             nodeSelector: {}
             os: Linux
         execution:
           steps:
             - step:
                 type: Run
                 name: Run
                 identifier: Run
                 spec:
                   connectorRef: account.harnessImage
                   image: node
                   shell: Bash
                   command: echo "Hello World"
 properties:
   ci:
     codebase:
       connectorRef: gitconnector
       repoName: default-repo-name
       build: <+input>
 variables:
   - name: INPUT_VARIABLE
     type: String
     description: "variable description is optional "
     value: <+input>

  • Sample Runtime Input Yaml:
pipeline:
  identifier: defaultpipeline
  stages:
    - stage:
        identifier: build
        type: CI
        spec:
          infrastructure:
            type: KubernetesDirect
            spec:
              namespace: ‘value’
  properties:
    ci:
      codebase:
        build:
          spec:
            branch: ‘value’
          type: branch
  variables:
    - name: INPUT_VARIABLE
      type: String
      value: ‘value’

  • Curl request for executing pipeline using above runtime YAML:
curl -X POST \
  'https://app.harness.io/gateway/pipeline/api/pipeline/execute/ci?accountIdentifier=ACCOUNT_IDENTIFIER&projectIdentifier=PROJECT_IDENTIFIER&orgIdentifier=ORG_IDENTIFIER&moduleType=ci&notifyOnlyUser=false' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/yaml' \
  -H 'x-api-key: YOUR_API_KEY' \
  -d 'pipeline:
  identifier: ci
  stages:
    - stage:
        identifier: build
        type: CI
        spec:
          infrastructure:
            type: KubernetesDirect
            spec:
              namespace: ‘value’
  properties:
    ci:
      codebase:
        build:
          spec:
            branch: ‘value’
          type: branch
  variables:
    - name: INPUT_VARIABLE
      type: String
      value: ‘value’