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

阿里云云效对接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,直接从调试页面复制下载的代码也不行,估计是换接口了,

相关文章:

  • mock库知识笔记(持续更新)
  • ⚡ Linux 系统安装与配置 Vim 编辑器(包括 Vim 插件管理器)
  • 【OSS】 前端如何直接上传到OSS 上返回https链接,如果做到OSS图片资源加密访问
  • VTK|Z轴拉伸功能的实现
  • maven项目编译时复制xml到classes目录方案
  • 2025-05-28 Python-List-二分法
  • 第一节 51单片机概述
  • 班级管理系统
  • vue 如何对 div 标签 设置assets内本地背景图片
  • 【网络安全】——Modbus协议详解:工业通信的“通用语言”
  • Spring AI 1.0 GA 深度解析:构建企业级AI应用的全栈实践指南
  • Linux 常用命令 -md5sum【计算和校验文件的MD5哈希值】
  • Xamarin入门笔记(Xamarin已经被MAUI取代)
  • 模型量化知识
  • 【数据库系列】bulk_save_objects 与 bulk_insert_mappings 对比
  • 利用openwrt路由器和随身WIFI搭建CPE
  • 使用 Unsloth 快速微调 LLMs 实用指南
  • 【机器学习基础】机器学习入门核心算法:隐马尔可夫模型 (HMM)
  • # Python 语音助手本地的ollama实现
  • Byte(字节)和 k(通常指 kilobit 或 kilobyte)是两种不同的单位,它们的区别和联系
  • 济南网站的建设/长沙百度搜索排名优化
  • 教研组网站的建设/广东疫情最新消息今天
  • 怎么做微信领券网站/优化设计三年级上册语文答案
  • 北京网站建设熊掌号/百度seo关键词排名价格
  • 免费论坛网站建设/网络营销推广方案案例
  • 某学校网站建设方案/网络推广是做什么工作