Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture jenkins build cause? triggered by GitLab

In Jenkins file(groovy syntax), manual build action(if any) cause is grabbed using hudson.model.Cause$UserIdCause in currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')


hudson.triggers.SCMTrigger.SCMTriggerCause looks deprecated in Jenkins CI code.

    @Deprecated
    public SCMTrigger(String scmpoll_spec, boolean ignorePostCommitHooks) throws ANTLRException {
        super(scmpoll_spec);
        this.ignorePostCommitHooks = ignorePostCommitHooks;
    }

On push event or merge event from GitLab, How do Jenkinsfile read the build cause?

like image 532
overexchange Avatar asked Dec 07 '25 19:12

overexchange


1 Answers

If you're looking to execute a stage on a Gitlab trigger:

stage('stage') {
    when {
        triggeredBy "GitLabWebHookCause"
    }
}

To get the data of the Gitlab cause you have the following syntax:

currentBuild.rawBuild.getCause(com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause).getData()

Example: get last commit

commit = currentBuild.rawBuild.getCause(com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause).getData().getLastCommit()

Documentation of the GitlabWebHookCause: https://javadoc.jenkins.io/plugin/gitlab-plugin/com/dabsquared/gitlabjenkins/cause/GitLabWebHookCause.html

like image 102
Remy Avatar answered Dec 09 '25 13:12

Remy