gitlab通过webhook使用参数触发jenkins项目

xytpbqjk  于 2023-08-03  发布在  Jenkins
关注(0)|答案(1)|浏览(227)

我在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没什么兴趣,有什么想法吗?
谢谢你,谢谢

f1tvaqid

f1tvaqid1#

扩展选择处于“特征结束”状态。也许你可以在params或input中使用3个boolean参数。
你不需要环境变量。可以使用when -表达式
输入步长比参数具有一些优势。它可以访问管道中的变量、参数-仅全局参数。但想法是一样的:

def action_params = [:]

pipeline {
  agent any
  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
        )
  }
  environment { 
    SRCCODE_DIR = "/var/jenkins_home/workspace/tmptest"
  }
  stages {    
    stage("input") {
      steps{
        script{
          catchError(buildResult: 'SUCCESS', message: 'Using defaul vars') {
            timeout(time: 13, unit: 'SECONDS') {
              action_params = input(
                  message: 'select action',
                  parameters: [
                  booleanParam (
                    defaultValue: action == 'build',
                    description: 'Build',
                    name: 'build'),
                  booleanParam (
                    defaultValue: action == 'burn',
                    description: 'Burn',
                    name: 'burn'),
                  booleanParam (
                    defaultValue: action == 'test',
                    description: 'test',
                    name: 'test') ])
            }
          }
        }
      }
    }

    stage("Build") {
      when { expression {action_params.build ?: action == 'build'   } }
      stages{
        stage('Pull'){
          steps {        
            println "pull code"
          } 
        }    
        stage("build image") {
          steps {        
            println "build image"
          }       
        }
        stage("deploy image") {
          steps {        
            println "deploy image"
          }       
        }

        stage("send message") {
          steps {        
            println "send msg"
          }       
        }
      }
    }
    stage("burn image") {
      when { expression {action_params.burn ?: action == 'burn' } }
      steps {  
        println "burn image"
      }       
    }
    stage("smoketest") {
      when { expression {action_params.test ?: action == 'test'} }
      steps {  
        println "smoke test"
      }       
    }
    stage("mail report") {
      steps {        
        println "send testreport"
      }       
    }   
  }
}

字符串

相关问题