Get Gradle Project Version via Shell Script
In our current project, we use a microservices architecture where backend services rely on Gradle for dependency management. When issues occur in production, we need to quickly identify which version is causing the problem. Since we don’t have application monitoring in place and our services are deployed to a Kubernetes cluster, we want a simple command to retrieve the application name, application version, and the image version in use.
Getting the Gradle Project Version
Option 1:
1 | version="$(./gradlew properties | grep '^version:' | awk '{printf $2}')" |
This option was ultimately dropped because it requires downloading gradlew, and our higher environments do not have access to the internet.
Option 2:
1 | version="$(cat "./build.gradle" | sed "s/=//g" | grep '^version' | awk '{printf $2}' | sed "s/'//g")" |
In the original version of this approach, the sed "s/=//g" part was absent. Why was it added? Because in build.gradle, the version can be set in two ways: version = 1.0.0 or version 1.0.0.
Getting the Name, Version, and Image of a Kubernetes Deployment
1 | kubectl get deploy -o custom-columns=NAMESPACE:.metadata.namespace,NAME:.metadata.name,"VERSION":".metadata.labels.app\.kubernetes\.io/version","IMAGE":".spec.template.spec.containers[0].image" --selector=app.kubernetes.io/managed-by=Helm -A |