阿里云云效对接SDK获取流水线制品

参考文档:

API旧版 企业令牌 https://help.aliyun.com/zh/yunxiao/developer-reference/api-reference

API新版 个人令牌 https://help.aliyun.com/zh/yunxiao/developer-reference/api-reference-standard-proprietary

API 个人令牌 https://www.alibabacloud.com/help/zh/yunxiao/developer-reference/api-reference-standard-proprietary

API调试 https://api.aliyun.com/api/devops/2021-06-25/ListPipelines

API调试文档 https://api.aliyun.com/document/devops/2021-06-25/ListPipelineRuns

RAM用户权限策略添加 AliyunRDCFullAccess

v1版sdk

pom引入

复制代码
 <!--云效v1 -->
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-devops</artifactId>
      <version>1.0.7</version>
    </dependency>
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-core</artifactId>
      <version>4.6.0</version>
    </dependency>

工具类

复制代码
package com.vvvtimes;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class Yunxiaov1Api {

    private static String regionId = "cn-hangzhou";
    private static String endpoint = "devops.cn-hangzhou.aliyuncs.com";
    private static String accessKey = "aaa";
    private static String accessSecret = "bbb";
    private static String organizationId = "ccc";

    private static String pipelineId = "3646953";

    private static String pipelineRunId = "59";

    //ListOrganizations 获取组织列表
    public static String ListOrganizations() {
        IClientProfile profile = DefaultProfile.getProfile(
                regionId,
                accessKey,
                accessSecret
        );
        DefaultAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.GET);
        request.setDomain(endpoint);
        request.setVersion("2021-06-25");
        request.setUriPattern("/users/joinedOrgs");

        request.putHeadParameter("Content-Type", "application/json");


        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }


    //ListPipelines 获取流水线列表
    //https://api.aliyun.com/api/devops/2021-06-25/ListPipelines?spm=api-workbench.API%20Document.0.0.11aa51cfP4tP38&RegionId=cn-hangzhou
    public static String ListPipelines() {
        IClientProfile profile = DefaultProfile.getProfile(
                regionId,
                accessKey,
                accessSecret
        );
        DefaultAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.GET);
        request.setDomain(endpoint);
        request.setVersion("2021-06-25");

        // 使用 putPathParameter 替换路径参数
        request.putPathParameter("organizationId", organizationId);

        request.setUriPattern("/organization/[organizationId]/pipelines");

        request.putHeadParameter("Content-Type", "application/json");

        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    // ListPipelineRuns 获取流水线运行示例列表 -->API错误403
    public static String ListPipelineRuns() {
        IClientProfile profile = DefaultProfile.getProfile(
                regionId,
                accessKey,
                accessSecret
        );
        DefaultAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();

        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.GET);
        request.setDomain(endpoint);
        request.setVersion("2021-06-25");
        // 使用 putPathParameter 替换路径参数
        request.putPathParameter("organizationId", organizationId);
        request.putPathParameter("pipelineId", pipelineId);
        request.setUriPattern("/organization/[organizationId]/pipelines/[pipelineId]/pipelineRuns");
        request.putQueryParameter("maxResults", "10");
        request.putQueryParameter("nextToken", "aaaaaa");
        request.putHeadParameter("Content-Type", "application/json");

        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    //GetPipelineRun 获取流水线单个运行示例详情 -->403
    public static String GetPipelineRun() throws Exception {
        IClientProfile profile = DefaultProfile.getProfile(
                regionId,
                accessKey,
                accessSecret
        );
        DefaultAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();

        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.GET);
        request.setDomain("devops.cn-hangzhou.aliyuncs.com");
        request.setVersion("2021-06-25");
        // 使用 putPathParameter 替换路径参数
        request.putPathParameter("organizationId", organizationId);
        request.putPathParameter("pipelineId", pipelineId);
        request.setUriPattern("/organization/[organizationId]/pipelines/[pipelineId]/pipelineRuns/59");

        request.putHeadParameter("Content-Type", "application/json");


        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取流水线制品URL
    public static String GetPipelineArtifactUrl() throws Exception {
        IClientProfile profile = DefaultProfile.getProfile(
                regionId,
                accessKey,
                accessSecret
        );
        DefaultAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();

        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("devops.cn-hangzhou.aliyuncs.com");
        request.setVersion("2021-06-25");
        // 使用 putPathParameter 替换路径参数
        request.putPathParameter("organizationId", organizationId);
        request.setUriPattern("/organization/[organizationId]/pipeline/getArtifactDownloadUrl");
        request.putQueryParameter("filePath", "aone2/2435041/1748354328689/Artifacts_3646953.tgz");
        request.putQueryParameter("fileName", "Artifacts_3646953.tgz");
        request.putHeadParameter("Content-Type", "application/json");


        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }
}

示例调用

复制代码
package com.vvvtimes;

/*
/**
 * Hello world!
 *
 */
public class App {


    public static void main(String[] args) throws Exception {
        String s = Yunxiaov1Api.GetPipelineArtifactUrl();
        System.out.println(s);
    }
}

v2版sdk

pom引入

复制代码
<!--云效v2 -->
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>devops20210625</artifactId>
      <version>5.0.1</version>
    </dependency>
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>tea-openapi</artifactId>
      <version>0.3.8</version>
    </dependency>
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>tea-console</artifactId>
      <version>0.0.1</version>
    </dependency>
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>tea-util</artifactId>
      <version>0.2.23</version>
    </dependency>
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>credentials-java</artifactId>
      <version>1.0.1</version>
    </dependency>

v2工具类

复制代码
package com.vvvtimes;

import com.aliyun.devops20210625.models.*;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.Common;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.Map;

public class Yunxiaov2Api {

    private static String endpoint = "devops.cn-hangzhou.aliyuncs.com";
    private static String accessKey = "aaa";
    private static String accessSecret = "bbb";
    private static String organizationId = "ccc";

    private static String pipelineId = "3646953";

    private static String pipelineRunId = "59";

    //ListOrganizations 获取组织列表
    public static String ListOrganizations() throws Exception {
        com.aliyun.devops20210625.Client client = Yunxiaov2Api.createClient();
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();
        try {

            // 复制代码运行请自行打印 API 的返回值
            ListJoinedOrganizationsResponse resp = client.listJoinedOrganizationsWithOptions(headers, runtime);

            // 将整个响应转为 JSON 字符串
            String fullJson = Common.toJSONString(resp);
            // 提取 body 并返回
            return extractPipelineBody(fullJson);
        } catch (TeaException error) {
            System.out.println(error.getMessage());
            System.out.println(error.getData().get("Recommend"));
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            System.out.println(error.getMessage());
        }
        return null;
    }


    //ListPipelines 获取流水线列表
    //参考调用 https://api.aliyun.com/api/devops/2021-06-25/ListPipelines?spm=api-workbench.API%20Document.0.0.11aa51cfP4tP38&RegionId=cn-hangzhou
    public static String ListPipelines() throws Exception {
        com.aliyun.devops20210625.Client client = Yunxiaov2Api.createClient();
        com.aliyun.devops20210625.models.ListPipelinesRequest listPipelinesRequest = new com.aliyun.devops20210625.models.ListPipelinesRequest()
                .setNextToken("aaaaaaaaaa")
                .setMaxResults(30L);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();

        try {
            com.aliyun.devops20210625.models.ListPipelinesResponse resp = client.listPipelinesWithOptions(organizationId, listPipelinesRequest, headers, runtime);

            // 将整个响应转为 JSON 字符串
            String fullJson = Common.toJSONString(resp);

            // 提取 body 并返回
            return extractPipelineBody(fullJson);

        } catch (TeaException error) {
            System.out.println(error.getMessage());
            System.out.println(error.getData().get("Recommend"));
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            System.out.println(error.getMessage());
        }
        return null;
    }

    // ListPipelineRuns 获取流水线运行示例列表 -->API错误403
    public static String ListPipelineRuns() throws Exception {
        com.aliyun.devops20210625.Client client = Yunxiaov2Api.createClient();
        com.aliyun.devops20210625.models.ListPipelineRunsRequest listPipelineRunsRequest = new com.aliyun.devops20210625.models.ListPipelineRunsRequest()
                .setMaxResults(10L)
                .setNextToken("aaaaaa");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();
        try {
            // 复制代码运行请自行打印 API 的返回值
            ListPipelineRunsResponse resp = client.listPipelineRunsWithOptions(organizationId, pipelineId, listPipelineRunsRequest, headers, runtime);
            // 将整个响应转为 JSON 字符串
            String fullJson = Common.toJSONString(resp);
            System.out.println(fullJson);

            // 提取 body 并返回
            return extractPipelineBody(fullJson);

        } catch (TeaException error) {
            System.out.println(error.getMessage());
            System.out.println(error.getData().get("Recommend"));
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            System.out.println(error.getMessage());

        }
        return null;
    }

    //GetPipelineRun 获取流水线单个运行示例详情 -->403
    public static String GetPipelineRun() throws Exception {
        com.aliyun.devops20210625.Client client = Yunxiaov2Api.createClient();
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();
        try {
            // 复制代码运行请自行打印 API 的返回值
            GetPipelineRunResponse resp = client.getPipelineRunWithOptions(organizationId, pipelineId, pipelineRunId, headers, runtime);
            // 将整个响应转为 JSON 字符串
            String fullJson = Common.toJSONString(resp);
            System.out.println(fullJson);

            // 提取 body 并返回
            return extractPipelineBody(fullJson);

        } catch (TeaException error) {
            System.out.println(error.getMessage());
            System.out.println(error.getData().get("Recommend"));
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            System.out.println(error.getMessage());
        }
        return null;
    }


    //获取流水线制品URL
    public static String GetPipelineArtifactUrl() throws Exception {
        com.aliyun.devops20210625.Client client = Yunxiaov2Api.createClient();
        com.aliyun.devops20210625.models.GetPipelineArtifactUrlRequest getPipelineArtifactUrlRequest = new com.aliyun.devops20210625.models.GetPipelineArtifactUrlRequest()
                .setFileName("Artifacts_3646953.tgz")
                .setFilePath("aone2/2435041/1748354328689/Artifacts_3646953.tgz");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();
        try {
            // 复制代码运行请自行打印 API 的返回值
            GetPipelineArtifactUrlResponse resp = client.getPipelineArtifactUrlWithOptions(organizationId, getPipelineArtifactUrlRequest, headers, runtime);
            // 将整个响应转为 JSON 字符串
            String fullJson = Common.toJSONString(resp);
            System.out.println(fullJson);

            // 提取 body 并返回
            return extractPipelineBody(fullJson);

        } catch (TeaException error) {
            System.out.println(error.getMessage());
            System.out.println(error.getData().get("Recommend"));
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            System.out.println(error.getMessage());
        }
        return null;
    }


    /**
     * <b>description</b> :
     * <p>使用凭据初始化账号Client</p>
     *
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.devops20210625.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKey)     // 替换为你的 AccessKey ID
                .setAccessKeySecret(accessSecret); // 替换为你的 Secret

        config.endpoint = endpoint;
        return new com.aliyun.devops20210625.Client(config);
    }


    public static String extractPipelineBody(String fullResponseJson) {
        // 将完整 JSON 转为 Map 结构
        java.util.Map<String, Object> fullResponseMap = (Map<String, Object>) Common.parseJSON(fullResponseJson);

        // 提取 body 部分
        Object body = fullResponseMap.get("body");

        // 再转成 JSON 字符串返回
        return Common.toJSONString(body);
    }


}

v2调用示例

复制代码
package com.vvvtimes;

/*
/**
 * Hello world!
 *
 */
public class App {

    public static void main(String[] args) throws Exception {

        String s = Yunxiaov2Api.GetPipelineArtifactUrl();
        System.out.println(s);
    }
}

存在的问题

实际有两个API ListPipelineRuns GetPipelineRun调用返回403,直接从调试页面复制下载的代码也不行,估计是换接口了。

问题处理:通过给阿里云提工单发现,需要在云效的管理后台将这个RAM用户的角色从成员改成管理员才能调通。

相关推荐
草莓熊Lotso2 小时前
【C++ 基于微服务的即时通讯系统】etcd + brpc 从安装到服务注册发现全链路实战
网络·数据库·c++·redis·微服务·etcd
Lethehong7 小时前
Oracle 加个字段,目标库就报错?KFS 用“先拆后组“打通异构 DDL
数据库·oracle
Web极客码8 小时前
锁定真正的磁盘卡顿:我用 OpenCode 配合 eBPF 诊断 NVMe 队列深度延迟与 I/O 写放大
数据库·oracle
cspttty8 小时前
2027用户研究求职准备:应用统计学如何补齐研究设计与业务表达
大数据·数据库
roman_日积跬步-终至千里9 小时前
【数据Agent论文·查询与语义|05】Spider:为什么换一个数据库,模型就不会查了?
数据库·数据分析
猫咪很可爱10 小时前
Oracle RAC 启动失败排查:私网通信与内核参数优化实战
运维·数据库·oracle
XLYcmy10 小时前
《数字经济赋能城市低碳治理的路径研究检索报告》审阅与修改指导意见——基于完整检索报告体例的完整性诊断、学术规范核查与方法论纠偏 上
数据库·ai·chatgpt·文心一言·检索·知网·豆包
泡海椒10 小时前
JQuick-SQL 快速开始:注册内存表并执行查询
数据库·windows·sql
ss27310 小时前
PG实战笔记 | 09 PostgreSQL psql客户端基本操作
数据库·笔记·postgresql
电商API_1800790524710 小时前
反向海淘多币种结算:汇率该在哪个时点被冻结
java·开发语言·数据库·跨境电商·1688 api