Jenkinsfile中的并行foreach

时间:2017-02-24 20:54:13

标签: maven jenkins parallel-processing continuous-integration continuous-deployment

我将构建过程转换为Jenkins文件,在此过程中我想简化一些事情。我有许多共享模块和已部署的模块。部署的模块都有一个Dockerfile,如下所示:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div id="screen">$&gt;<input></input></div>
<script>
$("#screen input").focus();
$("#screen input").on('keydown', function(event) {
    if(event.which === 13) {// Enter key pressed
        var $this = $(this),
            val = $this.val();
        $this.focus().val('');
        if(val === "hello world") {
            $this.val('hi!')  // just set the val
        }
    }
});
</script>
</body>

他们都有pom.xml。我想要做的是构建所有共享组件,然后并行构建和发布共享模块。我遇到的问题是

  1. 仅获取已部署的模块
  2. 并行执行未知数量的子模块。
  3. 经过一些沮丧的删除后,我所拥有的只是

    Parent 
    |-shared1
    |-shared2
    |-Deploy1
    | \-Dockerfile
    |-shared3
    |-Deploy2
    | \-Dockerfile
    |-Jenkinsfile
    |-pom.xml
    

    ---- 更新

    不得不前进,但我取得了很大的进步。以下是我最终的结果:

    stage('Build Test') {
      dockerSupport.insideContainer('dockerRepository/docker-mvn-ci:0.1.0') {
        sh 'mvn clean install'
      }
    }
    
    stage('Publish') {
        sh "docker login -u '${env.DOCKER_USERNAME}' -p '${env.DOCKER_PASSWORD}' https://dockerrepository"
    }
    

    因此,在node { withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'creds', passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USERNAME']]) { stage('Checkout') { checkout scm gitSupport.configureEnvGitCommit() } dockerSupport.insideContainer('dockerRepository/docker-mvn-ci:0.1.0') { stage('Build Shared') { sh 'mvn clean' sh 'mvn install' } stage('Docker Login') { sh "docker login -u '${env.DOCKER_USERNAME}' -p '${env.DOCKER_PASSWORD}' https://dockerrepository" } } stage('Parallel') { def subfolders = sh(returnStdout: true, script: 'ls -1d */').replace("/", "").trim().split("\n") echo "${subfolders}" def branches = [:] for (int i = 0; i < subfolders.length; i++) { def index = i def folder = subfolders[index] branches["${folder}"] = { echo "In folder ${folder}" sh "./publish.sh ${folder}" } } echo "Starting build" sh "ls -la" parallel branches } }//withCredentials }//node 部分,它将所有文件夹的映射构建为lambda(排序),然后并行执行所有这些文件夹。还有一些问题需要解决。

1 个答案:

答案 0 :(得分:0)

试试这个:

pipeline {
agent { label "master"}
stages {
    stage('1') {
        steps {
            script {
                def tests = [:]
                for (f in findFiles(glob: '**/html/*.html')) {
                    tests["${f}"] = {
                        node {
                            stage("${f}") {
                                echo '${f}'
                            }
                        }
                    }
                }
                parallel tests
            }
        }
    }       
}

}