当前位置: 首页 > article >正文

阿里云云效对接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-hangzhoupublic 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错误403public 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 获取流水线单个运行示例详情 -->403public 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;}//获取流水线制品URLpublic 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-hangzhoupublic 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错误403public 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 获取流水线单个运行示例详情 -->403public 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;}//获取流水线制品URLpublic 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); // 替换为你的 Secretconfig.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用户的角色从成员改成管理员才能调通。

http://www.lryc.cn/news/2395138.html

相关文章:

  • Qt 相关 编译流程及交叉编译 部署所遇到的问题总结-持续更新
  • 前端面经 DNSxieyi1
  • 如何通过ES实现SQL风格的查询?
  • ​​知识图谱:重构认知的智能革命​
  • 【计算机网络】4网络层①
  • MATLAB中的table数据类型:高效数据管理的利器
  • Dropout 在大语言模型中的应用:以 GPT 和 BERT 为例
  • CentOS 7 如何安装libsndfile?
  • 基于深度学习的语音识别系统设计与实现
  • gitLab 切换中文模式
  • 133.在 Vue3 中使用 OpenLayers 实现画多边形、任意编辑、遮罩与剪切处理功能
  • 4.8.4 利用Spark SQL实现分组排行榜
  • 40. 自动化异步测试开发之编写异步业务函数、测试函数和测试类(类写法)
  • 【五子棋在线对战】一.前置知识的了解
  • 历年中国科学技术大学计算机保研上机真题
  • 内联盒模型基本概念?——前端面试中的隐形考点剖析
  • HackMyVM-Art
  • 网页前端开发(基础进阶1)
  • const ‘不可变’到底是值不变还是地址不变
  • 如何找到一条适合自己企业的发展之路?
  • Vue-数据监听
  • 当前用户的Git全局配置情况:git config --global --list
  • AI生态警报:MCP协议风险与应对指南(中)——MCP Server运行时安全​​
  • day15 leetcode-hot100-29(链表8)
  • DeepSeek 赋能文化遗产数字化修复:AI 重构千年文明密码
  • MonitorSDK_性能监控(从Web Vital性能指标、PerformanceObserver API和具体代码实现)
  • Spring Boot整合JWT实现认证与授权
  • 在 Linux 系统上连接 GitHub 的方法 (适用2025年)
  • 解决matlab两个库文件名冲突的问题
  • PHP 垃圾回收机制解析与应用案例