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.
            Asked
            
        
        
            Active
            
        
            Viewed 1,692 times
        
    2 Answers
6
            You can approach the problem in two steps:
- Get the id of the most recent build with list-builds-for-project
- 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
- 
                    1In the first step, you could write `jq '.ids[0]'` (saving the call to `head`). – peak Feb 07 '20 at 22:38
- 
                    Great idea, @peak; I was also able to remove the call to `tr` by asking `jq` for raw output with `-r` – enharmonic Feb 07 '20 at 22:49
- 
                    You can also get the build ID from `start-build` using the `jq` expression `jq -r '.build.id'` – killthrush Sep 16 '20 at 10:52
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
