Help Center> CodeArts Pipeline> API Reference> API> Pipeline Management> Creating a Pipeline With a Template
Updated on 2024-06-14 GMT+08:00

Creating a Pipeline With a Template

Function

This API is used to create a pipeline using a template.

Calling Method

For details, see Calling APIs.

URI

POST /v5/{project_id}/api/pipeline-templates/{template_id}/create-pipeline

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Project ID.

template_id

Yes

String

Template ID.

Table 2 Query Parameters

Parameter

Mandatory

Type

Description

component_id

No

String

Microservice ID.

Request Parameters

Table 3 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

Yes

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 4 Request body parameters

Parameter

Mandatory

Type

Description

name

Yes

String

Pipeline name.

description

No

String

Pipeline description.

is_publish

Yes

Boolean

Whether the pipeline is a change pipeline.

sources

Yes

Array of CodeSource objects

Pipeline source.

variables

No

Array of variables objects

Pipeline parameters.

Table 5 CodeSource

Parameter

Mandatory

Type

Description

type

No

String

Pipeline source type.

params

No

params object

Pipeline source parameters.

Table 6 params

Parameter

Mandatory

Type

Description

git_type

No

String

Git type.

codehub_id

No

String

Repo repository ID.

endpoint_id

No

String

Endpoint ID.

default_branch

No

String

Default branch.

git_url

No

String

Git URL.

ssh_git_url

No

String

SSH_Git URL.

web_url

No

String

Web URL.

repo_name

No

String

Pipeline source name.

alias

No

String

Code repository alias. The alias can contain a maximum of 128 characters, including letters, digits, and underscores (_).

Table 7 variables

Parameter

Mandatory

Type

Description

name

No

String

Parameter name.

sequence

No

Integer

Parameter number.

type

No

String

Parameter type.

value

No

String

Parameter value.

is_secret

No

Boolean

Private parameter or not.

description

No

String

Description.

is_runtime

No

Boolean

Runtime setting or not.

is_reset

No

Boolean

Reset or not.

latest_value

No

String

Last used parameter value.

limits

No

Array of strings

Enumerated value.

Response Parameters

Status code: 200

Table 8 Response body parameters

Parameter

Type

Description

pipeline_id

String

Pipeline ID.

Example Requests

Create a pipeline based on a template. Set the pipeline name to New pipeline-2023041516464511, code repository to test001.git, and default branch to master.

POST https://{endpoint}/v5/54f90b75fc1447b1b65e8d3a9f77923d/api/pipeline-templates/434d660a96de423a935a0571c2223bca/create-pipeline

{
  "name" : "New Pipeline - 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" : ""
    }
  } ]
}

Example Responses

Status code: 200

OK

{
  "pipeline_id" : "ad6b7f66283a45be9c1f82b06c831e59"
}

SDK Sample Code

The SDK sample code is as follows.

Create a pipeline based on a template. Set the pipeline name to New pipeline-2023041516464511, code repository to test001.git, and default branch to master.

 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
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.withComponentId("<component_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("New Pipeline - 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());
        }
    }
}

Create a pipeline based on a template. Set the pipeline name to New pipeline-2023041516464511, code repository to test001.git, and default branch to master.

 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
# 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.component_id = "<component_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="New Pipeline - 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)

Create a pipeline based on a template. Set the pipeline name to New pipeline-2023041516464511, code repository to test001.git, and default branch to master.

 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
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.CreatePipelineByTemplateIdRequest{}
	componentIdRequest:= "<component_id>"
	request.ComponentId = &componentIdRequest
	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: "New Pipeline - 2023041516464511",
	}
	response, err := client.CreatePipelineByTemplateId(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

401

Unauthorized

403

Forbidden

404

Not Found

Error Codes

See Error Codes.