buildScanPublished callbacks occurring before build scan upload

I have a small fragment in my build.gradle that takes the build Scan url and forwards it to another server for processing

buildScan {
    buildScanPublished {
        println "Doing a preflight check"
        println "calling http://preflight.internal.com/score/${it.getBuildScanId()}"

        def base = "http://preflight.internal.com/%s/${it.getBuildScanId()}"
        def percent = new URL(String.format(base, 'd')).getText([connectTimeout: 30000, readTimeout: 30000]).replace('%', '') \
as Integer
        println "Score details: ${String.format(base, 'score')}"
        
       //more processing
     
   }

On gradle 5.6.4 this worked as expected:

Publishing build scan...
http://gradleentsea.tsi.lan/s/f2dihlcfk7ktg
Doing a preflight check
calling http://preflight.internal.com/score/f2dihlcfk7ktg
Score details: http://preflight.internal.com/score/f2dihlcfk7ktg

But on gradle 7.3.3 the callback seems to occur after the scan is created but before its uploaded

Example:

Publishing build scan...
Doing a preflight check
calling http://preflight.internal.com/score/5bmkjm7ja3qrg
WARNING: Error invoking build scan buildScanPublished action
        http://preflight.internal.com/d/5bmkjm7ja3qrg
http://gradleentsea.tsi.lan/s/5bmkjm7ja3qrg

Checking simultaneously against gradle enterprise the timing lines up. The scan isn’t uploaded until the URL message printed which is after the callback is called now. (You can see this in the order of outputs above)

Is this a bug or is there some new logic needed to execute a callback after the upload?

Hello Benjamin,

The callback is called as soon as the URL is available, this is before the the build scan data has been uploaded. This has been the case for a while (plugin 3.3+, Gradle Enterprise 2020.2+). By default, build scans are uploaded in the background after the build has finished. However, disabling background uploading will not guarantee that the callback (buildScanPublished) is executed before the build scan is available for consumption.

Since I don’t know exactly what you need to do with the URL on your preflight server it’s hard to provide guidance to make it work, however, if you want to compute a score for build scans you might change your implementation, and instead of using the buildScanPublished hook, you could leverage the Gradle Enterprise API.

What version of plugin and Gradle were you using before and what are the new versions you are using now?

Thanks for the info. That confirms the behavior I saw. We’re using the 3.5 plugin currently with gradle 7. It wouldn’t surprise me if it was earlier than 3.3 with the 5.6.4 version.

I also already fiddled with background uploads to no effect.

How would the gradle enterprise API help in this case? Does it provide more ways to plug into the life cycle? What I’ve done for now is just save the scan ID during one phase of CI and use it later on which bysteps the issue.

Thanks again for your help

Hello,

Gradle Enterprise API does not provide any way to plug into the lifecycle so you would still need to either poll the API for builds or do the same thing, save the scan ID and use it later. Since I don’t know exactly what you need to compute all I can say is that Gradle Enterprise API might provide the data you need already computed or in a more easily accessible format. Here is an example on computing the cache hit percentage and avoidance savings ratio.

If your existing usage can be replaced with the easier-to-use Gradle Enterprise API, please consider migrating. Documentation is available here and the API reference here.