젠킨스 파이프라인 아이템 만들기


파이프라인

여러 빌드 에이전트에 걸쳐있을 수 있는 장기 실행 활동을 운영합니다.
파이프 라인을 구축하거나 자유형 작업 유형에 쉽게 맞지 않는 복잡한 활동을 구성하는데 적합합니다.

파이프라인은 구르비 코드를 기반으로 작성되며 선언 파이프라인과, 스크립트 파이프라인이 있습니다.

파이프라인으로 Item 만들기

  1. 파이프라인 선택하기

  2. 권한 추가하기

  3. 로그관리 설정하기

  4. 매개변수 설정하기

  5. 파이프라인 작성하기

파이프라인 상세내용

env.BRANCH
env.IPS

pipeline {
	agent any
    options {
        timeout(time: 30, unit: 'MINUTES')
        //ansiColor('xterm')
    }
	stages{
        stage ('[SET]') {
        	stages{
        		stage ('[SET] Properties') {
        		    steps {
        		        setProperties()
        		    }
        		}
        	}
    	}
    	stage ('[DEV]') {
    		when { expression { "${params.environment}" == "dev" } }
        	stages{
        		stage ('[DEV] Checkout') {
        		    steps {
        		    	checkoutBranch()
        		    }
        		}
        		stage ('[DEV] Build') {
        		    steps {
        		        preBuild()
        				mvnBuild()
        				postBuild()
        			}
        		}
        		stage ('[DEV] Deploy') {
        			steps {
        				preDeploy()
        				deploy()
        				postDeploy()
        			}
        		}
        	}
    	}
        stage ('[PROD-KR]') {
    		when { expression { "${params.environment}" == "kr" } }
        	stages{
        		stage ('[PROD-KR] Checkout') {
        		    steps {
        		    	checkoutBranch()
        		    }
        		}
        		stage ('[PROD-KR] Build') {
        			steps {
        				preBuild()
        				mvnBuild()
        				postBuild()
        			}
        		}
        		stage ('[PROD-KR] Deploy') {
        			steps {
        				preDeploy()
        				deploy()
        				postDeploy()
        			}
        		}
        	}
    	}
    	stage ('[PROD-EU]') {
    		when { expression { "${params.environment}" == "eu" } }
        	stages{
        		stage ('[PROD-EU] Checkout') {
        		    steps {
        		    	checkoutBranch()
        		    }
        		}
        		stage ('[PROD-EU] Build') {
        			steps {
        				preBuild()
        				mvnBuild()
        				postBuild()
        			}
        		}
        		stage ('[PROD-EU] Deploy') {
        			steps {
        			    preDeploy()
        				deploy()
        				postDeploy()
        			}
        		}
        	}
    	}

	}
}

// 초기 셋팅
void setProperties(){    
    def infos

    if ( "${params.environment}" == "kr"){
		infos = "${params.kr_info}".tokenize(',')
    } else if ( "${params.environment}" == "eu") {
		infos = "${params.eu_info}".tokenize(',')
    } else {
		infos = "${params.dev_info}".tokenize(',')
    }

    env.BRANCH = "${infos[0]}"
	env.IPS = "${infos[1]}"    
}

// 소스 체크아웃
void checkoutBranch(){
	sh "git config --global credential.helper gcloud.sh"
	
	checkout([
		$class: 'GitSCM',
		branches: [[name: "*/${BRANCH}"]],
		doGenerateSubmoduleConfigurations: false,
        extensions: [[$class: "PruneStaleBranch"]],
       submoduleCfg: [],
        userRemoteConfigs: [[ url: "${params.source_repository}" ]]
    ])
}

// pre-build
void preBuild(){
	sh ''' 
       gsutil rsync -r gs://trk-maven-repository/com/pantos/ /engn001/jenkins/.m2/repository/com/pantos/
       gsutil rsync -r gs://trk-maven-repository/com/lgcns/ /engn001/jenkins/.m2/repository/com/lgcns/
       gsutil rsync -r gs://trk-maven-repository/devonframe/ /engn001/jenkins/.m2/repository/devonframe/
       gsutil rsync -r gs://trk-maven-repository/org/apache/commons/commons-discovery/ /engn001/jenkins/.m2/repository/org/apache/commons/commons-discovery/
       gsutil rsync -r gs://trk-maven-repository/com/sap/ /engn001/jenkins/.m2/repository/com/sap/
       gsutil rsync -r gs://trk-maven-repository/markany/ /engn001/jenkins/.m2/repository/markany/
	'''
}
// 빌드
void mvnBuild(){
	// build
	sh "/usr/local/src/apache-maven/bin/mvn -f pomw.xml clean package"
}

// post-build
void postBuild(){
	echo "nothing to do in the post build phase"
}

// pre-deploy
void preDeploy(){
    sh "gsutil cp -r gs://trk-maven-repository/com/pantos/vms-cmm/1.0.0/vms-cmm-1.0.0.jar ."
}


// 배포
void deploy(){
    // IP 별로 접속
    for(remoteIp in ips.tokenize('|')) {
		def remote = [:]
        remote.name = "gce"
        remote.host = "${remoteIp}"
        remote.port = "${params.ssh_port}" as int
        remote.user = "${params.ssh_user}"
        remote.allowAnyHosts = true

		withCredentials([sshUserPrivateKey(credentialsId: 'ssh_jenkins', keyFileVariable: 'KEY')]) {
		    remote.identityFile = KEY
		    
		    // artifact 복사
            sshCommand remote: remote, command: "mkdir -p ${params.remote_path}"
            sshPut remote: remote, from: "target/${params.war_file}", into: "${params.remote_path}/${params.war_file}"
            sshCommand remote: remote, command: "cd ${params.remote_path} && jar xvf ${params.war_file} > /dev/null"
			sshCommand remote: remote, command: "rm ${params.remote_path}/${params.war_file}"
			
            // copy
            sshPut remote: remote, from: "build/${params.deploy_script_file}", into: "${params.remote_path}/${params.deploy_script_file}"
			// 차량관제 전용
			sshPut remote: remote, from: "vms-cmm-1.0.0.jar", into: "/sorc001/tomcat/was_new/WEB-INF/lib/vms-cmm-1.0.0.jar"

			// 권한 추가
			sshCommand remote: remote, command: "chmod 775 ${params.remote_path}/${params.deploy_script_file}"

			// 배포 스크립트 실행
			sshCommand remote: remote, command: "${params.remote_path}/${params.deploy_script_file}"
			
			/*
            sshCommand remote: remote, command: 'mkdir -p /sorc001/tomcat/was_new'
            sshPut remote: remote, from: 'target/vms.war', into: '/sorc001/tomcat/was_new/vms.war'
            sshCommand remote: remote, command: 'cd /sorc001/tomcat/was_new && jar xvf vms.war'
			sshCommand remote: remote, command: 'rm /sorc001/tomcat/was_new/vms.war'
            // copy
            sshPut remote: remote, from: 'build/deploy-was.sh', into: '/sorc001/tomcat/was_new/deploy-was.sh'
			sshPut remote: remote, from: 'vms-cmm-1.0.0.jar', into: '/sorc001/tomcat/was_new/WEB-INF/lib/vms-cmm-1.0.0.jar'

			// 권한 추가
			sshCommand remote: remote, command: 'chmod 775 /sorc001/tomcat/was_new/deploy-was.sh'

			// 배포 스크립트 실행
			sshCommand remote: remote, command: '/sorc001/tomcat/was_new/deploy-was.sh'
			*/
		}
    }
}


// post-deploy
void postDeploy(){
    sh "rm vms-cmm-1.0.0.jar"
}

Reference

젠킨스 파이프라인 참고문서 : https://www.jenkins.io/doc/book/pipeline/syntax/

댓글남기기