pipeline { agent any parameters { string(name: 'BRANCH_NAME', defaultValue: '', description: '빌드할 브랜치명(비우면 main)') string(name: 'GIT_COMMIT_HASH', defaultValue: '', description: '빌드할 커밋 해시(비우면 최신 커밋 기준)') } stages { stage('Checkout') { steps { script { sh 'git fetch --all' def checkoutBranch = params.BRANCH_NAME?.trim() ? params.BRANCH_NAME.trim() : 'main' def commitHash = params.GIT_COMMIT_HASH?.trim() if (commitHash) { echo "브랜치(${checkoutBranch})의 커밋 해시(${commitHash})로 체크아웃합니다." sh "git fetch origin ${checkoutBranch}" sh "git checkout ${checkoutBranch}" sh "git checkout ${commitHash}" } else { echo "브랜치(${checkoutBranch})의 최신 커밋으로 체크아웃합니다." sh "git fetch origin ${checkoutBranch}" sh "git checkout ${checkoutBranch}" commitHash = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim() } env.BRANCH_NAME = checkoutBranch env.GIT_COMMIT_HASH = commitHash } } } stage('Build & Deploy') { steps { script { def composeProject = "{{ node_id }}-" + (env.BRANCH_NAME ?: "") + "{{ node_type }}" writeFile file: '.env', text: """ BRANCH_NAME=${env.BRANCH_NAME} GIT_COMMIT_HASH=${env.GIT_COMMIT_HASH} """ sh "cat .env" sh "cat docker-compose.yml" sh "docker compose -p ${composeProject} --env-file .env down --rmi all" sh "docker compose -p ${composeProject} build --no-cache" sh "docker compose -p ${composeProject} --env-file .env up --force-recreate --remove-orphans -d" } } } } post { always { cleanWs() } } }