CodeBuild examples using SDK for C++ - Amazon SDK for C++
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

CodeBuild examples using SDK for C++

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for C++ with CodeBuild.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use ListBuilds.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! 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()) { 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::cout << "Error getting builds" << getBuildsOutcome.GetError().GetMessage() << std::endl; return false; } nextToken = listBuildsOutcome.GetResult().GetNextToken(); } else { std::cerr << "Error listing builds" << listBuildsOutcome.GetError().GetMessage() << std::endl; return false; } } while (!nextToken.empty()); return true; }
  • For API details, see ListBuilds in Amazon SDK for C++ API Reference.

The following code example shows how to use ListProjects.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! 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; }
  • For API details, see ListProjects in Amazon SDK for C++ API Reference.

The following code example shows how to use StartBuild.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! 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(); }
  • For API details, see StartBuild in Amazon SDK for C++ API Reference.