文件最后提交记录最后更新时间
2 年前
2 年前
2 年前
README

1. 示例简介

华为云流水线CodeArtsPipeline服务可以帮助客户快速搭建和灵活编排从编译构建-代码检查-测试-部署的机制自动化持续交付能力,帮助研发团队有效减少手工操作和提升应用发布效率。

该示例展示如何通过 CodeArtsPipeline Python SDK 进行删除流水线操作。

2. 开发前准备

  • 注册 华为云,并完成 实名认证
  • 已获取华为云开发Python 软件开发工具包SDK,SDK版本号请参见SDK开发中心
  • 已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK)。请在华为云控制台“我的凭证 > 访问密钥”页面上创建和查看您的AK/SK。具体请参见 访问密钥
  • 已在CodeArts平台创建流水线

3. 安装SDK

使用服务端SDK前,您需要安装“huaweicloud-sdk-codeartspipeline”,具体的SDK版本号请参见 SDK开发中心

使用pip install安装华为云流水线Python SDK,执行如下命令安装相关依赖库,命令如下:

pip install huaweicloudsdkcodeartspipeline

4. 示例代码

import os

from huaweicloudsdkcodeartspipeline.v2.codeartspipeline_client import CodeArtsPipelineClient
from huaweicloudsdkcodeartspipeline.v2.model.code_source import CodeSource
from huaweicloudsdkcodeartspipeline.v2.model.code_source_params import CodeSourceParams
from huaweicloudsdkcodeartspipeline.v2.model.create_pipeline_new_request import CreatePipelineNewRequest
from huaweicloudsdkcodeartspipeline.v2.model.custom_variable import CustomVariable
from huaweicloudsdkcodeartspipeline.v2.model.pipeline_dto import PipelineDTO
from huaweicloudsdkcodeartspipeline.v2.region.codeartspipeline_region import CodeArtsPipelineRegion
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.exceptions.exceptions import ClientRequestException
from huaweicloudsdkcore.exceptions.exceptions import ServerResponseException


class CreatePipelineNewDemo:

    def __init__(self):
        pass

    @staticmethod
    def main(args):
        # 初始化必要参数及客户端
        # 认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
        # 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
        create_pipe_ak, create_pipe_sk = os.environ("HUAWEICLOUD_SDK_AK"), os.environ("HUAWEICLOUD_SDK_SK")
        # create_pipe_project_id 请使用华为云console->iam我的凭证->对应region所属项目的项目ID
        create_pipe_project_id = "<Corresponding region Iam Project Id>"
        # 创建流水线所需请求的region信息、配置认证信息、创建服务客户端
        create_pipe_region = CodeArtsPipelineRegion.value_of(region_id="cn-north-4")
        create_pipe_auth = BasicCredentials(ak=create_pipe_ak, sk=create_pipe_sk, project_id=create_pipe_project_id)
        code_arts_pipeline_client = CodeArtsPipelineClient.new_builder()\
            .with_credentials(credentials=create_pipe_auth)\
            .with_region(region=create_pipe_region).build()
        # 创建流水线
        try:
            print("start to create a pipeline")
            create_pipeline_new_request = CreatePipelineNewRequest()
            # 流水线所属的项目ID
            code_arts_project_id = "<ProjectId your pipeline belongs to>"
            create_pipeline_new_request.project_id = code_arts_project_id
            # 设置流水线基本信息
            pipeline_dto = PipelineDTO()
            pipeline_name = "<Name of the pipeline to be created>"
            description = "<Description of the pipeline to be created>"
            # 是否为发布流水线
            is_publish = False
            # 流水线版本信息,新版默认为3.0
            manifest_version = "<Pipeline Manifest Version, default to 3.0>"
            # 流水线定义信息
            definition = "<Definition of pipeline structure>"
            pipeline_dto.name = pipeline_name
            pipeline_dto.description = description
            pipeline_dto.is_publish = is_publish
            pipeline_dto.manifest_version = manifest_version
            pipeline_dto.definition = definition
            # 设置流水线代码源
            code_source_params = CodeSourceParams()
            code_source_params.git_type = "<Git type>"
            code_source_params.git_url = "<Git link>"
            code_source_params.ssh_git_url = "<Ssh git link>"
            code_source_params.web_url = "<Web page url>"
            code_source_params.repo_name = "<Pipeline source name>"
            code_source_params.default_branch = "<Default branch>"
            code_source_params.codehub_id = "<CodeHub code repository ID>"
            code_source_params.endpoint_id = "<Extension point ID>"
            code_source_params.alias = "<Code repository alias>"
            code_source = CodeSource()
            code_source.type = "<Pipeline Source Type>"
            code_source.params = code_source_params
            pipeline_dto.sources = [
                code_source
            ]
            # 设置流水线自定义参数
            custom_variable = CustomVariable()
            custom_variable.name = "<Custom parameter name>"
            # User-defined Parameter Sequence
            custom_variable.sequence = 1
            custom_variable.type = "<Custom Parameter Type>"
            custom_variable.value = "<Default values of customized parameters>"
            custom_variable.description = "<User-defined Parameter Description>"
            # Indicates whether to set during running.
            custom_variable.is_runtime = False
            # Indicates whether the parameter is private.
            custom_variable.is_secret = False
            # Reset or Not
            custom_variable.is_reset = False
            custom_variable.latest_value = "<Latest Parameter Value>"
            # Limited
            custom_variable.limits = []
            pipeline_dto.variables = [
                custom_variable
            ]
            create_pipeline_new_request.body = pipeline_dto
            response = code_arts_pipeline_client.create_pipeline_new(create_pipeline_new_request)
            print("create pipeline with projectId:%s, pipelineDto:%s, result:%s" % (code_arts_project_id, pipeline_dto,
                                                                                    response))
            print("created pipeline ID: %s" % response.pipeline_id)
        except (ClientRequestException, ServerResponseException) as e:
            print(str(e.status_code))


if __name__ == "__main__":
    CreatePipelineNewDemo().main(any)

相关参数说明如下所示:

  • create_pipe_ak:华为云账号Access Key。
  • create_pipe_sk:华为云账号Secret Access Key。
  • create_pipe_region:服务所在区域。

5. 参考

更多api及参数详细说明信息请参考如下链接流水线 CodeArtsPipeline Api详细说明文档 中的"流水线管理--新"目录下内容。

6. 修订记录

发布日期 文档版本 修订说明
2023-11-15 1.0 文档首次发布