Programmer:)

[Jenkins] Pipeline Script (환경변수, input, 병렬작업) 본문

DEV/Jenkins

[Jenkins] Pipeline Script (환경변수, input, 병렬작업)

ryeggg 2022. 4. 25. 15:24
반응형

#Pipeline 으로 Job을 생성시 유니티 경로 or 저장할 폴더 경로 등 계속적으로 복붙해야하는 것들과

선택적으로 stage 실행을 하고싶었다.

 

#기본 Pipeline Script 뼈대

 

#작업할 stage 추가

 

#Pipeline Syntax 클릭 후 스크립트 생성 후 추가

(선행작업) gitlab 연동

 

[Jenkins-Pipeline] Jenkins(Mac...Windows..) - GitLab 연동

#iMac을 오늘 처음 사용해봐서 두서 없을 수 있음. # homebrew를 이용하여 jenkins를 설치하는 내용이 많아 homebrew를 설치해줬다. # homebrew 설치 후 cask도 설치. # 터미널에 brew install jenkins 입력 하면..

ryeggg.tistory.com

pipeline {
    agent any

    stages {
        stage('Pull gitlab project') {
            steps {
                git credentialsId: '******', url: 'git@git.*******.git'
                echo 'Pull gitlab project Success'
            }
        }
        
         stage('Build project') {
            steps {
                echo 'Hello World'
            }
        }
        
        stage('SSH upload') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

 

#Build Stage 스크립트 작성

(참고) 

 

[Jenkins-Pipeline] Pipeline 구성 (commandline 인자 값 받아오기)

전 게시물에서 gitlab 연동작업을 완료했다. 최신 브랜치를 받은 후 빌드까지 순차 작업 할 수 있도록 설정해보자 #Freestyle project job을 하나를 만든다. (난 windows32bit , 64bit 이렇게 빌드를 하기위해

ryeggg.tistory.com

 

pipeline {
    agent any

    stages {
        stage('Pull gitlab project') {
            steps {
               git credentialsId: '******', url: 'git@git.*******.git'
                echo 'Pull gitlab project Success'
            }
        }
        
         stage('Build project') {
            steps {
                script{
                    echo "build project 32"
                        sh (script : """
                        "/Applications/Unity/Hub/Editor/2019.4.3f1/Unity.app/Contents/MacOS/Unity" -quit -batchmode -projectPath "/Users/apple/.jenkins/workspace/hitest/Application" -executeMethod ProjectBuild.BuildStartTest -buildTarget "StandaloneWindows" -ConnectServer "dev" -Company "hitest" -at 32
                     """)
                
                }
            }
        }
        
        stage('SSH upload') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

 

#여기서 저 경로들을 좀 더 보기쉽게 환경변수로 빼기

pipeline {
    agent any

    environment{
        UNITY_PATH = "/Applications/Unity/Hub/Editor/2019.4.3f1/Unity.app/Contents/MacOS/Unity"
        PROJECT_PATH = "/Users/apple/.jenkins/workspace/hitest/Application"
        COMPANY_NAME = "hitest"
        BUILD_TARGET = "StandaloneWindows"
        CONNECTSERVER = "dev"
    }
    
    stages {
        stage('Pull gitlab project') {
            steps {
                git credentialsId: '******', url: 'git@git.*******.git'
                echo 'Pull gitlab project Success'
            }
        }
        
         stage('Build project') {
            steps {
                script{
                    echo "build project 32"
                        sh (script : """
                         ${env.UNITY_PATH} -quit -batchmode -projectPath ${env.PROJECT_PATH} -executeMethod ProjectBuild.BuildStartTest -buildTarget ${env.BUILD_TARGET} -ConnectServer ${env.CONNECTSERVER} -Company ${env.COMPANY_NAME} -at 32
                     """)
                
                }
            }
        }
        
        stage('SSH upload') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

 

#SSH upload Stage 추가

(참고)

 

[Jenkins] SSH연동 (freestyle, Pipeline)

#[Jenkins] Plugin Manager 에서 Publish Over SSH 플로그인을 설치. #[Jenkins] 관리 -> 시스템 설정 SSH Server 입력  난 서버 ID 와 Password로 연결해서 따로 key값은 입력하지 않음 # 하단 TestConfigurat..

ryeggg.tistory.com

 

pipeline {
    agent any

    environment{
        UNITY_PATH = "/Applications/Unity/Hub/Editor/2019.4.3f1/Unity.app/Contents/MacOS/Unity"
        PROJECT_PATH = "/Users/apple/.jenkins/workspace/hitest/Application"
        COMPANY_NAME = "hitest"
        BUILD_TARGET = "StandaloneWindows"
        CONNECTSERVER = "dev"
    }
    
    stages {
        stage('Pull gitlab project') {
            steps {
                git credentialsId: '******', url: 'git@git.*******.git'
                echo 'Pull gitlab project Success'
            }
        }
        
         stage('Build project') {
            steps {
                script{
                    echo "build project 32"
                        sh (script : """
                         ${env.UNITY_PATH} -quit -batchmode -projectPath ${env.PROJECT_PATH} -executeMethod ProjectBuild.BuildStartTest -buildTarget ${env.BUILD_TARGET} -ConnectServer ${env.CONNECTSERVER} -Company ${env.COMPANY_NAME} -at 32
                     """)
                
                }
            }
        }
        
        stage('SSH upload') {
            steps {
                script{
                    sshPublisher(publishers: [sshPublisherDesc(configName: 'hitest', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, 
                         makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'Windows/dev/Client', remoteDirectorySDF: false, 
                         removePrefix: 'Application/32bit', sourceFiles: 'Application/32bit/**')], 
                         usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
                     
            }
        }
    }
}

 

 

#SSH upload 환경변수에 따라 실행하도록 if문으로 빼기

pipeline {
    agent any

    environment{
        SSH_UPLOAD = "true"
        UNITY_PATH = "/Applications/Unity/Hub/Editor/2019.4.3f1/Unity.app/Contents/MacOS/Unity"
        PROJECT_PATH = "/Users/apple/.jenkins/workspace/hitest/Application"
        COMPANY_NAME = "hitest"
        BUILD_TARGET = "StandaloneWindows"
        CONNECTSERVER = "dev"
    }
    
    stages {
        stage('Pull gitlab project') {
            steps {
                git credentialsId: '******', url: 'git@git.*******.git'
                echo 'Pull gitlab project Success'
            }
        }
        
         stage('Build project') {
            steps {
                script{
                    echo "build project 32"
                        sh (script : """
                         ${env.UNITY_PATH} -quit -batchmode -projectPath ${env.PROJECT_PATH} -executeMethod ProjectBuild.BuildStartTest -buildTarget ${env.BUILD_TARGET} -ConnectServer ${env.CONNECTSERVER} -Company ${env.COMPANY_NAME} -at 32
                     """)
                
                }
            }
        }
        
        stage('SSH upload') {
            steps {
              script{
                    if("${env.SSH_UPLOAD}" == "true"){
                         sshPublisher(publishers: [sshPublisherDesc(configName: 'hitest', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, 
                         makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'Windows/dev/Client', remoteDirectorySDF: false, 
                         removePrefix: 'Application/32bit', sourceFiles: 'Application/32bit/**')], 
                         usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
                    }
                    else{
                         echo "DO NOT UPLOAD"
                    }
                     
                }
             }
         }
    }
}

 

#병렬 작업 추가 Parallel 부분

pipeline {
    agent any

    environment{
        SSH_UPLOAD = "true"
        UNITY_PATH = "/Applications/Unity/Hub/Editor/2019.4.3f1/Unity.app/Contents/MacOS/Unity"
        PROJECT_PATH = "/Users/apple/.jenkins/workspace/hitest/Application"
        COMPANY_NAME = "hitest"
        BUILD_TARGET = "StandaloneWindows"
        CONNECTSERVER = "dev"
    }
    
    stages {
        stage('Pull gitlab project') {
            steps {
                git credentialsId: '******', url: 'git@git.*******.git'
                echo 'Pull gitlab project Success'
            }
        }
        
         stage('Build project') {
            steps {
                script{
                    echo "build project 32"
                        sh (script : """
                         ${env.UNITY_PATH} -quit -batchmode -projectPath ${env.PROJECT_PATH} -executeMethod ProjectBuild.BuildStartTest -buildTarget ${env.BUILD_TARGET} -ConnectServer ${env.CONNECTSERVER} -Company ${env.COMPANY_NAME} -at 32
                     """)
                
                }
            }
        }
        
       stage('SSH') {
           
            parallel{
                stage('SSH Send 32bit') {
                    steps {
                        script{
                            if("${env.SSH_UPLOAD}" == "true"){
                            sshPublisher(publishers: [sshPublisherDesc(configName: 'hitest', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, 
                            makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'Windows/dev/Client', remoteDirectorySDF: false, 
                            removePrefix: 'Application/32bit', sourceFiles: 'Application/32bit/**')], 
                            usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
                            }
                            else{
                            echo "DO NOT UPLOAD"
                            }
                        }
                    }
                }
                
                stage('SSH Send 64bit') {
                    steps {
                        script{
                            if("${env.SSH_UPLOAD}" == "true"){
                            sshPublisher(publishers: [sshPublisherDesc(configName: 'hitest', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, 
                            makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'Windows/dev/Client', remoteDirectorySDF: false, 
                            removePrefix: 'Application/64bit', sourceFiles: 'Application/64bit/**')], 
                            usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
                            }
                            else{
                            echo "DO NOT UPLOAD"
                            }
                        }
                    }
                }
            }
            
        }
    }
}

 

#환경변수 if문 input message로 수정

SSH 병렬작업 전 작업진행 여부 message창 팝업

Yes -> 작업진행

abort -> 빌드까지만 진행 SSH 작업 중단

pipeline {
    agent any

    environment{
        SSH_UPLOAD = "true"
        UNITY_PATH = "/Applications/Unity/Hub/Editor/2019.4.3f1/Unity.app/Contents/MacOS/Unity"
        PROJECT_PATH = "/Users/apple/.jenkins/workspace/hitest/Application"
        COMPANY_NAME = "hitest"
        BUILD_TARGET = "StandaloneWindows"
        CONNECTSERVER = "dev"
    }
    
    stages {
        stage('Pull gitlab project') {
            steps {
                git credentialsId: '******', url: 'git@git.*******.git'
                echo 'Pull gitlab project Success'
            }
        }
        
         stage('Build project') {
            steps {
                script{
                    echo "build project 32"
                        sh (script : """
                         ${env.UNITY_PATH} -quit -batchmode -projectPath ${env.PROJECT_PATH} -executeMethod ProjectBuild.BuildStartTest -buildTarget ${env.BUILD_TARGET} -ConnectServer ${env.CONNECTSERVER} -Company ${env.COMPANY_NAME} -at 32
                     """)
                
                }
            }
        }
        
       stage('SSH') {
           input{
                     message "Continue SSH uploading?"
                     ok "Yes"
                     parameters{
                     string(name: 'ANSWER', defaultValue: 'Yes', description: '')
                    }
                }
           
            parallel{
                stage('SSH Send 32bit') {
                    steps {
                        script{
                            if("${ANSWER}" == "Yes"){
                            sshPublisher(publishers: [sshPublisherDesc(configName: 'hitest', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, 
                            makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'Windows/dev/Client', remoteDirectorySDF: false, 
                            removePrefix: 'Application/32bit', sourceFiles: 'Application/32bit/**')], 
                            usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
                            }
                            else{
                            echo "DO NOT UPLOAD"
                            }
                        }
                    }
                }
                
                stage('SSH Send 64bit') {
                    steps {
                        script{
                            if("${ANSWER}" == "Yes"){
                            sshPublisher(publishers: [sshPublisherDesc(configName: 'hitest', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, 
                            makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'Windows/dev/Client', remoteDirectorySDF: false, 
                            removePrefix: 'Application/64bit', sourceFiles: 'Application/64bit/**')], 
                            usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
                            }
                            else{
                            echo "DO NOT UPLOAD"
                            }
                        }
                    }
                }
            }
            
        }
    }
}
반응형
Comments