2017-06-29 76 views
1

我們希望將我們的項目拆分爲更小的部分。我們目前的CI過程經歷了一個短暫的測試階段,然後運行一個部署腳本。但是,如果子項目中沒有任何變化,我們不希望爲此通過構建。如果事情發生變化,只能構建項目

沒有管道的Jenkins支持SCM配置中的排除(我們使用git),並基於此,您可以配置特定的作業運行。但是,當使用管道時,我怎麼知道,如果我應該建立這個部分?如何訪問受上次推送影響的路徑?

目前我們的腳本非常簡單,我們希望儘可能簡單。

我們玩弄scripteddeclarative的語法,但找不到一個好的解決方案。

聲明:

#!groovy​ 
pipeline { 
    agent any 
    tools { 
     nodejs '8.1' 
    } 
    stages { 
     stage('Checkout') { 
      steps { 
       checkout scm 
      } 
     } 

     # Only continue, if something has changed 

     stage('Install') { 
      steps { 
       sh 'npm install' 
      } 
     } 

     stage('Test') { 
      steps { 
       sh 'npm run test-jenkins' 
      } 
      post { 
       always { 
        junit "artifacts/test/report.xml" 
       } 
      } 
     } 
    } 
} 

腳本:

#!groovy​ 
node { 
    def nodejs = tool name: '8.1', type: 'jenkins.plugins.nodejs.tools.NodeJSInstallation' 
    env.PATH = "${nodejs}/bin:${env.PATH}" 

    stage('Checkout') { 
     checkout scm 
    } 

    # Only continue, if something has changed 


    stage('Install') { 
     sh 'npm install' 
    } 

    stage('Test') { 
     try { 
      sh 'npm run test-jenkins' 
     } finally { 
      junit "artifacts/test/report.xml" 
     } 
    } 
} 
+2

後,使標記到當前提交。在下一個版本中,比較新的提交和標記。如果只有它們不是相同的提交,則繼續(如果僅關心文件內容,則繼續)。新構建完成後,將標記從之前的提交重置爲當前提交。 – ElpieKay

+0

@ElpieKay這工作!非常感謝你。我會把它寫出來並作爲答案發布。 – Kariem

+1

沒關係。很高興它有幫助。 =) – ElpieKay

回答

2

感謝ElpieKay's fast comment我的問題,我們現在有一個優雅的解決方案:

  1. 使當前的標籤提交上成功構建
  2. 在下一個構建比較新提交和更改標記

我們正在使用multi-branch pipeline以及我們在同一個源根目錄下的多個項目的並行構建。我們通過項目(serviceX)迭代,並在相應的目錄中檢查是否有變化:

def projects = ['service1', 'service2'] 
def builders = [:] 
for (p in projects) { 
    def label = p 

    builders[label] = { 
     def tag = "${BRANCH_NAME}_last" 
     node { 
      echo "Checking for changes compared to ${tag} in directory ${label}" 
      try { 
       sh "./check-for-changes ${tag} ${label}" 
      } catch (ignored) { 
       echo "Nothing to do" 
       return 
      } 
      dir (label) { 
       stage(label + ": Install") { 
        sh "npm install" 
       } 

       stage(label + ": Test") { 
        try { 
         sh "npm run test-jenkins" 
        } finally { 
         junit 'artifacts/test/report.xml' 
        } 
       } 

       echo "Setting tag for the last build on this branch" 
       sh "git tag -f ${tag}" 
      } 
     } 
    } 
} 

parallel builders 

...和腳本來檢查的變化:構建完成

#!/bin/bash 
SHA_PREV=$1 
if [ -z ${SHA_PREV} ]; then 
    echo "Usage: `basename $0` <tag> <path>" 
    exit 1 
fi 

CHECK_PATH=$2 
if [ -z ${CHECK_PATH} ]; then 
    echo "Usage: `basename $0` <tag> <path>" 
    exit 1 
fi 

if `git rev-parse ${SHA_PREV} >/dev/null 2>&1`; then 
    echo "Found previous tag: ${SHA_PREV}" 
else 
    SHA_PREV=`git rev-list --max-parents=0 HEAD` 
    echo "Using initial commit: ${SHA_PREV}" 
fi 

changes=`git diff --name-only ${SHA_PREV} HEAD | grep ${CHECK_PATH}/` 
if [ ! -n "${changes}" ]; then 
    echo "No changes found" 
    exit 2 # no changes found 
fi 
相關問題