
# 基于模板创建流水线 - CreatePipelineByTemplateId
#### 功能介绍
基于模板创建流水线
#### 调用方法
请参见[如何调用API](https://support.huaweicloud.com/api-pipeline/pipeline_03_0006.html)。
#### URI
POST /v5/{project_id}/api/pipeline-templates/{template_id}/create-pipeline
表1路径参数 
| 参数          | 是否必选 | 参数类型   | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
|:---|:---|:---|:---|
| project_id  | 是    | String | **参数解释**： 项目ID，获取方式请参见[获取项目ID](https://support.huaweicloud.com/api-pipeline/pipeline_03_0036.html)。 **约束限制**： 不涉及。 **取值范围**： 32位字符，由数字和字母组成。 **默认取值**： 不涉及。                                |
| template_id | 是    | String | **参数解释**： 模板ID，模板的唯一标识，通过[查询模板列表](https://support.huaweicloud.com/api-pipeline/ListPipelineTemplates.html)接口获取，templates.id即为模板ID。 **约束限制**： 不涉及。 **取值范围**： 32位字符，由数字和字母组成。 **默认取值**： 不涉及。 |
   
表2Query参数 
| 参数           | 是否必选 | 参数类型   | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
|:---|:---|:---|:---|
| component_id | 否    | String | **参数解释**： 流水线微服务ID。可以通过[查询微服务列表](https://support.huaweicloud.com/api-pipeline/ListMicroservice.html)接口获取，其中data.id即为微服务ID。 **约束限制**： 不涉及。 **取值范围**： 32位字符，仅由数字和字母组成。 **默认取值**： 不涉及。 |
   
#### 请求参数
表3请求Header参数 
| 参数           | 是否必选 | 参数类型   | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
|:---|:---|:---|:---|
| X-Auth-Token | 是    | String | **参数解释**： 用户Token。通过调用[IAM服务获取用户Token接口](https://support.huaweicloud.com/api-iam/iam_30_0003.html)获取（响应消息头中X-Subject-Token的值）。 **约束限制**： 不涉及。 **取值范围**： 不涉及。 **默认取值**： 不涉及。 |
   
表4请求Body参数 
| 参数          | 是否必选 | 参数类型                                                                           | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
|:---|:---|:---|:---|
| name        | 是    | String                                                                         | **参数解释**： 流水线名称。 **约束限制**： 不涉及。 **取值范围**： 仅包含中文、大小写英文字母、数字、'-'和'_'，且长度为\[1,128\]个字符。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                      |
| description | 否    | String                                                                         | **参数解释**： 流水线描述。 **约束限制**： 不涉及。 **取值范围**： 不超过1024字符。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                      |
| is_publish  | 是    | Boolean                                                                        | **参数解释**： 是否为变更流水线。 **约束限制**： 不涉及。 **取值范围**： - true：是变更流水线。   - false：不是变更流水线。    **默认取值**： 不涉及。 |
| sources     | 是    | Array of [CodeSource] objects | **参数解释**： 流水线源列表。 **约束限制**： 不涉及。 **取值范围**： 不涉及。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                           |
| variables   | 否    | Array of [variables] objects   | **参数解释**： 流水线参数列表。 **约束限制**： 不涉及。 **取值范围**： 不涉及。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                          |
   
 表5CodeSource 
| 参数     | 是否必选 | 参数类型                                                         | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
|:---|:---|:---|:---|
| type   | 否    | String                                                       | **参数解释**： 流水线源类型。 **约束限制**： 不涉及。 **取值范围**： - code：代码仓。   - artifact：制品仓。    **默认取值**： 不涉及。 |
| params | 否    | [params] object | **参数解释**： 流水线源参数，包含流水线源的详细信息。 **约束限制**： 不涉及。 **取值范围**： 不涉及。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                         |
   
 表6params 
| 参数             | 是否必选 | 参数类型   | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
|:---|:---|:---|:---|
| git_type       | 否    | String | **参数解释**： 代码仓类型。 **约束限制**： 不涉及。 **取值范围**： - codehub。   - gitee   - github   - gitcode   - gitlab   - git。    **默认取值**： 不涉及。 |
| codehub_id     | 否    | String | **参数解释**： CodeArts Repo代码仓ID。 **约束限制**： 不涉及。 **取值范围**： 不涉及。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| endpoint_id    | 否    | String | **参数解释**： 代码源扩展点ID。 **约束限制**： 不涉及。 **取值范围**： 不涉及。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| default_branch | 否    | String | **参数解释**： 默认分支。 **约束限制**： 不涉及。 **取值范围**： 不涉及。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| git_url        | 否    | String | **参数解释**： Git仓库https地址，例如https://example.com/CloudPipelinezycs00001/2000.git。 **约束限制**： 不涉及。 **取值范围**： 不涉及。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| ssh_git_url    | 否    | String | **参数解释**： ssh_git链接地址，例如https://example.com/CloudPipelinezycs00001/2000.git。 **约束限制**： 不涉及。 **取值范围**： 不涉及。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| web_url        | 否    | String | **参数解释**： 网页url。 **约束限制**： 不涉及。 **取值范围**： 不涉及。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| repo_name      | 否    | String | **参数解释**： 流水线源名称。 **约束限制**： 不涉及。 **取值范围**： 不涉及。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| alias          | 否    | String | **参数解释**： 代码仓别名。 **约束限制**： 不涉及。 **取值范围**： 仅支持输入大小写英文字母、数字、"_"，至多128个字符。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
   
 表7variables 
| 参数           | 是否必选 | 参数类型             | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
|:---|:---|:---|:---|
| name         | 否    | AnyType          | **参数解释**： 参数名称。 **约束限制**： 不涉及。 **取值范围**： 不涉及。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| sequence     | 否    | Integer          | **参数解释**： 参数序号，从1开始递增。 **约束限制**： 不涉及。 **取值范围**： 不涉及。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| type         | 否    | String           | **参数解释**： 参数类型。 **约束限制**： 不涉及。 **取值范围**： - autoIncrement：自增长参数。   - enum：枚举参数。   - string：字符串参数。    **默认取值**： 不涉及。 |
| value        | 否    | String           | **参数解释**： 参数值。 **约束限制**： 不涉及。 **取值范围**： 不超过8192字符。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| is_secret    | 否    | Boolean          | **参数解释**： 是否私密参数。 **约束限制**： 不涉及。 **取值范围**： - true：是私密参数。   - false：不是私密参数。    **默认取值**： false。                                                                                                                                                                                 |
| description  | 否    | String           | **参数解释**： 参数描述。 **约束限制**： 不涉及。 **取值范围**： 不超过512字符。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| is_runtime   | 否    | Boolean          | **参数解释**： 是否运行时设置参数。 **约束限制**： 不涉及。 **取值范围**： - true：是运行时设置参数。   - false：不是运行时设置参数。    **默认取值**： false。                                                                                                                                                                        |
| is_reset     | 否    | Boolean          | **参数解释**： 是否重置。自增长参数被编辑，则使用编辑后的值，否则进行末位数字递增。 **约束限制**： 不涉及。 **取值范围**： - true：使用编辑后的参数值。   - false：使用自增长参数。    **默认取值**： false                                                                                                                                                  |
| latest_value | 否    | String           | **参数解释**： 最近一次运行的参数值。 **约束限制**： 不涉及。 **取值范围**： 不超过8192字符。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| limits       | 否    | Array of strings | **参数解释**： 枚举值列表。 **约束限制**： 不涉及。 **取值范围**： 每个枚举值不超过1024字符。 **默认取值**： 不涉及。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
   
#### 响应参数
**状态码：200**
表8响应Body参数 
| 参数          | 参数类型   | 描述                                                                                                                                                                                                                                                                                                   |
|:---|:---|:---|
| pipeline_id | String | **参数解释**： 流水线ID，可以通过[查询流水线列表](https://support.huaweicloud.com/api-pipeline/ListPipelines.html)接口，其中pipelines.pipelineId即为流水线ID。 **取值范围**： 32位字符，仅由数字和字母组成。 |
   
#### 请求示例
基于模板创建流水线。
指定创建的流水线名称为"新建流水线-2023041516464511"，代码仓为"test001.git"，默认分支为"master"。
```
POST https://{endpoint}/v5/54f90b75fc1447b1b65e8d3a9f77923d/api/pipeline-templates/434d660a96de423a935a0571c2223bca/create-pipeline
{
  "name" : "新建流水线-2023041516464511",
  "description" : "",
  "is_publish" : false,
  "sources" : [ {
    "type" : "code",
    "params" : {
      "git_type" : "codehub",
      "codehub_id" : "2111717853",
      "endpoint_id" : "",
      "default_branch" : "master",
      "git_url" : "https://example.com/cjrTest00002/test001.git",
      "ssh_git_url" : "git@example.com:cjrTest00002/test001.git",
      "web_url" : "",
      "repo_name" : "test001",
      "alias" : ""
    }
  } ]
}
```
#### 响应示例
**状态码：200**
OK
```
{
  "pipeline_id" : "ad6b7f66283a45be9c1f82b06c831e59"
}
```
#### SDK代码示例
SDK代码示例如下。
- [Java]
  基于模板创建流水线。
  指定创建的流水线名称为"新建流水线-2023041516464511"，代码仓为"test001.git"，默认分支为"master"。
  ```
  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;
  public class CreatePipelineByTemplateIdSolution {
      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();
          CreatePipelineByTemplateIdRequest request = new CreatePipelineByTemplateIdRequest();
          request.withTemplateId("{template_id}");
          request.withProjectId("{project_id}");
          PipelineByTemplateDTO body = new PipelineByTemplateDTO();
          CodeSourceParams paramsSources = new CodeSourceParams();
          paramsSources.withGitType("codehub")
              .withCodehubId("2111717853")
              .withEndpointId("")
              .withDefaultBranch("master")
              .withGitUrl("https://example.com/cjrTest00002/test001.git")
              .withSshGitUrl("git@example.com:cjrTest00002/test001.git")
              .withWebUrl("")
              .withRepoName("test001")
              .withAlias("");
          List<CodeSource> listbodySources = new ArrayList<>();
          listbodySources.add(
              new CodeSource()
                  .withType("code")
                  .withParams(paramsSources)
          );
          body.withSources(listbodySources);
          body.withIsPublish(false);
          body.withDescription("");
          body.withName("新建流水线-2023041516464511");
          request.withBody(body);
          try {
              CreatePipelineByTemplateIdResponse response = client.createPipelineByTemplateId(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());
          }
      }
  }
  ```
- [Python]
  基于模板创建流水线。
  指定创建的流水线名称为"新建流水线-2023041516464511"，代码仓为"test001.git"，默认分支为"master"。
  ```
  # 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 = CreatePipelineByTemplateIdRequest()
          request.template_id = "{template_id}"
          request.project_id = "{project_id}"
          paramsSources = CodeSourceParams(
              git_type="codehub",
              codehub_id="2111717853",
              endpoint_id="",
              default_branch="master",
              git_url="https://example.com/cjrTest00002/test001.git",
              ssh_git_url="git@example.com:cjrTest00002/test001.git",
              web_url="",
              repo_name="test001",
              alias=""
          )
          listSourcesbody = [
              CodeSource(
                  type="code",
                  params=paramsSources
              )
          ]
          request.body = PipelineByTemplateDTO(
              sources=listSourcesbody,
              is_publish=False,
              description="",
              name="新建流水线-2023041516464511"
          )
          response = client.create_pipeline_by_template_id(request)
          print(response)
      except exceptions.ClientRequestException as e:
          print(e.status_code)
          print(e.request_id)
          print(e.error_code)
          print(e.error_msg)
  ```
- [Go]
  基于模板创建流水线。
  指定创建的流水线名称为"新建流水线-2023041516464511"，代码仓为"test001.git"，默认分支为"master"。
  ```
  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, err := basic.NewCredentialsBuilder().
          WithAk(ak).
          WithSk(sk).
          SafeBuild()
      if err != nil {
          fmt.Println(err)
          return
      }
      hcClient, err := codeartspipeline.CodeArtsPipelineClientBuilder().
           WithRegion(region.ValueOf("<YOUR REGION>")).
           WithCredential(auth).
           SafeBuild()
      if err != nil {
          fmt.Println(err)
          return
      }
      client := codeartspipeline.NewCodeArtsPipelineClient(hcClient)
      request := &model.CreatePipelineByTemplateIdRequest{}
  request.TemplateId = "{template_id}"
  request.ProjectId = "{project_id}"
  gitTypeParams:= "codehub"
  codehubIdParams:= "2111717853"
  endpointIdParams:= ""
  defaultBranchParams:= "master"
  gitUrlParams:= "https://example.com/cjrTest00002/test001.git"
  sshGitUrlParams:= "git@example.com:cjrTest00002/test001.git"
  webUrlParams:= ""
  repoNameParams:= "test001"
  aliasParams:= ""
  paramsSources := &model.CodeSourceParams{
  GitType: &gitTypeParams,
  CodehubId: &codehubIdParams,
  EndpointId: &endpointIdParams,
  DefaultBranch: &defaultBranchParams,
  GitUrl: &gitUrlParams,
  SshGitUrl: &sshGitUrlParams,
  WebUrl: &webUrlParams,
  RepoName: &repoNameParams,
  Alias: &aliasParams,
  }
  typeSources:= "code"
  var listSourcesbody = []model.CodeSource{
          {
              Type: &typeSources,
              Params: paramsSources,
          },
      }
  descriptionPipelineByTemplateDto:= ""
  request.Body = &model.PipelineByTemplateDto{
  Sources: listSourcesbody,
  IsPublish: false,
  Description: &descriptionPipelineByTemplateDto,
  Name: "新建流水线-2023041516464511",
  }
  response, err := client.CreatePipelineByTemplateId(request)
  if err == nil {
          fmt.Printf("%+v\n", response)
      } else {
          fmt.Println(err)
      }
  }
  ```
- [更多]
  更多编程语言的SDK代码示例，请参见[API Explorer](https://console.huaweicloud.com/apiexplorer/#/openapi/CodeArtsPipeline/sdk?api=CreatePipelineByTemplateId)的代码示例页签，可生成自动对应的SDK代码示例。
#### 状态码
| 状态码 | 描述           |
|:---|:---|
| 200 | OK           |
| 401 | Unauthorized |
| 403 | Forbidden    |
| 404 | Not Found    |
   
#### 错误码
请参见[错误码](https://support.huaweicloud.com/api-pipeline/ErrorCode.html)。
