Trigger Workflows or Pipelines Using GraphQL API

Updated 1 week ago by Archana Singh

This topic describes how to trigger a Workflow or a Pipeline using Harness GraphQL APIs.

In this topic:

Before You Begin

Step 1: Fetch Application ID

Ensure that you provide details of the correct Environment, for example, Prod,QA, or dev.

This sample returns Application ID. You can use either ID or name. In this sample, name is used.

Request
{
applicationByName(name: "Test App") {
id
name
}
}
Response
{
"data": {
"applicationByName": {
"id": "naNJDKrUTZKKdKycL5Tx4g",
"name": "Test App"
}
}
}

Step 2: Fetch Workflow or Pipeline ID

This sample returns Workflow ID. Get the Workflow variables using the same query. These details are needed to start the execution.

You can also trigger Pipeline using the same API, but use pipelineByName API to fetch the Pipeline ID and Pipeline Variables.
Request
{
workflowByName(applicationId: "naNJDKrUTZKKdKycL5Tx4g", workflowName: "multi service graphql test") {
id
name
workflowVariables {
name
type
required
}
}
}
Response
{
"data": {
"workflowByName": {
"id": "bhI2FyPrTpupdzL4QEk0_A",
"name": "multi service graphql test",
"workflowVariables": [
{
"name": "Environment",
"type": "Environment",
"required": true
},
{
"name": "InfraDefinition_Kubernetes",
"type": "Infrastructure definition",
"required": true
},
{
"name": "InfraDefinition_Kubernetes2",
"type": "Infrastructure definition",
"required": true
}
]
}
}
}

Step 3: Fetch Execution Input

Currently, only Services need Artifacts inputs to start Workflow or Pipeline execution.

This sample returns Services that require artifacts  to trigger your Workflow.

Request
query {
executionInputs(applicationId: "naNJDKrUTZKKdKycL5Tx4g"
entityId: "bhI2FyPrTpupdzL4QEk0_A"
executionType: WORKFLOW,
variableInputs: [
{
name: "Environment"
variableValue: {
type: NAME
value: "Prod"
}
},
{
name: "InfraDefinition_Kubernetes"
variableValue: {
type: NAME
value: "k8s"
}
},
{
name: "InfraDefinition_Kubernetes2"
variableValue: {
type: NAME
value: "k8s"
}
}
]){
serviceInputs{
id
name
artifactType
}
}
}
Response
{
"data": {
"executionInputs": {
"serviceInputs": [
{
"id": "XceFDCA7QgirrO8LJRphZQ",
"name": "test",
"artifactType": "DOCKER"
},
{
"id": "kqf9rA1dQ4SluU54K-ouOw",
"name": "k8s",
"artifactType": "DOCKER"
}
]
}
}
}

Step 4: Start Execution

This sample shows how to start executing your Workflow.

Request
mutation {
startExecution(input: {
applicationId: "naNJDKrUTZKKdKycL5Tx4g"
entityId: "bhI2FyPrTpupdzL4QEk0_A"
executionType: WORKFLOW,
variableInputs: [
{
name: "Environment"
variableValue: {
type: NAME
value: "Prod"
}
},
{
name: "InfraDefinition_Kubernetes"
variableValue: {
type: NAME
value: "k8s"
}
},
{
name: "InfraDefinition_Kubernetes2"
variableValue: {
type: NAME
value: "k8s"
}
}
],
serviceInputs: [ {
name: "test",
artifactValueInput: {
valueType: BUILD_NUMBER
buildNumber: {
buildNumber: "stable-perl"
artifactSourceName: "library_nginx"
}
}
},
{
name: "k8s",
artifactValueInput: {
valueType: BUILD_NUMBER
buildNumber: {
buildNumber: "latest"
artifactSourceName: "docker_hello-world"
}
}
}
]
}
){
clientMutationId
execution{
id
status
}
}
}
Response
{
"data": {
"startExecution": {
"clientMutationId": null,
"execution": {
"id": "s7QEfVP7SL-3ZUt8FBrnzQ",
"status": "RUNNING"
}
}
}
}


How did we do?