From 2e3a68421b4a908852dde7d731f9838bdb828b16 Mon Sep 17 00:00:00 2001 From: duynguyen Date: Sun, 26 Apr 2026 13:55:58 +0700 Subject: [PATCH] feat: add BranchStrategy class Mirrors company BasePipeline pattern. Static methods determine what pipeline stages run per branch: - PR-*: test only - feature/*: test + build/push + helm bump + git push - develop/main/release/hotfix: test + build/push only imageTag() returns branch-appropriate tag format: release/x.y.z -> x.y.z, others -> - --- .../fireflylab/pipeline/BranchStrategy.groovy | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/vn/fireflylab/pipeline/BranchStrategy.groovy 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}" + } +}