10
votes

I've created an AWS CodeBuild project that includes a build badge and when I trigger the build manually, all works OK (i.e. the badge is updated). I've now added a CodePipeline project that triggers that build based on a GitHub checkin. I can see it go through in the CodeBuild project history but the badge does NOT seem to get updated?

  1. Manual build - Build failed - Badge updated
  2. Pipeline build - Build succeeded - Badge NOT updated
  3. Manual build - Build succeeded - Badge updated

Codebuild History View

Is this intentional? Am I doing something wrong? Do I have to hack together yet ANOTHER Lambda script to do something so simple!?!?

3
Over a year later and this is still the case.Mark Maglana

3 Answers

3
votes

I've written a build script as a workaround. In the buildspec.yml, I set executable on, then run the script..

  build:
    commands:
      - echo Build started on `date`
      - chmod +x aws_scripts/build.sh
      - aws_scripts/build.sh mvn -B package

The script itself pulls details out of the master pom.xml, sets the badges to "pending", calls the build command, processes the results.

#!/bin/bash

mkdir badges

# Artifact
artifact=$( xmllint --xpath "/*[local-name() = 'project']/*[local-name() = 'artifactId']/text()" pom.xml )

# Version
version=$( xmllint --xpath "/*[local-name() = 'project']/*[local-name() = 'version']/text()" pom.xml )
version=${version/-/--} # Hyphen escaping required by shields.io

# Update badges pre-build
echo "https://img.shields.io/badge/Build-In_progress-orange.svg"
curl -s "https://img.shields.io/badge/Build-In_progress-orange.svg" > badges/build.svg

echo "https://img.shields.io/badge/Version-$version-green.svg"
curl -s "https://img.shields.io/badge/Version-$version-green.svg" > badges/version.svg

echo "https://img.shields.io/badge/Unit_Tests-Pending-orange.svg"
curl -s "https://img.shields.io/badge/Unit_Tests-Pending-orange.svg" > badges/unit-test.svg

# Sync with S3
aws s3 cp badges s3://endeavour-codebuild/badges/${artifact}/ --recursive --acl public-read

# Build
{ #try
    eval $* &&
    buildresult=0
} || { #catch
    buildresult=1
}

# Build
if [ "$buildresult" -gt "0" ] ; then
        badge_status=failing
        badge_colour=red
else
        badge_status=passing
        badge_colour=green
fi
echo "https://img.shields.io/badge/Build-$badge_status-$badge_colour.svg"
curl -s "https://img.shields.io/badge/Build-$badge_status-$badge_colour.svg" > badges/build.svg

# Unit tests
failures=$( xmllint --xpath 'string(//testsuite/@failures) + string(//testsuite/@errors)' API/target/surefire-reports/TEST-*.xml )

if [ "$failures" -gt "0" ] ; then
        badge_status=failing
        badge_colour=red
else
        badge_status=passing
        badge_colour=green
fi

echo "Generating badge 'https://img.shields.io/badge/Unit_Tests-$badge_status-$badge_colour.svg'"
curl -s "https://img.shields.io/badge/Unit_Tests-$badge_status-$badge_colour.svg" > badges/unit-test.svg

# Sync with S3
aws s3 cp badges s3://endeavour-codebuild/badges/${artifact}/ --recursive --acl public-read

exit ${buildresult}
0
votes

This feature isn't supported for CodePipeline builds, as when CodePipeline passes the source to CodeBuild it doesn't contain the .git directory.

To comment on dstrants's answer, the request URL is tied to the project, not to individual builds. The request URL does require a branch to be specified, however (the default is master)

0
votes

I've created this repo: https://github.com/unfor19/aws-build-badges

With just a few steps you can easily have build badges for both CodeBuild and CodePipeline. The badges are created by Lambda functions which run for a very short time (less than 200ms, 192MB).