From 0d0ccf0e6e9ca36f86a0995c73053836b5413951 Mon Sep 17 00:00:00 2001 From: duynguyen Date: Sun, 26 Apr 2026 13:56:15 +0700 Subject: [PATCH] feat: add branch strategy via BranchStrategy class Stages now gated by branch: - PR-*: test only - feature/*: test + build/push + helm bump + git push - develop/main/release/hotfix: test + build/push only --- Jenkinsfile | 70 +++++++++++++++++++++-------------------------------- 1 file changed, 28 insertions(+), 42 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 39ae709..cdc99b3 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,64 +1,50 @@ +@Library('homelab') _ + +import vn.fireflylab.pipeline.BranchStrategy + pipeline { agent { kubernetes { - yaml """ -apiVersion: v1 -kind: Pod -spec: - containers: - - name: node - image: node:18-slim - command: - - sleep - args: - - infinity - - name: docker - image: docker:dind - securityContext: - privileged: true - env: - - name: DOCKER_TLS_CERTDIR - value: "" - args: - - --insecure-registry=harbor-core.harbor.svc.cluster.local -""" + yaml homelabK8sAgent(withTools: true) } } environment { - APP_NAME = 'tictactoe' - HARBOR_REGISTRY = 'harbor-core.harbor.svc.cluster.local' - HARBOR_PROJECT = 'library' - IMAGE = "${HARBOR_REGISTRY}/${HARBOR_PROJECT}/${APP_NAME}" - DOCKER_HOST = 'tcp://localhost:2375' + DOCKER_HOST = 'tcp://localhost:2375' } stages { stage('Install & Test') { steps { container('node') { - sh 'npm install' - sh 'npm test' + runNodeTest() } } } stage('Build & Push Image') { + when { expression { BranchStrategy.shouldBuildImage(env.BRANCH_NAME) } } steps { - script { - def imageTag = "${env.BRANCH_NAME.replaceAll('/', '-')}-${UUID.randomUUID().toString().take(8)}" - container('docker') { - withCredentials([usernamePassword( - credentialsId: 'harbor-credentials', - usernameVariable: 'HARBOR_USER', - passwordVariable: 'HARBOR_PASS' - )]) { - sh """ - docker login ${HARBOR_REGISTRY} -u \${HARBOR_USER} -p \${HARBOR_PASS} - docker build -t ${IMAGE}:${imageTag} . - docker push ${IMAGE}:${imageTag} - """ - } + container('docker') { + script { + def tag = BranchStrategy.imageTag(env.BRANCH_NAME) + env.IMAGE_TAG = tag + dockerBuildPush(appName: 'tictactoe', tag: tag) + } + } + } + } + + stage('Bump Helm Chart') { + when { expression { BranchStrategy.shouldBumpChart(env.BRANCH_NAME) } } + steps { + container('tools') { + script { + bumpHelmChart(imageTag: env.IMAGE_TAG) + gitCommitPush( + files: ['manifest/helm/Chart.yaml', 'manifest/helm/values.yaml'], + message: "ci: bump tictactoe chart to ${env.IMAGE_TAG}" + ) } } }