文档首页> 流水线 CodeArts Pipeline> API参考> API> 流水线管理(旧版,待下线)> 基于模板快速创建流水线及流水线内任务
更新时间:2024-06-20 GMT+08:00
分享

基于模板快速创建流水线及流水线内任务

功能介绍

基于模板快速创建流水线及流水线内任务

调用方法

请参见如何调用API

URI

POST /v3/templates/task

请求参数

表1 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

用户Token。 通过调用IAM服务获取用户Token接口获取(响应消息头中X-Subject-Token的值)。

表2 请求Body参数

参数

是否必选

参数类型

描述

flow

Map<String,Map<String,String>>

编排flow详情,描述流水线内各阶段任务的串并行关系。map类型数据,key为阶段名字,默认第一阶段initial,最后阶段为final,其余名字以'state_数字'标识。value为该阶段内任务(以'Task_数字'标识)以及后续阶段的标识。本字段为描述流水线基础编排数据之一,建议可通过流水线真实界面基于模板创建接口中获取

states

Map<String,TemplateState>

编排State详情,map类型数据。本字段为描述流水线基础编排数据之一,建议可通过流水线真实界面基于模板创建接口中获取

workflow

Workflow object

参数详情

表3 TemplateState

参数

是否必选

参数类型

描述

type

String

任务类型

name

String

任务名字

module_or_template_id

String

模板任务ID

module_or_template_name

String

模板任务名字

display_name

String

任务在流水线页面展示名字

dsl_method

String

流水线可挂载任务类型

parameters

Map<String,Object>

任务参数,map类型数据

is_manual_execution

Boolean

是否手动执行

job_parameter_validate

Boolean

任务参数是否校验

is_show_codehub_url

Boolean

是否显示代码仓URL

is_execute

Boolean

是否执行

job_id

String

执行任务ID

job_name

String

执行任务名字

project_id

String

任务所属项目ID

execution_mode

String

控制阶段下任务的串并行(type是task的execution_mode值为null,type是stage的execution_mode可选parallel(并行)或者serial(串行))

表4 Workflow

参数

是否必选

参数类型

描述

parameter

Array of PipelineParam objects

任务类型,list类型数据

source

Array of Source objects

源码仓,list类型数据

name

String

流水线名字

project_id

String

项目ID

project_name

String

项目名字

表5 PipelineParam

参数

是否必选

参数类型

描述

name

String

流水线参数名字

value

String

流水线参数值

description

String

流水线参数描述

param_type

String

流水线参数类型

is_static

Boolean

是否静态参数

is_default

Boolean

是否默认参数

表6 Source

参数

是否必选

参数类型

描述

codehub_name

String

源码仓名字

branches

Array of strings

触发分支

scm_type

String

源码仓来源

hook_flag

Boolean

是否开启触发执行流水线功能

branch

String

触发分支

git_url

String

源码仓ssh地址

codehub_id

String

源码仓ID

web_url

String

源码仓首页url

branch_list

Array of strings

分支列表

init_id

String

初始化ID

disable

Boolean

是否废弃

响应参数

状态码: 200

表7 响应Body参数

参数

参数类型

描述

task_id

String

实例ID

状态码: 400

表8 响应Body参数

参数

参数类型

描述

error_msg

String

错误信息

error_code

String

错误码

请求示例

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
}

响应示例

状态码: 200

OK

{
  "task_id" : "eafaff0bf9094d3d8456d13dddf67b40"
}

SDK代码示例

SDK代码示例如下。

  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)
    }
}

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

OK

400

Bad request

401

Unauthorized

403

Forbidden

404

Not Found

错误码

请参见错误码

分享:

    相关文档

    相关产品