Skip to content

Instantly share code, notes, and snippets.

@fomuon
Created February 11, 2019 02:11
Show Gist options
  • Save fomuon/0fb79049b62a582e97c9ecb54b58a01b to your computer and use it in GitHub Desktop.
Save fomuon/0fb79049b62a582e97c9ecb54b58a01b to your computer and use it in GitHub Desktop.
Jenkins Pipeline

Jenkins Pipeline

Jenkins Pipeline 이란 Jenkins 2 부터 정식으로 도입 되었으며, continuous delivery 흐름을 스크립트로 정의하고 실행 할 수 있도록 하는 기능 입니다. 스크립트는 groovy DSL로 작성되며 이는 Jenkinsfile 이라 부르는 텍스트파일로 작성될 수 있고 이 파일은 source repository에 포함되어 Jenkins에 의해 실행 될 수 있습니다.
continuous delivery 흐름을 스크립트 파일로 정의한다는 것은 DevOps 관점에서 큰 의미가 있습니다. DevOps 에서 가장 중요하게 다루는 가치 중 하나가 인프라를 코드로서 정의하고 이를 통해 고도로 자동화된 운영환경을 구현하는 것에 있는데 Jenkins Pipeline 이 그러한 가치의 구현체 중 하나인 것 입니다.

1. 특징

  • source repository에 Jenkinsfile로서 정의될 수 있기 때문에 모든 브랜치 혹은 Pull Request 별로 생성 될 수 있다.
  • continuous delivery 과정 코드리뷰 가능
  • 변경사항 추적 가능

2. Jenkins Pipeline 의 두가지 타입 (Declarative Pipeline & Scripted Pipeline)

2.1 Declarative Pipeline

  • Jenkins Pipeline의 개선된 버전
  • groovy script 를 섞지 않은 groovy DSL로 정의된 rule 로만 정의한다.
  • 좀더 심플하고 세련된 방식 (Scripted Pipeline 의 복잡한 단점 보완)
  • 그러나 자유도가 떨어짐
// Jenkinsfile (Declarative Pipeline) 예제
//Build agent 로 docker를 띄워 그 내부에서 Build, Archive 처리 후 stages의 성공실패 여부에 따라 post 작업을 처리함

pipeline {
  agent label:'has-docker', dockerfile: true
  environment {
    GIT_COMMITTER_NAME = "jenkins"
    GIT_COMMITTER_EMAIL = "jenkins@jenkins.io"
  }
  stages {
    stage("Build") {
      steps {
        sh 'mvn clean install -Dmaven.test.failure.ignore=true'
      }
    }
    stage("Archive"){
      steps {
        archive "*/target/**/*"
        junit '*/target/surefire-reports/*.xml'
      }
    }
  }
  post {
    always {
      deleteDir()
    }
    success {
      mail to:"me@example.com", subject:"SUCCESS: ${currentBuild.fullDisplayName}", body: "Yay, we passed."
    }
    failure {
      mail to:"me@example.com", subject:"FAILURE: ${currentBuild.fullDisplayName}", body: "Boo, we failed."
    }
  }
}

2.2 Scripted Pipeline

  • Jenkins Pipeline의 초기버전
  • groovy DSL로 정의된 rule 과 groovy script를 자유롭게 사용할 수 있다.
  • 자유도가 높지만 코드가 복잡한 단점.
//Jenkinsfile (Scripted Pipeline) 예제
//위와 같은일을 하는 Scripted Pipeline

withEnv(["GIT_COMMITTER_NAME = jenkins","GIT_COMMITTER_EMAIL = jenkins@jenkins.io"]) {
  node('has-docker') {
    try {
      checkout scm // checks out Dockerfile and source code
      def myImage = docker.build 'my-environment:snapshot'
      myImage.inside {
        stage('Build') {
          sh 'mvn clean install -Dmaven.test.failure.ignore=true'
        }
        stage('Archive') {
          archive "*/target/**/*"
          junit '*/target/surefire-reports/*.xml'
        }
      }
      if (currentBuild.result == null || currentBuild.result == 'SUCCESS') {
        mail to:"me@example.com", subject:"SUCCESS: ${currentBuild.fullDisplayName}", body: "Yay, we passed."
      }
    }
    catch (exc) {
      mail to:"me@example.com", subject:"FAILURE: ${currentBuild.fullDisplayName}", body: "Boo, we failed."
    }
    finally {
      deleteDir()
    }
  }
}

참고

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment