2017-06-12 106 views
1

我寫了下面的管道:是否可以創建管道模板,以便Jenkins自動生成管道?

import groovy.transform.Field 

channel = '#jenkins-dev-alerts' 
@Field def stage_name, emailadd 
def docker_creds = 'XXXXXXXXX' 
def git_creds = 'XXXXXXXXX' 

fileLoader.withGit('[email protected]:company/pipeline_utils.git', 'master', git_creds, ''){ 
    utils = fileLoader.load('functions.groovy'); 
} 

def run_in_stage(String stage_name, Closure command){ 
    utils.run_in_stage(stage_name, command, emailadd) 
} 

node('docker') { 
    timestamps { 
    run_in_stage('Checkout', { 
     checkout([$class: 'GitSCM', 
      userRemoteConfigs: [[credentialsId: git_creds, 
      extensions: [[$class: 'LocalBranch', localBranch: "**"]], 
      url: '[email protected]:fdnainc/research.git']]]) 
     currentBuild.displayName = "#${BUILD_NUMBER} | ${env.BRANCH}" 
     }) 

    run_in_stage('Build', { 
     withCredentials([ 
      [$class: 'UsernamePasswordMultiBinding', credentialsId: docker_creds, usernameVariable: 'D_USER', passwordVariable: 'D_PASS'], 
     ]){ 
      sh """ 
      docker login company-docker.jfrog.io -u \${D_USER} -p \${D_PASS} 
      export GIT_COMMIT=\$(git rev-parse --short HEAD) 
      export GIT_BRANCH=\$(git rev-parse --abbrev-ref HEAD) 
      docker-compose build 
      """ 
      } 
     }) 

    run_in_stage('Test', { 
     sh """ 
      docker-compose down 
      docker-compose up --abort-on-container-exit || true 
      """ 
      junit 'reports/**/*.xml' 
      sh 'docker volume prune -f' 
    }) 

    if (currentBuild.result == 'UNSTABLE') { 
     currentBuild.result = 'FAILURE' 
    } 
    if (currentBuild.result == null) { 
     currentBuild.result = "SUCCESS" 
    } 
    } 
} 
if (currentBuild.result == "FAILURE") { 
    utils.notifyOnFail(stage_name) 

} else if (currentBuild.result == "SUCCESS") { 
    node ('docker') { 
    run_in_stage('Deploy', { 
     sh 'docker-compose push' 
     utils.notifyOnSuccess(stage_name) 
    }) 
    } 
} 

這條管道建立一個項目。

我有大約10個項目需要使用與此管道相同的階段來構建。

我可以複製&將此管道的骨架粘貼到剩餘的項目中,但我知道必須有更好的方法來實現,因此管理起來會更容易。

任何人都知道如何做到這一點?

回答

相關問題