在Jenkins中编写管道插件

时间:2018-03-01 09:11:18

标签: java jenkins groovy jenkins-pipeline

我正在写一个Jenkinsplugin。我已经设置了一个管道脚本,当我执行脚本时,它调用一些shell脚本并设置一个管道。这很好。

我的代码示例:

node('master') {
        try {            
            def appWorkspace = './app/'
            def testWorkspace = './tests/'

            stage('Clean up') { 
            //    cleanWs()

            }

            stage('Build') {
                parallel (
                    app: {
                        dir(appWorkspace) {
                            git changelog: false, credentialsId: 'jenkins.git', poll: false, url: 'https://src.url/to/our/repo'

                            dir('./App') {
                                sh "#!/bin/bash -lx \n ./gradlew assembleRelease"
                            }
                        }
                    },
                    tests: {
                        dir(testWorkspace) {
                            git changelog: false, credentialsId: 'jenkins.git', poll: false, url: 'https://src.url/to/our/repo'

                            sh "#!/bin/bash -lx \n nuget restore ./Tests/MyProject/MyProject.sln"
                            sh "#!/bin/bash -lx \n msbuild ./Tests/MyProject/MyProject.Core/ /p:Configuration=Debug"
                        }
                    }
                )
            }

            stage('Prepare') {
                parallel (
                    'install-apk': { 
                        sh '''#!/bin/bash -lx
                        result="$(adbExtendedVersion shell pm list packages packagename.app)"

                        if [ ! -z "$result" ]
                        then
                            adbExtendedVersion uninstall packagename.app
                        fi

                        adbExtendedVersion install ''' + appWorkspace + '''/path/to/app-release.apk'''
                    },
                    'start-appium': { 
                        sh "#!/bin/bash -lx \n GetAllAttachedDevices.sh"
                        sh "sleep 20s"
                    }
                )
            }

            stage('Test') {
                // Reading content of the file
                def portsFileContent = readFile 'file.txt'

                // Split the file by next line
                def ports = portsFileContent.split('\n')

                // Getting device IDs to get properties of device
                def deviceIDFileContent = readFile 'IDs.txt'
                def deviceIDs = deviceIDFileContent.split('\n')

                // Define port and id as an pair
                def pairs = (0..<Math.min(ports.size(), deviceIDs.size())).collect { i -> [id: deviceIDs[i], port: ports[i]] }

                def steps = pairs.collectEntries { pair ->
                    ["UI Test on ${pair.id}", {
                        sh "#!/bin/bash -lx \n mono $testWorkspace/Tests/packages/NUnit.ConsoleRunner.3.7.0/tools/nunit3-console.exe $testWorkspace/Tests/bin/Debug/MyProject.Core.dll --params=port=${pair.port}"
                    }]
                }
                parallel steps
            }
        }

catch (Exception e) {
    println(e);
}
finally {
    stage('Clean') {
        archiveArtifacts 'TestResult.xml'
        sh "#!/bin/bash -lx \n KillInstance.sh"   
    }
  }
}

这是一个定义我的管道的groovy脚本。我试图通过我的插件实现的是,使用此插件的用户只是插入一些路径变量,例如。他的解决方案的路径,或他的github源的路径。然后,我的插件会使用给定的参数自动执行上面列出的脚本。

我的问题是,我找不到任何文档如何用Java编写这样的管道构造。如果有人能指出我正确的方向,我会很感激。

0 个答案:

没有答案