Skip to content

Commit a0fec9b

Browse files
committed
#369 Use SHA for Docker tags if no branch detected
On GH pull requests the `git branch --show-current` does not return the branch name. In this case we use the SHA tag (only) for Docker tagging.
1 parent a839d97 commit a0fec9b

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

htmlSanityCheck-cli/build.gradle

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,20 @@ shadowJar {
3434

3535
def 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

5566
docker {
@@ -62,7 +73,9 @@ docker {
6273
}
6374

6475
tasks.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

integration-test/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ tasks.register("integrationTestDocker") {
227227
String reportsDir = buildReportsDirectory.absolutePath
228228

229229
String params = "--rm -v \"${docsDir}:${docsDir}\" -v \"${reportsDir}\":/reports -w \"${docsDir}\" ${image} -r /reports --exclude https://www\\.baeldung\\.com/.* --fail-on-errors"
230+
logger.quiet("Executing Docker run with '${params}'")
230231
def result = exec {
231232
if (System.getProperty("os.name") ==~ /Windows.*/) {
232233
// Use cmd to run docker on Windows

0 commit comments

Comments
 (0)