Jenkins 2管道-如何为连续输送管道建模
我对詹金斯2号管道完全陌生 我曾与Jenkins 1合作过,并持有以下观点: 您可以直接启动某个阶段,比如说,我可以选择从发布阶段开始运行,跳过测试 我有一个非常简单的Jenkins 2管道定义:Jenkins 2管道-如何为连续输送管道建模,jenkins,jenkins-plugins,jenkins-pipeline,continuous-delivery,jenkins-2,Jenkins,Jenkins Plugins,Jenkins Pipeline,Continuous Delivery,Jenkins 2,我对詹金斯2号管道完全陌生 我曾与Jenkins 1合作过,并持有以下观点: 您可以直接启动某个阶段,比如说,我可以选择从发布阶段开始运行,跳过测试 我有一个非常简单的Jenkins 2管道定义: stage('Preparation'){ echo """ Preparing something """ } stage('Greeting') { parallel 'hello1':{ node{
stage('Preparation'){
echo """
Preparing
something
"""
}
stage('Greeting') {
parallel 'hello1':{
node{
echo 'hello world 1'
}
}, 'hello2':{
node{
echo 'hello world 2'
}
}
}
在管道页面中,我有一个“Build now”,它运行从准备开始的所有阶段
我的问题是:
Build stage --> AUTO --> Acceptance Stage --> MANUAL --> Production Stage
--> MANUAL --> QA Stage
这就是我想要的行为:
构建阶段(任何用户都可以启动)完成后,它会自动触发验收阶段。这个不能手动启动,只能在成功完成构建阶段后自动启动
从验收阶段我需要只有授权用户才能手动触发QA阶段和生产阶段
业务流程是:开发人员进入构建阶段,其代码被构建和打包。验收阶段开始,使用打包的代码运行一系列自动化测试
此时,验收阶段完成OK后,可能会发生两件事:
我正在努力用Jenkins 2管道对此进行建模。您的一些问题没有直接的答案,但可以通过一些额外的编码来实现。虽然某些人可能会找到其他方法来实现,但让我用我的想法来尝试:
1) How can I run the stage I prefer? For instance Greeting instead of starting from Preparation?
这可以通过将布尔参数FASTFORWARD\u添加到\u来实现,而不是使用在执行构建时提供的值来操纵构建流。因此,您的代码现在看起来像:
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管道视图中,该阶段将被称为“approve greeting”,它将等待有人通过在视图中单击它来批准构建
4) How do you setup manual stages?
我相信答案3回答了这个问题
如果您需要进一步的信息/解释,请告诉我
编辑::请在下面找到进一步的答案:
构建阶段(任何用户都可以启动它)在完成时触发
自动进入验收阶段
显然,在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
}
在这一点上,当验收阶段已经完成OK时,两件事情可以
发生:
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的值为真,则进入生产阶段,
如果qa值为真,则进入qa手册阶段,就像我上面的快速前进问候语示例代码一样
编辑2
进一步回答评论部分的问题:
1) 如何或在何处指定参数,如快进\u到\u问候语
快速转发\u至\u问候语等参数将定义为作业级别
2) 在促销阶段,您必须在ManualQA和
生产。如果用户选择ManualQA,则会跳过该阶段
生产。之后,如果用户愿意,我希望他得到提示
提升到生产阶段。如果你能提供一个完整的定义
管道会很好的
在MaualQA阶段之后,您可以使用另一个输入步骤来处理这个问题,但这次只使用一个参数。因此,在阶段升级之后,将有阶段手册QA,然后是以下输入步骤:
def userInput1 = input(
id: 'userInput', message: 'Let\'s promote?', parameters: [
[$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Production', name: 'prod']
])
3) 如何确定用户是否有权运行阶段或阶段
不理想情况下,我会根据角色来做
我不确定如何处理角色,但我相信任何具有管理员权限或运行该作业的人都有权运行/批准该阶段,但我不能100%确定是否可以
node{
def user
def userInput
def mvnHome = tool 'M3'
wrap([$class: 'BuildUser']) {
user = env.BUILD_USER_ID
}
stage('Commit Stage'){
echo 'Downloading from Git...'
git 'https://github.com/codependent/spring-nio-rest.git'
'Building project...'
sh "${mvnHome}/bin/mvn clean install -DskipTests"
}
stage('Acceptance Stage') {
echo """
Getting image from Nexus...OK
Deploying image...OK
Executing tests...OK
"""
userInput = input(id: 'userInput', message: 'Select the next stage:', parameters: [
[$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Run QA tests', name: 'QA'],
[$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Run performance tests', name: 'performance']
])
}
if(userInput['QA']){
stage('QA Stage') {
echo """
Getting image from Nexus...OK
Deploying image...OK
Executing QA tests...OK
"""
}
}
if(userInput['performance']){
stage('Performance Stage') {
echo """
Getting image from Nexus...OK
Deploying image...OK
Executing Performance tests...OK
"""
}
}
stage('Production Stage') {
input message: 'Are you sure you want to deploy to Production?', submitter: 'codependent'
echo 'Deploying to Production...OK'
}
}