使用 CodeBuild for C++ 的 SDK 範例 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 CodeBuild for C++ 的 SDK 範例

下列程式碼範例示範如何使用 AWS SDK for C++ with CodeBuild 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會示範如何呼叫個別服務函數,但您可以在相關案例中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例示範如何使用 ListBuilds

C++ 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

//! List the CodeBuild builds. /*! \param sortType: 'SortOrderType' type. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::CodeBuild::listBuilds(Aws::CodeBuild::Model::SortOrderType sortType, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::CodeBuild::CodeBuildClient codeBuildClient(clientConfiguration); Aws::CodeBuild::Model::ListBuildsRequest listBuildsRequest; listBuildsRequest.SetSortOrder(sortType); Aws::String nextToken; // Used for pagination. do { if (!nextToken.empty()) { listBuildsRequest.SetNextToken(nextToken); } Aws::CodeBuild::Model::ListBuildsOutcome listBuildsOutcome = codeBuildClient.ListBuilds( listBuildsRequest); if (listBuildsOutcome.IsSuccess()) { const Aws::Vector<Aws::String> &ids = listBuildsOutcome.GetResult().GetIds(); if (!ids.empty()) { std::cout << "Information about each build:" << std::endl; Aws::CodeBuild::Model::BatchGetBuildsRequest getBuildsRequest; getBuildsRequest.SetIds(listBuildsOutcome.GetResult().GetIds()); Aws::CodeBuild::Model::BatchGetBuildsOutcome getBuildsOutcome = codeBuildClient.BatchGetBuilds( getBuildsRequest); if (getBuildsOutcome.IsSuccess()) { const Aws::Vector<Aws::CodeBuild::Model::Build> &builds = getBuildsOutcome.GetResult().GetBuilds(); std::cout << builds.size() << " build(s) found." << std::endl; for (auto val: builds) { std::cout << val.GetId() << std::endl; } } else { std::cerr << "Error getting builds" << getBuildsOutcome.GetError().GetMessage() << std::endl; return false; } } else { std::cout << "No builds found." << std::endl; } // Get the next token for pagination. nextToken = listBuildsOutcome.GetResult().GetNextToken(); } else { std::cerr << "Error listing builds" << listBuildsOutcome.GetError().GetMessage() << std::endl; return false; } } while (!nextToken. empty() ); return true; }
  • 如需 API 詳細資訊,請參閱 ListBuilds AWS SDK for C++ 參考中的 API

下列程式碼範例示範如何使用 ListProjects

C++ 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

//! List the CodeBuild projects. /*! \param sortType: 'SortOrderType' type. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::CodeBuild::listProjects(Aws::CodeBuild::Model::SortOrderType sortType, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::CodeBuild::CodeBuildClient codeBuildClient(clientConfiguration); Aws::CodeBuild::Model::ListProjectsRequest listProjectsRequest; listProjectsRequest.SetSortOrder(sortType); Aws::String nextToken; // Next token for pagination. Aws::Vector<Aws::String> allProjects; do { if (!nextToken.empty()) { listProjectsRequest.SetNextToken(nextToken); } Aws::CodeBuild::Model::ListProjectsOutcome outcome = codeBuildClient.ListProjects( listProjectsRequest); if (outcome.IsSuccess()) { const Aws::Vector<Aws::String> &projects = outcome.GetResult().GetProjects(); allProjects.insert(allProjects.end(), projects.begin(), projects.end()); nextToken = outcome.GetResult().GetNextToken(); } else { std::cerr << "Error listing projects" << outcome.GetError().GetMessage() << std::endl; } } while (!nextToken.empty()); std::cout << allProjects.size() << " project(s) found." << std::endl; for (auto project: allProjects) { std::cout << project << std::endl; } return true; }
  • 如需 API 詳細資訊,請參閱 ListProjects AWS SDK for C++ 參考中的 API

下列程式碼範例示範如何使用 StartBuild

C++ 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

//! Start an AWS CodeBuild project build. /*! \param projectName: A CodeBuild project name. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::CodeBuild::startBuild(const Aws::String &projectName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::CodeBuild::CodeBuildClient codeBuildClient(clientConfiguration); Aws::CodeBuild::Model::StartBuildRequest startBuildRequest; startBuildRequest.SetProjectName(projectName); Aws::CodeBuild::Model::StartBuildOutcome outcome = codeBuildClient.StartBuild( startBuildRequest); if (outcome.IsSuccess()) { std::cout << "Successfully started build" << std::endl; std::cout << "Build ID: " << outcome.GetResult().GetBuild().GetId() << std::endl; } else { std::cerr << "Error starting build" << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • 如需 API 詳細資訊,請參閱 StartBuild AWS SDK for C++ 參考中的 API