1

I want pull requests to Git where the target is master and the source has a string "foo" to trigger a Jenkins build.

If this string "foo" appears in the source branch's name when there is a pull request, I want the Jenkins build to start.

The only way I can think of doing something this sophisticated is to put the logic into a Jenkins pipeline file. Is there a plugin or supported way of making this work? I am not using Github.

Is there a way to filter any branch that does not have "foo" in it? How do I trigger a build based on a pull request? I tried doing this by itself (without the search for the pattern "foo"), and even that failed. There were no errors, but I know Jenkins was authenticating to Git with the credentials I configured in Jenkins. The Jenkins job would not run when the conditions I configured were met.

1
  • This depends a bit on which plugins you've got on the server - do you have access to this information? Commented Jul 25, 2019 at 7:01

2 Answers 2

2

Using a multi-branch pipeline you have several different ways of filtering branches, take for instance a regular expression for "foo". In order to use this job type, you must create a Jenkinsfile in your repo with build instructions using the scripted or declarative syntax. The term pull request is typically synonymous with services like Github and Bitbucket.

multibranch pipeline branch source

You can also filter within the pipeline itself using declarative syntax:

when {changeRequest branch: "foo.*", comparator: 'REGEXP' }

And also scripted syntax:

if (env.BRANCH_NAME ==~ /foo.*/){  
    ...
}
1

Once could use the when in conjunction with the branch keyword if Jenkins declarative pipelines would be used:

pipeline {
    agent any
    stages {
        stage('Example Build') {
            steps {
                echo 'Hello World'
            }
        }
        stage('Example Deploy') {
            when {
                branch 'production'
            }
            steps {
                echo 'Deploying'
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.