Dev.끄적끈적

Jenkins pipeline Script [프로젝트명: Wepeech] 본문

Own Projects

Jenkins pipeline Script [프로젝트명: Wepeech]

YeouidoSexyDeveloper 2022. 7. 2. 12:26

pipeline {
    agent any

    environment {
        imagename = "hyunjae92/wepeech"
        registryCredential = 'dockerhub-Credential'
        dockerImage = ''
    }

    stages {
        stage('Prepare') {
          steps {
            echo 'Clonning Repository'
            git url: 'git@github.com:hanghae99-6/Back-End.git',
              branch: 'main',
              credentialsId: 'github-Credential'
            }
            post {
             success { 
               echo 'Successfully Cloned Repository'
             }
             failure {
               error 'This pipeline stops here...'
             }
          }
        }

        stage('Bulid Gradle') {
          steps {
            echo 'Bulid Gradle'
            dir('.'){
               sh 'chmod +x gradlew'
               sh './gradlew clean build'
            }
          }
          post {
            failure {
              error 'This pipeline stops here...'
            }
          }
        }
        
        stage('Bulid Docker') {
          steps {
            echo 'Bulid Docker'
            script {
                dockerImage = docker.build imagename
            }
          }
          post {
            failure {
              error 'This pipeline stops here...'
            }
          }
        }

        stage('Push Docker') {
          steps {
            echo 'Push Docker'
            script {
                docker.withRegistry( '', registryCredential) {
                    dockerImage.push("hello") 
                }
            }
          }
          post {
            failure {
              error 'This pipeline stops here...'
            }
          }
        }
        
        stage('Docker Run') {
            steps {
                echo 'Pull Docker Image & Docker Image Run'
                sshagent (credentials: ['ssh-Credential']) {
                    sh "ssh -o StrictHostKeyChecking=no ubuntu@3.35.78.146 'sudo docker pull hyunjae92/wepeech:hello'" 
                    sh "ssh -o StrictHostKeyChecking=no ubuntu@3.35.78.146 'sudo docker ps -q --filter name=jenkins | grep -q . && sudo docker rm -f jenkins'"
                    sh "ssh -o StrictHostKeyChecking=no ubuntu@3.35.78.146 'sudo docker run -d --name jenkins -p 8443:8443 --network redis-net hyunjae92/wepeech:hello'"
                }
            }
        }
    }
}

'Own Projects' 카테고리의 다른 글

[Week1] 6조 S.A. (Starting Assignment)  (0) 2022.03.07