def PIPELINE_ID = "${env.BUILD_NUMBER}"

def getImageTag() {
    def dateFormat = new java.text.SimpleDateFormat('yyyyMMddHHmmss')
    def currentDate = new Date()
    return dateFormat.format(currentDate)
}

podTemplate(
    label: "${PIPELINE_ID}",
    serviceAccount: 'jenkins',
    containers: [
        containerTemplate(name: 'node', image: 'node:20-slim', ttyEnabled: true, command: 'cat'),
        containerTemplate(name: 'podman', image: "mgoltzsche/podman", ttyEnabled: true, command: 'cat', privileged: true),
        containerTemplate(name: 'git', image: 'alpine/git:latest', command: 'cat', ttyEnabled: true),
        containerTemplate(name: 'sonar-scanner', image: 'sonarsource/sonar-scanner-cli:latest', command: 'cat', ttyEnabled: true)
    ],
    volumes: [
        emptyDirVolume(mountPath: '/opt/sonar-scanner/.sonar/cache', memory: false)
    ]
) {
    node(PIPELINE_ID) {
        def props
        def imageTag = getImageTag()
        def sonarScannerHome = '/opt/sonar-scanner'

        stage("Get Source") {
            checkout scm
            props = readProperties file: "deployment/deploy_env_vars"
        }

        stage('Code Analysis & Quality Gate') {
            container('node') {
                sh "npm install"
                sh "npm test -- --coverage --passWithNoTests"
            }
            
            container('sonar-scanner') {
                withSonarQubeEnv('SonarQube') {
                    sh """
                        ${sonarScannerHome}/bin/sonar-scanner \
                        -Dsonar.projectKey=lifesub-web-dg0400 \
                        -Dsonar.sources=src \
                        -Dsonar.tests=src \
                        -Dsonar.test.inclusions=src/**/*.test.js,src/**/*.test.jsx \
                        -Dsonar.javascript.lcov.reportPaths=coverage/lcov.info
                    """
                }
            }
            
            timeout(time: 10, unit: 'MINUTES') {
                def qg = waitForQualityGate()
                if (qg.status != 'OK') {
                    error "Pipeline aborted due to quality gate failure: ${qg.status}"
                }
            }
        }

        stage('Build & Push Image') {
            container('podman') {
                withCredentials([usernamePassword(
                    credentialsId: 'acr-credentials',
                    usernameVariable: 'USERNAME',
                    passwordVariable: 'PASSWORD'
                )]) {
                    def imagePath = "${props.registry}/${props.image_org}/lifesub-web:${imageTag}"
                    
                    sh """
                        podman login ${props.registry} --username \$USERNAME --password \$PASSWORD

                        podman build \
                            --build-arg PROJECT_FOLDER="." \
                            --build-arg BUILD_FOLDER="deployment/container" \
                            --build-arg EXPORT_PORT="${props.export_port}" \
                            -f deployment/container/Dockerfile-lifesub-web \
                            -t ${imagePath} .
                        
                        podman push ${imagePath}
                    """
                }
            }
        }

        stage('Update Manifest Repository') {
            container('git') {
                withCredentials([usernamePassword(
                    credentialsId: 'github-credentials-dg0400',
                    usernameVariable: 'GIT_USERNAME',
                    passwordVariable: 'GIT_PASSWORD'
                )]) {
                    sh """
                        # Git 설정
                        git config --global user.name "Jenkins"
                        git config --global user.email "jenkins@example.com"
                        
                        # Manifest Repository Clone
                        git clone https://\$GIT_USERNAME:\$GIT_PASSWORD@github.com/cna-bootcamp/lifesub-manifest.git
                        cd lifesub-manifest
                        
                        # Frontend 이미지 태그 업데이트
                        echo "Updating lifesub-web deployment with image tag: ${imageTag}"
                        
                        if [ -f "lifesub-web/frontend-deployment.yaml" ]; then
                            # 기존 이미지 태그를 새 태그로 교체
                            sed -i "s|image: ${props.registry}/${props.image_org}/lifesub-web:.*|image: ${props.registry}/${props.image_org}/lifesub-web:${imageTag}|g" lifesub-web/frontend-deployment.yaml
                            
                            echo "Updated frontend deployment file:"
                            cat lifesub-web/frontend-deployment.yaml | grep "image:"
                        else
                            echo "Warning: Frontend deployment file not found"
                        fi
                        
                        # 변경사항 커밋 및 푸시
                        git add .
                        git commit -m "Update frontend service image tag to ${imageTag}" || echo "No changes to commit"
                        git push origin main
                    """
                }
            }
        }
    }
}

