githubbadgeaws-codepipelineaws-codebuild

AWS CodeBuild Badge update on CodePipeline trigger


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!?!?


Solution

  • 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://<BUCKET>/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://<BUCKET>/badges/${artifact}/ --recursive --acl public-read
    
    exit ${buildresult}