diff --git a/src/vn/fireflylab/pipeline/BranchStrategy.groovy b/src/vn/fireflylab/pipeline/BranchStrategy.groovy new file mode 100644 index 0000000..fd37db8 --- /dev/null +++ b/src/vn/fireflylab/pipeline/BranchStrategy.groovy @@ -0,0 +1,31 @@ +package vn.fireflylab.pipeline + +class BranchStrategy implements Serializable { + + static boolean isPR(String branch) { branch ==~ /^PR-\d+$/ } + static boolean isFeature(String branch) { branch ==~ /^feature\/.+/ } + static boolean isDevelop(String branch) { branch == 'develop' } + static boolean isMain(String branch) { branch == 'main' } + static boolean isRelease(String branch) { branch ==~ /^release\/\d+\.\d+\.\d+$/ } + static boolean isHotfix(String branch) { branch ==~ /^hotfix\/.+/ } + + // Build + push on all branches except PRs + static boolean shouldBuildImage(String branch) { + return !isPR(branch) + } + + // Helm bump + git commit/push only on feature branches + static boolean shouldBumpChart(String branch) { + return isFeature(branch) + } + + static String imageTag(String branch) { + if (isRelease(branch)) return branch.replaceFirst('release/', '') + def shortId = UUID.randomUUID().toString().take(8) + if (isMain(branch)) return "main-${shortId}" + if (isDevelop(branch)) return "dev-${shortId}" + if (isHotfix(branch)) return "hotfix-${shortId}" + // feature/* and anything else: sanitize branch name + random suffix + return "${branch.replaceAll('[^a-zA-Z0-9._-]', '-')}-${shortId}" + } +}