如何在jenkins上从jfrog webhook触发器获取负载信息

6tqwzwtp  于 2022-11-01  发布在  Jenkins
关注(0)|答案(2)|浏览(357)

我正在设置一个jenkins作业,只要在jfrog中部署了工件,就会触发该作业。我已经按照下面文档中的步骤操作,并且能够触发该作业。但不幸的是,我无法获取变量值。我不知道如何查看jenkins端接收到的有效负载,以提取所需的变量。
https://www.eficode.com/blog/triggering-jenkins-pipelines-on-artifactory-events
我的管道:

pipeline {
    agent any

    triggers {
        GenericTrigger(
            genericVariables: [
                [key: 'ARTIFACT_NAME', value: '$.artifactory.webhook.data.name'],
                [event: 'EVENT_NAME', value: '$.artifactory.webhook.event']
            ],
            causeString: 'Triggered on $ARTIFACT_NAME'
        )
    }

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

Jenkins作业日志:

java.lang.NullPointerException: Variable name
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:204)
    at org.jenkinsci.plugins.gwt.GenericVariable.<init>(GenericVariable.java:31)
Caused: java.lang.reflect.InvocationTargetException
    at sun.reflect.GeneratedConstructorAccessor6020.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:330)
Caused: java.lang.IllegalArgumentException: Could not instantiate {event=EVENT_NAME, value=$.artifactory.webhook.event} for org.jenkinsci.plugins.gwt.GenericVariable
    at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:334)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:474)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.coerceList(DescribableModel.java:585)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:458)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.buildArguments(DescribableModel.java:409)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:329)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:272)
    at org.jenkinsci.plugins.pipeline.modeldefinition.CommonUtils.instantiateDescribable(CommonUtils.java:131)
    at org.jenkinsci.plugins.pipeline.modeldefinition.CommonUtils$instantiateDescribable.callStatic(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
    at org.kohsuke.groovy.sandbox.impl.Checker$2.call(Checker.java:194)
    at org.kohsuke.groovy.sandbox.GroovyInterceptor.onStaticCall(GroovyInterceptor.java:35)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onStaticCall(SandboxInterceptor.java:186)
    at org.kohsuke.groovy.sandbox.impl.Checker$2.call(Checker.java:192)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedStaticCall(Checker.java:196)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:103)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
Caused: java.lang.IllegalArgumentException: Could not instantiate {genericVariables=[{key=ARTIFACT_NAME, value=$.artifactory.webhook.data.name}, {event=EVENT_NAME, value=$.artifactory.webhook.event}], causeString=Triggered on $ARTIFACT_NAME} for org.jenkinsci.plugins.gwt.GenericTrigger
wz3gfoph

wz3gfoph1#

我做到了。我们必须在通用webhook触发器选项下的configure jenkins作业中启用打印发布内容。一旦完成,它就开始在控制台日志中显示json有效负载。之后,我们可以使用jsonpath过滤器从有效负载添加变量


指令集

dy1byipe

dy1byipe2#

我想建议使用Jenkins Artifactory plugin的另一种方法:
您可以在UI中配置生成触发器:

或者您可以在管道中配置构建触发器:

stages {
    stage('Artifactory configuration') {
        steps {
            rtServer(
                    id: "ARTIFACTORY_SERVER",
                    url: SERVER_URL,
                    credentialsId: CREDENTIALS
            )
        }
    }

    stage('Add build trigger') {
        steps {
            rtBuildTrigger(
                    serverId: "ARTIFACTORY_SERVER",
                    spec: "*/10 * * * *",
                    paths: "generic-libs-local/builds/starship"
            )
        }
    }
}

要在Artifactory中获取导致触发的路径,可以执行以下操作:

environment {
    // The URL of the artifact in Artifactory, caused the job to be triggered.
    // May be empty if the build isn't triggered by a change in Artifactory.
    RT_TRIGGER_URL = "${currentBuild.getBuildCauses('org.jfrog.hudson.trigger.ArtifactoryCause')[0]?.url}"
}

资源:
1.正在触发生成
1.触发宣告式管缐中的建置

  1. Example

相关问题