@@ -34,10 +34,20 @@ shadowJar {
3434
3535def dockerTags (Project project ) {
3636 String currentBranch = " ${ 'git branch --show-current'.execute().text.trim()} "
37- def result = [
38- java.time.LocalDateTime . now(). format(java.time.format.DateTimeFormatter . ofPattern(' yyyyMMddHHmmss' )) as String ,
39- " ${ currentBranch.replaceAll('/', '-')} " as String ,
40- ]
37+ // Determine a safe tag: prefer branch name, fallback to Git SHA (from env or git)
38+ String githubSha = System . getenv(' GITHUB_SHA' ) ?: " ${ 'git rev-parse --short=12 HEAD'.execute().text.trim()} "
39+ String branchOrShaTag = currentBranch ? currentBranch. replaceAll(' /' , ' -' ) : (githubSha ? " sha-${ githubSha} " : null )
40+
41+ def result = [] as List<String >
42+ // Always include a timestamp tag for traceability
43+ result + = java.time.LocalDateTime . now(). format(java.time.format.DateTimeFormatter . ofPattern(' yyyyMMddHHmmss' )) as String
44+ // Include branch or SHA-derived tag if available
45+ if (branchOrShaTag) {
46+ result + = branchOrShaTag as String
47+ } else {
48+ logger. quiet(" No branch name or Git SHA could be determined; only timestamp tag will be used" )
49+ }
50+
4151 if (currentBranch == ' main' ) {
4252 // On the main branch we should have the latest major prepended with 'v' for GH actions
4353 result + = [' v' + project. version. substring(0 , 1 ), ' latest' ]
@@ -49,7 +59,8 @@ def dockerTags(Project project) {
4959 result + = additionalTags. split(' ,' ). collect { it. trim() }
5060 }
5161
52- return result
62+ // Avoid duplicate tags (e.g., if additionalTags repeats a SHA)
63+ return result. findAll { it && it. trim() }. unique()
5364}
5465
5566docker {
@@ -62,7 +73,9 @@ docker {
6273}
6374
6475tasks. register(' dockerBuildLocal' , com.fussionlabs.gradle.docker.tasks.DockerBuildx ) {
65- def tag = " ${ 'git branch --show-current'.execute().text.trim().replaceAll('/', '-')} "
76+ def branch = " ${ 'git branch --show-current'.execute().text.trim()} "
77+ def sha = System . getenv(' GITHUB_SHA' ) ?: " ${ 'git rev-parse --short=12 HEAD'.execute().text.trim()} "
78+ def tag = branch ? branch. replaceAll(' /' , ' -' ) : (sha ?: ' timestamp-only' )
6679 logger. quiet(" Using tag '${ tag} ' for dockerBuildLocal" )
6780 loadImage = true
6881 pushImage = false
0 commit comments