3

How do I check the status of my most recent AWS Codebuild build projects using the CLI? I see that you can view the build details, but it requires a specific build-id, and the summarized build information does not give any details on which build phase is correlated to the status that appears in the console.

enharmonic
  • 1,800
  • 15
  • 30

2 Answers2

6

You can approach the problem in two steps:

  1. Get the id of the most recent build with list-builds-for-project
  2. Select the relevant field from the output of batch-get-builds

Assuming you have aws CLI and jq installed, and are receiving CLI results in JSON format:

id=$(aws codebuild list-builds-for-project --project-name myproject | jq -r '.ids[0]')

The default sort order puts the most recently completed build at the top of the list. Then use $id from the prior step:

aws codebuild batch-get-builds --ids "$id" | jq '.builds[].phases[] | select (.phaseType=="BUILD") | .phaseStatus'

See select objects based on value of variable in object using jq for a discussion of the jq syntax.

enharmonic
  • 1,800
  • 15
  • 30
0

I may have reinvented or over-engineered the wheel, but wrote a little node program to do this that might be of use to you.

const { exec } = require("child_process");
const { promisify } = require("util");

const asyncExec = promisify(exec);

const getLatestBuildId = async () => {
  const listBuildsCommand = "aws codebuild list-builds";
  const { stdout } = await asyncExec(listBuildsCommand);
  const { ids } = JSON.parse(stdout);
  const [latestBuildId] = ids;
  return latestBuildId;
};

const getBuildStatus = async (latestBuildId) => {
  const batchGetBuildsCommand = `aws codebuild  batch-get-builds --ids ${latestBuildId}`;
  const { stdout } = await asyncExec(batchGetBuildsCommand);
  const { builds } = JSON.parse(stdout);
  const [latestBuild] = builds;
  const { id, currentPhase, buildStatus, startTime, endTime } = latestBuild;
  return {
    id,
    currentPhase,
    buildStatus,
    start: new Date(startTime * 1000).toLocaleTimeString(),
    end: new Date(endTime * 1000).toLocaleTimeString(),
  };
};

const reportBuildStatus = async () => {
  const latestBuildId = await getLatestBuildId();
  const latestBuild = await getBuildStatus(latestBuildId);
  console.log(latestBuild);
};

reportBuildStatus();
Josef
  • 2,869
  • 2
  • 22
  • 23