gpt4 book ai didi

Jenkins 2 Pipelines - 如何为持续交付管道建模

转载 作者:行者123 更新时间:2023-12-01 09:17:48 24 4
gpt4 key购买 nike

我对 Jenkins 2 管道完全陌生。

我曾使用 Jenkins 1 管道,具有以下 View :

enter image description here

你可以直接启动某个阶段,比如说我可以选择从发布阶段开始运行,跳过Test。

我有一个非常简单的 Jenkins 2 管道定义:

stage('Preparation'){
echo """
Preparing
something
"""
}
stage('Greeting') {
parallel 'hello1':{
node{
echo 'hello world 1'
}
}, 'hello2':{
node{
echo 'hello world 2'
}
}
}

在管道页面中,我有“立即构建”,它运行从准备开始的所有阶段。

我的问题是:

  1. 如何运行我喜欢的舞台?例如问候而不是从准备开始?
  2. 如何定义阶段之间的依赖关系?我的意思是另一个阶段完成后调用的阶段
  3. 有没有办法限制某个用户可以开始的阶段?假设我只希望特定用户启动 Greeting 阶段。
  4. 您如何设置手动阶段?

更新:我的问题背后的真正目标是使用 Jenkins 2 流水线模拟如下的持续交付流水线:: p>

Build stage --> AUTO --> Acceptance Stage --> MANUAL --> Production Stage
--> MANUAL --> QA Stage

这是我想要的行为:

构建阶段(任何用户都可以启动)当它完成时会自动触发接受阶段。这个不能手动启动,只能在构建阶段成功后自动启动。

Acceptance Stage我需要只有授权用户才能手动触发QA StageProduction Stage

业务流程是:开发人员进入构建阶段,构建并打包其代码。验收阶段开始,使用打包的代码运行一堆自动化测试。

此时,当 Acceptance Stage OK 完成时,可能会发生两件事:

  1. 也许需要 QA 阶段来运行更多测试(Cucumber、手动等)。一些授权用户会触发此阶段。
  2. 当产品负责人满意时,他可以决定启动生产阶段以在生产环境中部署代码。

我正在努力使用 Jenkins 2 管道对此进行建模。

最佳答案

您的某些问题没有直接答案,但可以通过一些额外的编码来实现。虽然某些人可能会找到其他方法来实现,但让我尝试一下我的想法:

1) How can I run the stage I prefer? For instance Greeting instead of starting from Preparation?

这可以通过添加 bool 参数 FASTFORWARD_TO_GREETING 来实现,而不是使用在执行构建时提供的值来操纵构建流程。所以你的代码现在看起来像:

if (FASTFORWARD_TO_GREETING == 'false') {
stage('Preparation'){
echo """
Preparing
something
"""
}
}

stage('Greeting') {
parallel 'hello1':{
node{
echo 'hello world 1'
}
}, 'hello2':{
node{
echo 'hello world 2'
}
}
}


2) How do you define the dependencies between stages? I mean the stage called after another one completes

阶段是按顺序执行的,因此如果首先定义一个阶段,它将首先启动并完成,然后再进入下一个阶段。然而,在并行步骤中,这并不成立,因为所有步骤都将并行执行。因此,在您的示例代码中,您定义的依赖项是“准备”阶段将首先执行,然后只有“hello1”和“hello2”步骤将并行执行。但是,不能保证会打印哪个“hello world1”或“hello world 2”。

3) Is there a way to limit the stages that a certain user can start? Imagine that I only want a specific user to launch the Greeting stage.

您可以在某个阶段之前进行手动批准步骤。例如,在您的代码中,您希望执行阶段准备工作,而不是希望在执行阶段问候之前手动批准它,您的代码将如下所示:

stage('Preparation'){
echo """
Preparing
something
"""
}
stage concurrency: 1, name: 'approve-greeting'
input id: 'greeting-deploy', message: 'Proceed to Greeting?', ok: 'Deploy'

stage('Greeting') {
parallel 'hello1':{
node{
echo 'hello world 1'
}
}, 'hello2':{
node{
echo 'hello world 2'
}
}
}

这之后会发生什么,当您执行构建时,将执行阶段准备,但之后该作业将等待手动批准才能继续。在 Jenkins Pipeline View 中,该阶段将被称为“approve-greeting”,它会等到有人通过在 View 中单击它来批准构建。

4) How do you setup manual stages?

我相信答案 3 已经回答了这个问题?

如果您需要更多信息/解释,请告诉我。

编辑::请在下面找到更多答案:

Build Stage (any user can start it) when it finishes it triggers automatically the Acceptance Stage.

显然,构建阶段和验收阶段都将被定义为 Jenkins 管道中的正常阶段。所以你的代码会很简单:

node {
//define any variable here
// Get source code from repo using checkout to directory say stackoverflow
// Get source code from repo for acceptance test using checkout to directory say stackoverflow-test
//Define any tool like Maven etc. location if required.
dir('stackoverflow') {
stage name: 'build'
//Do required steps
}
dir('stackoverflow-test') {
stage name: 'Acceptance'
//Do required steps here
}

At this point, when Acceptance Stage has finished OK, two things can happen:

  1. Maybe QA Stage is needed to run more tests (Cucumber, manual, etc.). Some some authorized user would fire this stage.

  2. When the product owner is happy, he can decide to launch the Production Stage to deploy the code in a production environment.

您可以通过输入选项来做到这一点,因此在上面的代码之后您现在可以编写:

    stage 'promotion'
def userInput = input(
id: 'userInput', message: 'Let\'s promote?', parameters: [
[$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Production', name: 'prod'],
[$class: 'BooleanParameterDefinition', defaultValue: false, description: 'ManualQA', name: 'qa']
])
echo ("Env: "+userInput['prod'])
echo ("Target: "+userInput['qa'])

您可以从上面获取值并再次操纵流程。喜欢:

如果 prod 的值为真,则进入 Production 阶段,如果 qa 的值为真,则进入 QA-Manual 阶段,就像我上面的 FASTFORWARD_TO_GREETING 示例代码一样。

编辑 2

评论区进一步回答问题:

1) How or where do I specify parameters like FASTFORWARD_TO_GREETING

FASTFORWARD_TO_GREETING 等参数将被定义为作业级别 parameter

2) In the promotion stage you have to choose between ManualQA and Production. If the user chooses ManualQA it runs that Stage skipping Production. After it I want the user to be promted if he wants to promote to production stage. If you could provide a full definition of the pipeline it'd be great.

您可以在 MaualQA 阶段之后使用另一个输入步骤进行操作,但这次只使用一个参数。所以在阶段 Promotion 之后,会有阶段 ManualQA,然后是下面的输入步骤:

def userInput1 = input(
id: 'userInput', message: 'Let\'s promote?', parameters: [
[$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Production', name: 'prod']
])

3) How can I determine if a user has permissions to run a stage or not. Ideally I would like to do it based on roles

我不确定如何使用角色执行此操作,但我相信任何具有管理员访问权限或运行该作业的人都有权运行/批准该阶段,但我不能 100% 确定是否可以以某种方式对其进行修改。

关于Jenkins 2 Pipelines - 如何为持续交付管道建模,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39722622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com