Skip to content

Commit 23e4907

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 23e4907

2 files changed

Lines changed: 24 additions & 8 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 HEAD'.execute().text.trim()}"
39+
String branchOrShaTag = currentBranch ? currentBranch.replaceAll('/', '-') : (githubSha ? "${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 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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,17 @@ tasks.register("integrationTestDocker") {
219219
buildReportsDirectory.mkdirs()
220220
// Compute the image tag exactly as in the CLI module: use branch name tag
221221
String currentBranch = "${'git branch --show-current'.execute().text.trim()}"
222-
String branchTag = currentBranch.replaceAll('/', '-')
223-
String image = "ghcr.io/aim42/hsc:${branchTag}"
222+
String branchTag = currentBranch?.replaceAll('/', '-')
223+
String sha = System.getenv('GITHUB_SHA') ?: "${'git rev-parse HEAD'.execute().text.trim()}"
224+
String tag = branchTag ?: sha
225+
String image = "ghcr.io/aim42/hsc:${tag}"
224226

225227
// Prepare absolute paths for volume mounts
226228
String docsDir = file("${INTEGRATION_TEST_DIRECTORY_COMMON_BUILD}/docs").absolutePath
227229
String reportsDir = buildReportsDirectory.absolutePath
228230

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

0 commit comments

Comments
 (0)