Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 914
Tirinoarim Avatar asked Feb 09 '18 12:02

Tirinoarim


People also ask

How is CodeBuild triggered?

CodeBuild uses Amazon EventBridge rules for build triggers. You can use the EventBridge API to programmatically create build triggers for your CodeBuild projects. See Amazon EventBridge API Reference for more information. To use the Amazon Web Services Documentation, Javascript must be enabled.

Does CodePipeline use CodeBuild?

CodePipeline integrates with multiple AWS and third-party services, including GitHub, AWS CodeCommit, CodeBuild, AWS CloudFormation, Amazon S3, and many others.

What is CodeBuild build badge?

AWS CodeBuild now supports the use of build badges, which provide an embeddable, dynamically generated image (badge) that displays the status of the latest build for a project. This image is accessible through a publicly available URL generated for your CodeBuild project.

What is the difference between CodeBuild and CodePipeline?

CodePipeline - A service for fast and reliable continuous integration (CI) and continuous delivery (CD) CodeBuild - A scalable service to compile, test, and package source code.


1 Answers

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}
like image 136
Tirinoarim Avatar answered Oct 30 '22 21:10

Tirinoarim