我在Jenkins中的项目有一个名为action的“Extended Choise Parameter”参数,它有三个值:build、burn、test,这三个值都设置为默认值。
我还启用了Generic Webhook Trigger并设置了一个令牌字符串,并在Gitlab上的repo中配置了webhook。
我的目标是:
1.如果需要,在Jenkins中手动触发项目;
1.当新代码合并到gitlab的主分支时,自动通过webhook触发Jenkins中的项目;
我的管道:
pipeline {
agent any
environment {
def SRCCODE_DIR = "/var/jenkins_home/workspace/tmptest"
_build="${sh(script:' echo $action|grep -w -o "build" || echo "error" ', returnStdout: true).trim()}"
_burn="${sh(script:' echo $action|grep -w -o "burn" || echo "error" ', returnStdout: true).trim()}"
_test="${sh(script:' echo $action|grep -w -o "test" || echo "error" ', returnStdout: true).trim()}"
}
triggers {
GenericTrigger(
genericVariables: [
[key: 'source_branch', value: '$.object_attributes.source_branch', expressionType: 'JSONPath'],
[key: 'target_branch', value: '$.object_attributes.target_branch', expressionType: 'JSONPath'],
[key: 'action', value: '$.object_attributes.action', expressionType: 'JSONPath'],
[key: 'action_status', value: '$.object_attributes.state', expressionType: 'JSONPath'],
],
token: 'tmptest',
regexpFilterText: '$target_branch_$action_status',
regexpFilterExpression: 'main_merged',
causeString: 'new code merge to main',
//printContributedVariables: true,
printPostContent: true
)
}
stages {
stage("pull code") {
when { environment name: '_build', value: 'build' }
steps {
println "pull code"
}
}
stage("build image") {
when { environment name: '_build', value: 'build' }
steps {
println "build image"
}
}
stage("deploy image") {
when { environment name: '_build', value: 'build' }
steps {
println "deploy image"
}
}
stage("send message") {
when { environment name: '_build', value: 'build' }
steps {
println "send msg"
}
}
stage("burn image") {
when { environment name: '_burn', value: 'burn' }
steps {
println "burn image"
}
}
stage("smoketest") {
when { environment name: '_test', value: 'test' }
steps {
println "smoke test"
}
}
stage("mail report") {
steps {
println "send testreport"
}
}
}
字符串
}
当我手动触发项目时,它工作得很好,所有阶段都被执行。
但是当它被Webhook触发时,只执行最后一个阶段,
我知道有些地方错了,但我不知道哪里错了。我对Jenkins没什么兴趣,有什么想法吗?
谢谢你,谢谢
1条答案
按热度按时间f1tvaqid1#
扩展选择处于“特征结束”状态。也许你可以在params或input中使用3个boolean参数。
你不需要环境变量。可以使用when -表达式
输入步长比参数具有一些优势。它可以访问管道中的变量、参数-仅全局参数。但想法是一样的:
字符串