简体   繁体   English

使用Pipeline Job和Groovy脚本创建新的Jenkins作业

[英]Create new Jenkins jobs using Pipeline Job and Groovy script

I have Jenkins pipeline Job with parameters (name, group, taskNumber) 我有带有参数(名称,组,任务号)的詹金斯管道作业

I need to write pipeline script which will call groovy script (this one?: https://github.com/peterjenkins1/jenkins-scripts/blob/master/add-job.groovy ) 我需要编写将调用groovy脚本的管道脚本(这个吗?: https : //github.com/peterjenkins1/jenkins-scripts/blob/master/add-job.groovy

I want to create new job (with name name_group_taskNamber) every times when I build main Pipeline Job. 我每次构建主管道作业时都想创建一个新作业(名称为name_group_taskNamber)。

I don't understand: Where do I need to put may groovy script ? 我不明白:我需要在哪里放一些时髦的脚本? How does Pipeline script should look like? 管道脚本应如何显示? :

node{
    stage('Build'){
        def pipeline = load "CreateJob.groovy"
        pipeline.run()
    }
}

我们通过使用https://wiki.jenkins.io/display/JENKINS/Jobcopy+Builder+plugin来做到这一点,尝试在管道脚本中构建另一步骤并传递要考虑的参数

You can use and configure a shared library like here (a git repo): https://github.com/lvthillo/shared-library . 您可以在此处使用和配置共享库 (一个git repo): https : //github.com/lvthillo/shared-library You need to configure this in your Jenkins global configuration. 您需要在Jenkins全局配置中进行配置。

It contains a folder vars/ . 它包含一个文件夹vars/ Here you can manage pipelines and groovy scripts like my slackNotifier.groovy . 在这里,您可以管理管道和groovy脚本,例如我的slackNotifier.groovy The script is just a groovy script to print the build result in Slack. 该脚本只是一个用于在Slack中打印生成结果的常规脚本。

In the jenkins pipeline job we will import our shared library: 在jenkins管道作业中,我们将导入共享库:

@Library('name-of-shared-pipeline-library')_

mavenPipeline {
  //define parameters
}

In the case above also the pipeline is in the shared library but this isn't necessary. 在上述情况下,管道也位于共享库中,但这不是必需的。

You can just write your pipeline in the job itself and call only the function from the pipeline like this: This is the script in the shared library: 您可以只在作业本身中编写管道,然后仅从管道中调用函数,如下所示:这是共享库中的脚本:

// vars/sayHello.groovy
def call(String name = 'human') {
    echo "Hello, ${name}."
}

And in your pipeline: 并在您的管道中:

final Lib= library('my-shared-library')

...
  stage('stage name'){
    echo "output"
    Lib.sayHello.groovy('Peter')
  }
...

EDIT: In new declarative pipelines you can use: 编辑:在新的声明性管道中,您可以使用:

pipeline {
    agent { node { label 'xxx' } }

    options {
        buildDiscarder(logRotator(numToKeepStr: '3', artifactNumToKeepStr: '1'))
    }

    stages {
        stage('test') {
            steps {
                sh 'echo "execute say hello script:"'
                sayHello("Peter")
            }
        }
    }

    post {
        always {
            cleanWs()
        }
    }
}

def sayHello(String name = 'human') {
    echo "Hello, ${name}."
}

output: 输出:

[test] Running shell script
+ echo 'execute say hello script:'
execute say hello script:
[Pipeline] echo
Hello, Peter.
[Pipeline] }
[Pipeline] // stage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM