简体   繁体   English

我该如何在詹金斯(Nenkins)中工作n次?

[英]How can I run a job in Jenkins n-times?

Is it possible in Jenkins to create a job, that will run n-times? 在詹金斯(Jenkins)中是否可以创建一个工作,该工作将运行n次?

I would like to write a script in configuration (windows batch command / groovy) which allows me to do it. 我想在配置中编写脚本(Windows批处理命令/ groovy),该脚本可以使我执行此操作。 In this script, I would like to have an array with parameters and then run this job with each parameter in the cycle. 在此脚本中,我想要一个带有参数的数组,然后在循环中对每个参数运行此作业。 It should look like that: 它看起来应该像这样:

paramArray [] = ["a","b","c"];
for(int i = 0; i < paramArray.length; i++)
{
    //Here I want to run this job with each parameter
    job.run(paramArray[i]);
}

Please, help me with that issue. 请帮我解决这个问题。

I found the answer! 我找到了答案!

We need to create 2 pipelines in Jenkins: downstream and upstream jobs. 我们需要在詹金斯创建2条管道:下游作业和上游作业。

1. The downstream job is parameterized and take 1 string parameter in 'General' section 1.在“常规”部分中对下游作业进行了参数化并采用1个字符串参数 在此处输入图片说明

Then, it just prints the choosing parameter in 'Pipeline' section: 然后,它仅在“管道”部分中打印选择参数: 在此处输入图片说明

Here is the result of this downstream job: 这是此下游作业的结果: 在此处输入图片说明

2. The upstream job has an array with all possible parameters for a downstream job. 2.上游作业具有一个数组,其中包含下游作业的所有可能参数。 And in the loop, it runs a downstream job with each parameter from an array. 在循环中,它使用数组中的每个参数运行下游作业。 在此处输入图片说明

In the result, an upstream job will run a downstream job 3 times with each parameter. 结果,上游作业将对每个参数运行3次下游作业。

:) :)

I think you can't run Jenkins job according to your above code. 我认为您无法根据上述代码运行Jenkins工作。 But you can configure the cronjob in Jenkins using “Build periodically” for run Jenkins job periodically. 但是您可以使用“定期构建”在Jenkins中配置cronjob,以定期运行Jenkins作业。

go to Jenkins job > Configure > tick Build periodically in build Triggers and put cronjob syntax like below image and Save. 转到Jenkins作业 > 配置 > 选中在构建触发器中定期构建,然后将cronjob语法如下图和保存。

在此处输入图片说明

This job runs every 15 minutes. 该作业每15分钟运行一次。 and also you can set a specific time in the schedule. 您还可以在时间表中设置特定时间

Please see the example from https://jenkins.io/doc/book/pipeline/jenkinsfile/ in "Handling parameters" section: With a Jenkinsfile like this (example here copied from that doc), you can launch "Build with parameters" and give params. 请在“处理参数”部分的https://jenkins.io/doc/book/pipeline/jenkinsfile/中查看示例:使用这样的Jenkinsfile(此处示例从该文档复制),您可以启动“使用参数构建”并给出参数。 Since you want multiple params, you can delimit them with , or ; 由于需要多个参数,因此可以使用或分隔它们。 or something else based on your data. 或其他基于您数据的信息。 You just need to parse the input params to get the values using the delimiter you chose. 您只需要解析输入参数即可使用您选择的定界符获取值。

pipeline {
    agent any
    parameters {
        string(name: 'Greeting', defaultValue: 'Hello', description: 'How should I greet the world?')
    }
    stages {
        stage('Example') {
            steps {
                echo "${params.Greeting} World!"
            }
        }
    }
}

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

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