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