Help Center> CodeArts Pipeline> API Reference> API> Pipeline Management (Old Version, Unavailable Soon)> Quickly Creating a Pipeline and Jobs with a Template
Updated on 2024-06-14 GMT+08:00

Quickly Creating a Pipeline and Jobs with a Template

Function

This API is used to quickly create a pipeline and jobs with a template.

Calling Method

For details, see Calling APIs.

URI

POST /v3/templates/task

Request Parameters

Table 1 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

No

String

User token. It can be obtained by calling the IAM API used to obtain a user token. The value of X-Subject-Token in the response header is the user token.

Table 2 Request body parameters

Parameter

Mandatory

Type

Description

flow

Yes

Map<String,Map<String,String>>

Flow orchestration details, which describe the serial and parallel relationships of jobs in each stage of a pipeline. A Map type value, key indicates the stage name. By default, the first stage is initial, the last stage is final, and other stages are identified by state_digit. The value is the ID of the task (identified by Task_digit) in the stage and the ID of the subsequent stage. This field is one of the basic orchestration data of a pipeline. It is advised to obtain the value from the template-based creation API on the pipeline web UI.

states

Yes

Map<String,TemplateState>

State orchestration details. It is a Map type value. This field is one of the basic orchestration data of a pipeline. It is advised to obtain the value from the template-based creation API on the pipeline web UI.

workflow

Yes

Workflow object

Parameter details.

Table 3 TemplateState

Parameter

Mandatory

Type

Description

type

Yes

String

Job type.

name

Yes

String

Job name.

module_or_template_id

Yes

String

Template job ID.

module_or_template_name

Yes

String

Template job name.

display_name

Yes

String

Job name displayed on the pipeline page.

dsl_method

Yes

String

Types of jobs that can be mounted to a pipeline.

parameters

Yes

Map<String,Object>

Job parameter, which is a value of map type.

is_manual_execution

Yes

Boolean

Whether to manually execute the job.

job_parameter_validate

Yes

Boolean

Whether to verify job parameters.

is_show_codehub_url

Yes

Boolean

Whether to display the code repository URL.

is_execute

Yes

Boolean

Whether to execute

job_id

Yes

String

ID of the executed job.

job_name

Yes

String

Name of the executed job.

project_id

Yes

String

ID of the project to which the job belongs.

execution_mode

Yes

String

This parameter controls the execution mode of jobs in a stage. If type is task, the value of execution_mode is null. If type is stage, the value of execution_mode can be parallel or serial.

Table 4 Workflow

Parameter

Mandatory

Type

Description

parameter

Yes

Array of PipelineParam objects

Job type, which is a value of list type.

source

Yes

Array of Source objects

Source code repository, which is a value of list type.

name

Yes

String

Pipeline name.

project_id

Yes

String

Project ID.

project_name

Yes

String

Project name.

Table 5 PipelineParam

Parameter

Mandatory

Type

Description

name

Yes

String

Pipeline parameter name.

value

Yes

String

Pipeline parameter value.

description

Yes

String

Pipeline parameter description.

param_type

Yes

String

Pipeline parameter type.

is_static

Yes

Boolean

Whether it is a static parameter.

is_default

Yes

Boolean

Whether it is a default parameter.

Table 6 Source

Parameter

Mandatory

Type

Description

codehub_name

Yes

String

Source code repository name.

branches

Yes

Array of strings

Triggered branch

scm_type

Yes

String

Source code repository type.

hook_flag

Yes

Boolean

Whether to enable the function of triggering pipeline execution.

branch

Yes

String

Triggered branch.

git_url

Yes

String

SSH address of the source code repository.

codehub_id

Yes

String

Source code repository ID.

web_url

Yes

String

URL of the source code repository homepage.

branch_list

Yes

Array of strings

Branch list.

init_id

Yes

String

Initialization ID.

disable

Yes

Boolean

Whether to discard

Response Parameters

Status code: 200

Table 7 Response body parameters

Parameter

Type

Description

task_id

String

Instance ID.

Status code: 400

Table 8 Response body parameters

Parameter

Type

Description

error_msg

String

Error message.

error_code

String

Error code.

Example Requests

POST https://{endpoint}/v3/templates/task

{
  "flow" : {
    "initial" : {
      "state_3" : "always"
    },
    "state_3" : {
      "state_4" : "always"
    },
    "state_4" : {
      "state_5" : "always",
      "Task_1" : "always",
      "Task_2" : "always"
    },
    "state_5" : {
      "final" : "always"
    }
  },
  "states" : {
    "initial" : {
      "type" : "stage",
      "name" : "initial",
      "module_or_template_id" : null,
      "module_or_template_name" : null,
      "display_name" : "Initial",
      "dsl_method" : "initial",
      "parameters" : {
        "display" : "hidden"
      },
      "params" : null,
      "is_manual_mxecution" : false,
      "execution_mode" : "parallel"
    },
    "state_3" : {
      "type" : "stage",
      "name" : "state_3",
      "module_or_template_id" : null,
      "module_or_template_name" : null,
      "display_name" : "Source",
      "dsl_method" : "source",
      "parameters" : null,
      "params" : null,
      "is_manual_execution" : false,
      "execution_mode" : "parallel"
    },
    "Task_1" : {
      "type" : "task",
      "name" : "Task_1",
      "module_or_template_id" : "00012",
      "module_or_template_name" : "Maven",
      "display_name" : "Build_Task",
      "dsl_method" : "buildJob",
      "parameters" : {
        "__repository__" : {
          "repo_name" : "fdfdfdf",
          "url" : "git@example.com:CloudPipelineTest-Copy00001/fdfdfdf.git"
        }
      },
      "params" : null,
      "is_manual_execution" : false,
      "execution_mode" : null,
      "job_parameter_palidate" : true,
      "is_show_code_hub_url" : false,
      "is_execute" : true,
      "job_id" : "00012",
      "job_name" : "Maven",
      "project_id" : "4719b59466624abfbae0f83c3ce3a0dc"
    },
    "Task_2" : {
      "type" : "task",
      "name" : "Task_2",
      "module_or_template_id" : "d7dffaefb6d94c63a09cf141668356c7",
      "module_or_template_name" : "codecheck_template",
      "display_name" : "Code_Check_Task",
      "dsl_method" : "codeCheck",
      "parameters" : {
        "language" : {
          "disable" : null,
          "name" : "Java",
          "display_name" : "Java",
          "id" : "782aade71ffb4ab780303555eb2e4825",
          "language" : "java"
        },
        "codeBranch" : "master",
        "__repository__" : {
          "repo_name" : "fdfdfdf",
          "url" : "git@example.com:CloudPipelineTest-Copy00001/fdfdfdf.git"
        }
      },
      "params" : null,
      "is_manual_execution" : false,
      "execution_mode" : null,
      "job_parameter_palidate" : true,
      "is_show_code_hub_url" : false,
      "is_execute" : true,
      "job_id" : "d7dffaefb6d94c63a09cf141668356c7",
      "job_name" : "codecheck_template",
      "project_id" : "4719b59466624abfbae0f83c3ce3a0dc"
    },
    "state_4" : {
      "type" : "stage",
      "name" : "state_4",
      "module_or_template_id" : null,
      "module_or_template_name" : null,
      "display_name" : "Build_and_Check",
      "dsl_method" : "build",
      "parameters" : null,
      "params" : null,
      "is_manual_mxecution" : false,
      "execution_mode" : "parallel"
    },
    "state_5" : {
      "type" : "stage",
      "name" : "state_5",
      "module_or_template_id" : null,
      "module_or_template_name" : null,
      "display_name" : "Release",
      "dsl_method" : "release",
      "parameters" : null,
      "params" : null,
      "is_manual_mxecution" : false,
      "execution_mode" : "parallel"
    },
    "final" : {
      "type" : "stage",
      "name" : "final",
      "module_or_template_id" : null,
      "module_or_template_name" : null,
      "display_name" : "Final",
      "dsl_method" : "final",
      "parameters" : null,
      "params" : null,
      "is_manual_mxecution" : false,
      "execution_mode" : "parallel"
    }
  },
  "workflow" : {
    "parameter" : [ {
      "name" : "serviceName",
      "value" : "pipeline-20201229165622",
      "description" : "this is a text type param",
      "paramtype" : "normalparam",
      "display_name" : null,
      "isStatic" : true,
      "isDefault" : true,
      "constraints" : null
    }, {
      "name" : "releaseVersion",
      "value" : "1.0.0",
      "description" : "this is a growth type param",
      "paramtype" : "growthparam",
      "display_name" : null,
      "isStatic" : false,
      "isDefault" : true,
      "constraints" : null
    } ],
    "source" : [ {
      "scmType" : "codehub",
      "codeHubName" : "fdfdfdf",
      "hookFlag" : false,
      "branches" : [ ],
      "defaultBranch" : "master",
      "branch" : "",
      "codeHubId" : "5224040",
      "gitUrl" : "git@example.com:CloudPipelineTest-Copy00001/fdfdfdf.git",
      "webUrl" : "https://example.com/codehub/project/bc97c4d99c7d4f98bb6cd104a77fddd6/codehub/140000127/home",
      "branchList" : [ "master" ]
    } ],
    "name" : "pipeline-123123",
    "description" : "",
    "project_id" : "bc97c4d99c7d4f98bb6cd104a77fddd6",
    "project_name" : "CloudPipelineTest-Copy"
  },
  "changeInfo" : null
}

Example Responses

Status code: 200

OK

{
  "task_id" : "eafaff0bf9094d3d8456d13dddf67b40"
}

SDK Sample Code

The SDK sample code is as follows.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.codeartspipeline.v2.region.CodeArtsPipelineRegion;
import com.huaweicloud.sdk.codeartspipeline.v2.*;
import com.huaweicloud.sdk.codeartspipeline.v2.model.*;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

public class CreatePipelineByTemplateSolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        CodeArtsPipelineClient client = CodeArtsPipelineClient.newBuilder()
                .withCredential(auth)
                .withRegion(CodeArtsPipelineRegion.valueOf("<YOUR REGION>"))
                .build();
        CreatePipelineByTemplateRequest request = new CreatePipelineByTemplateRequest();
        TemplateCddl body = new TemplateCddl();
        List<Source> listWorkflowSource = new ArrayList<>();
        listWorkflowSource.add(
            new Source()
                .withBranches()
                .withBranch("")
        );
        List<PipelineParam> listWorkflowParameter = new ArrayList<>();
        listWorkflowParameter.add(
            new PipelineParam()
                .withName("serviceName")
                .withValue("pipeline-20201229165622")
                .withDescription("this is a text type param")
        );
        listWorkflowParameter.add(
            new PipelineParam()
                .withName("releaseVersion")
                .withValue("1.0.0")
                .withDescription("this is a growth type param")
        );
        Workflow workflowbody = new Workflow();
        workflowbody.withParameter(listWorkflowParameter)
            .withSource(listWorkflowSource)
            .withName("pipeline-123123")
            .withProjectId("bc97c4d99c7d4f98bb6cd104a77fddd6")
            .withProjectName("CloudPipelineTest-Copy");
        TemplateState statesStates = new TemplateState();
        statesStates.withType("stage")
            .withName("final")
            .withDisplayName("Final")
            .withDslMethod("final")
            .withExecutionMode("parallel");
        TemplateState statesStates1 = new TemplateState();
        statesStates1.withType("stage")
            .withName("state_5")
            .withDisplayName("Release")
            .withDslMethod("release")
            .withExecutionMode("parallel");
        TemplateState statesStates2 = new TemplateState();
        statesStates2.withType("stage")
            .withName("state_4")
            .withDisplayName("Build_and_Check")
            .withDslMethod("build")
            .withExecutionMode("parallel");
        Map<String, Object> listStatesParameters = new HashMap<>();
        listStatesParameters.put("language", "{\"name\":\"Java\",\"language\":\"java\",\"id\":\"782aade71ffb4ab780303555eb2e4825\",\"display_name\":\"Java\"}");
        listStatesParameters.put("codeBranch", "master");
        listStatesParameters.put("__repository__", "{\"repo_name\":\"fdfdfdf\",\"url\":\"git@example.com:CloudPipelineTest-Copy00001/fdfdfdf.git\"}");
        TemplateState statesStates3 = new TemplateState();
        statesStates3.withType("task")
            .withName("Task_2")
            .withModuleOrTemplateId("d7dffaefb6d94c63a09cf141668356c7")
            .withModuleOrTemplateName("codecheck_template")
            .withDisplayName("Code_Check_Task")
            .withDslMethod("codeCheck")
            .withParameters(listStatesParameters)
            .withIsManualExecution(false)
            .withIsExecute(true)
            .withJobId("d7dffaefb6d94c63a09cf141668356c7")
            .withJobName("codecheck_template")
            .withProjectId("4719b59466624abfbae0f83c3ce3a0dc");
        Map<String, Object> listStatesParameters1 = new HashMap<>();
        listStatesParameters1.put("__repository__", "{\"repo_name\":\"fdfdfdf\",\"url\":\"git@example.com:CloudPipelineTest-Copy00001/fdfdfdf.git\"}");
        TemplateState statesStates4 = new TemplateState();
        statesStates4.withType("task")
            .withName("Task_1")
            .withModuleOrTemplateId("00012")
            .withModuleOrTemplateName("Maven")
            .withDisplayName("Build_Task")
            .withDslMethod("buildJob")
            .withParameters(listStatesParameters1)
            .withIsManualExecution(false)
            .withIsExecute(true)
            .withJobId("00012")
            .withJobName("Maven")
            .withProjectId("4719b59466624abfbae0f83c3ce3a0dc");
        TemplateState statesStates5 = new TemplateState();
        statesStates5.withType("stage")
            .withName("state_3")
            .withDisplayName("Source")
            .withDslMethod("source")
            .withIsManualExecution(false)
            .withExecutionMode("parallel");
        Map<String, Object> listStatesParameters2 = new HashMap<>();
        listStatesParameters2.put("display", "hidden");
        TemplateState statesStates6 = new TemplateState();
        statesStates6.withType("stage")
            .withName("initial")
            .withDisplayName("Initial")
            .withDslMethod("initial")
            .withParameters(listStatesParameters2)
            .withExecutionMode("parallel");
        Map<String, TemplateState> listbodyStates = new HashMap<>();
        listbodyStates.put("initial", statesStates6);
        listbodyStates.put("state_3", statesStates5);
        listbodyStates.put("Task_1", statesStates4);
        listbodyStates.put("Task_2", statesStates3);
        listbodyStates.put("state_4", statesStates2);
        listbodyStates.put("state_5", statesStates1);
        listbodyStates.put("final", statesStates);
        Map<String, String> listFlowFlow = new HashMap<>();
        listFlowFlow.put("final", "always");
        Map<String, String> listFlowFlow1 = new HashMap<>();
        listFlowFlow1.put("state_5", "always");
        listFlowFlow1.put("Task_1", "always");
        listFlowFlow1.put("Task_2", "always");
        Map<String, String> listFlowFlow2 = new HashMap<>();
        listFlowFlow2.put("state_4", "always");
        Map<String, String> listFlowFlow3 = new HashMap<>();
        listFlowFlow3.put("state_3", "always");
        Map<String, Map<String, String>> listbodyFlow = new HashMap<>();
        listbodyFlow.put("initial", listFlowFlow3);
        listbodyFlow.put("state_3", listFlowFlow2);
        listbodyFlow.put("state_4", listFlowFlow1);
        listbodyFlow.put("state_5", listFlowFlow);
        body.withWorkflow(workflowbody);
        body.withStates(listbodyStates);
        body.withFlow(listbodyFlow);
        request.withBody(body);
        try {
            CreatePipelineByTemplateResponse response = client.createPipelineByTemplate(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcodeartspipeline.v2.region.codeartspipeline_region import CodeArtsPipelineRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkcodeartspipeline.v2 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = os.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]

    credentials = BasicCredentials(ak, sk)

    client = CodeArtsPipelineClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(CodeArtsPipelineRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = CreatePipelineByTemplateRequest()
        listSourceWorkflow = [
            Source(
                branch=""
            )
        ]
        listParameterWorkflow = [
            PipelineParam(
                name="serviceName",
                value="pipeline-20201229165622",
                description="this is a text type param"
            ),
            PipelineParam(
                name="releaseVersion",
                value="1.0.0",
                description="this is a growth type param"
            )
        ]
        workflowbody = Workflow(
            parameter=listParameterWorkflow,
            source=listSourceWorkflow,
            name="pipeline-123123",
            project_id="bc97c4d99c7d4f98bb6cd104a77fddd6",
            project_name="CloudPipelineTest-Copy"
        )
        statesStates = TemplateState(
            type="stage",
            name="final",
            display_name="Final",
            dsl_method="final",
            execution_mode="parallel"
        )
        statesStates1 = TemplateState(
            type="stage",
            name="state_5",
            display_name="Release",
            dsl_method="release",
            execution_mode="parallel"
        )
        statesStates2 = TemplateState(
            type="stage",
            name="state_4",
            display_name="Build_and_Check",
            dsl_method="build",
            execution_mode="parallel"
        )
        listParametersStates = {
            "language": "{\"name\":\"Java\",\"language\":\"java\",\"id\":\"782aade71ffb4ab780303555eb2e4825\",\"display_name\":\"Java\"}",
            "codeBranch": "master",
            "__repository__": "{\"repo_name\":\"fdfdfdf\",\"url\":\"git@example.com:CloudPipelineTest-Copy00001/fdfdfdf.git\"}"
        }
        statesStates3 = TemplateState(
            type="task",
            name="Task_2",
            module_or_template_id="d7dffaefb6d94c63a09cf141668356c7",
            module_or_template_name="codecheck_template",
            display_name="Code_Check_Task",
            dsl_method="codeCheck",
            parameters=listParametersStates,
            is_manual_execution=False,
            is_execute=True,
            job_id="d7dffaefb6d94c63a09cf141668356c7",
            job_name="codecheck_template",
            project_id="4719b59466624abfbae0f83c3ce3a0dc"
        )
        listParametersStates1 = {
            "__repository__": "{\"repo_name\":\"fdfdfdf\",\"url\":\"git@example.com:CloudPipelineTest-Copy00001/fdfdfdf.git\"}"
        }
        statesStates4 = TemplateState(
            type="task",
            name="Task_1",
            module_or_template_id="00012",
            module_or_template_name="Maven",
            display_name="Build_Task",
            dsl_method="buildJob",
            parameters=listParametersStates1,
            is_manual_execution=False,
            is_execute=True,
            job_id="00012",
            job_name="Maven",
            project_id="4719b59466624abfbae0f83c3ce3a0dc"
        )
        statesStates5 = TemplateState(
            type="stage",
            name="state_3",
            display_name="Source",
            dsl_method="source",
            is_manual_execution=False,
            execution_mode="parallel"
        )
        listParametersStates2 = {
            "display": "hidden"
        }
        statesStates6 = TemplateState(
            type="stage",
            name="initial",
            display_name="Initial",
            dsl_method="initial",
            parameters=listParametersStates2,
            execution_mode="parallel"
        )
        listStatesbody = {
            "initial": statesStates6,
            "state_3": statesStates5,
            "Task_1": statesStates4,
            "Task_2": statesStates3,
            "state_4": statesStates2,
            "state_5": statesStates1,
            "final": statesStates
        }
        listFlowFlow = {
            "final": "always"
        }
        listFlowFlow1 = {
            "state_5": "always",
            "Task_1": "always",
            "Task_2": "always"
        }
        listFlowFlow2 = {
            "state_4": "always"
        }
        listFlowFlow3 = {
            "state_3": "always"
        }
        listFlowbody = {
            "initial": listFlowFlow3,
            "state_3": listFlowFlow2,
            "state_4": listFlowFlow1,
            "state_5": listFlowFlow
        }
        request.body = TemplateCddl(
            workflow=workflowbody,
            states=listStatesbody,
            flow=listFlowbody
        )
        response = client.create_pipeline_by_template(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    codeartspipeline "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/codeartspipeline/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/codeartspipeline/v2/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/codeartspipeline/v2/region"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        Build()

    client := codeartspipeline.NewCodeArtsPipelineClient(
        codeartspipeline.CodeArtsPipelineClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.CreatePipelineByTemplateRequest{}
	var listSourceWorkflow = []model.Source{
        {
            Branch: "",
        },
    }
	var listParameterWorkflow = []model.PipelineParam{
        {
            Name: "serviceName",
            Value: "pipeline-20201229165622",
            Description: "this is a text type param",
        },
        {
            Name: "releaseVersion",
            Value: "1.0.0",
            Description: "this is a growth type param",
        },
    }
	workflowbody := &model.Workflow{
		Parameter: listParameterWorkflow,
		Source: listSourceWorkflow,
		Name: "pipeline-123123",
		ProjectId: "bc97c4d99c7d4f98bb6cd104a77fddd6",
		ProjectName: "CloudPipelineTest-Copy",
	}
	statesStates := model.TemplateState{
		Type: "stage",
		Name: "final",
		DisplayName: "Final",
		DslMethod: "final",
		ExecutionMode: "parallel",
	}
	statesStates1 := model.TemplateState{
		Type: "stage",
		Name: "state_5",
		DisplayName: "Release",
		DslMethod: "release",
		ExecutionMode: "parallel",
	}
	statesStates2 := model.TemplateState{
		Type: "stage",
		Name: "state_4",
		DisplayName: "Build_and_Check",
		DslMethod: "build",
		ExecutionMode: "parallel",
	}
	var listParametersStates = map[string]interface{}{
        "language": "{\"name\":\"Java\",\"language\":\"java\",\"id\":\"782aade71ffb4ab780303555eb2e4825\",\"display_name\":\"Java\"}",
        "codeBranch": "master",
        "__repository__": "{\"repo_name\":\"fdfdfdf\",\"url\":\"git@example.com:CloudPipelineTest-Copy00001/fdfdfdf.git\"}",
    }
	statesStates3 := model.TemplateState{
		Type: "task",
		Name: "Task_2",
		ModuleOrTemplateId: "d7dffaefb6d94c63a09cf141668356c7",
		ModuleOrTemplateName: "codecheck_template",
		DisplayName: "Code_Check_Task",
		DslMethod: "codeCheck",
		Parameters: listParametersStates,
		IsManualExecution: false,
		IsExecute: true,
		JobId: "d7dffaefb6d94c63a09cf141668356c7",
		JobName: "codecheck_template",
		ProjectId: "4719b59466624abfbae0f83c3ce3a0dc",
	}
	var listParametersStates1 = map[string]interface{}{
        "__repository__": "{\"repo_name\":\"fdfdfdf\",\"url\":\"git@example.com:CloudPipelineTest-Copy00001/fdfdfdf.git\"}",
    }
	statesStates4 := model.TemplateState{
		Type: "task",
		Name: "Task_1",
		ModuleOrTemplateId: "00012",
		ModuleOrTemplateName: "Maven",
		DisplayName: "Build_Task",
		DslMethod: "buildJob",
		Parameters: listParametersStates1,
		IsManualExecution: false,
		IsExecute: true,
		JobId: "00012",
		JobName: "Maven",
		ProjectId: "4719b59466624abfbae0f83c3ce3a0dc",
	}
	statesStates5 := model.TemplateState{
		Type: "stage",
		Name: "state_3",
		DisplayName: "Source",
		DslMethod: "source",
		IsManualExecution: false,
		ExecutionMode: "parallel",
	}
	var listParametersStates2 = map[string]interface{}{
        "display": "hidden",
    }
	statesStates6 := model.TemplateState{
		Type: "stage",
		Name: "initial",
		DisplayName: "Initial",
		DslMethod: "initial",
		Parameters: listParametersStates2,
		ExecutionMode: "parallel",
	}
	var listStatesbody = map[string](model.TemplateState){
        "initial": statesStates6,
        "state_3": statesStates5,
        "Task_1": statesStates4,
        "Task_2": statesStates3,
        "state_4": statesStates2,
        "state_5": statesStates1,
        "final": statesStates,
    }
	var listFlowFlow = map[string]string{
        "final": "always",
    }
	var listFlowFlow2 = map[string]string{
        "state_5": "always",
        "Task_1": "always",
        "Task_2": "always",
    }
	var listFlowFlow6 = map[string]string{
        "state_4": "always",
    }
	var listFlowFlow8 = map[string]string{
        "state_3": "always",
    }
	var listFlowbody = map[string]map[string]string{
        "initial": listFlowFlow8,
        "state_3": listFlowFlow6,
        "state_4": listFlowFlow2,
        "state_5": listFlowFlow,
    }
	request.Body = &model.TemplateCddl{
		Workflow: workflowbody,
		States: listStatesbody,
		Flow: listFlowbody,
	}
	response, err := client.CreatePipelineByTemplate(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

For SDK sample code of more programming languages, see the Sample Code tab in API Explorer. SDK sample code can be automatically generated.

Status Codes

Status Code

Description

200

OK

400

Bad request

401

Unauthorized

403

Forbidden

404

Not Found

Error Codes

See Error Codes.