Jenkins with Github Trigger (Github Plugin): How to use other branch (aside from master/main)? - github

Description:
I am trying to have Github webhook to trigger my Jenkins job. I am successful in triggering the job using the Main branch but no success using other branches. I have a new branch called 'develop' but it does not trigger the job.
Current Setup:
In Jenkins, I am using Pipeline -> Pipeline Script from SCM.
Under SCM, My repository is defined and it can access Main branch pr
But when I change it to other branch like 'develop', It does not work.
Is there additional configuration to use other branches?

Apparently, the input is accepting Regular Expression. The initial specified branch is '*/master'. Therefore, I used '*/develop' to trigger my Jenkins job for develop branch

Related

How to trigger a task on merged pull requests only?

In Azure Devops, I have a repo that's in Bitbucket. I'd like to trigger a package publish on every approved pr that gets merged to the develop branch.
I've figured out how to conditionally run a task if the build is a pr or not, and how to trigger if the pr is to develop, but that means that the task is run for every PR created to develop. I'd like the task to only run when the pr has been merged to develop.
I noticed the following variables in my pipeline:
SYSTEM_PULLREQUEST_ISFORK=False
SYSTEM_PULLREQUEST_MERGEDAT=
SYSTEM_PULLREQUEST_PULLREQUESTID=139
SYSTEM_PULLREQUEST_PULLREQUESTNUMBER=139
SYSTEM_PULLREQUEST_SOURCEBRANCH=source-branch
SYSTEM_PULLREQUEST_SOURCECOMMITID=e55835e7e2e65ad87fd09a03959fefcfcc4d475f
SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI=[repoURL]
SYSTEM_PULLREQUEST_TARGETBRANCH=develop
And the SYSTEM_PULLREQUEST_MERGEDAT= variable stood out. Anyone have suggestions? Am I overly complicating this?
It is possible to achieve this with just conditions. Let's say you were merging from feature branch to develop branch. And you only want a task to be executed when the pr has been merged to develop.
First of all you should know the default CI triggers and PR triggers for Bitbucket repository on Azure pipeline.
1, CI triggers
If you don't specify any triggers, the default is as if you wrote below, which means commit to any branch will trigger the pipeline.
trigger:
branches:
include:
- '*'
When you specify a trigger, it replaces the default implicit trigger, and only pushes to branches that are explicitly configured to be included will trigger a pipeline. Includes are processed first, and then excludes are removed from that list.
2, PR triggers
If no pr triggers appear in your YAML file, pull request validations are automatically enabled for all branches.
When you specify a pr trigger, it replaces the default implicit pr trigger, and only pushes to branches that are explicitly configured to be included will trigger a pipeline.
Each new run builds the latest commit from the source branch of the pull request. This is different from how Azure Pipelines builds pull requests in other repositories (e.g., Azure Repos or GitHub), where it builds the merge commit,
See the document for more information.
So if you don't specify any CI triggers or PR Triggers. The default behavior is to enable the triggers for all branches. And the PR triggers will only trigger the pipeline to build the last commit from the source branch(ie. Feature branch) instead of develop branch.
So it will explain why there are two triggered builds on an update to a pr. one is CI trigger(ie. IndividualCI), another is PullRequest. Both builds were against the source branch (ie.feature).
When the pr was merged to develop. what happened was a new commit being added to develop branch, which will trigger the CI build. So the task you want to trigger should be run against develop branch.
As for above case of yours. I suggest you disable the pr triggers and only enable the CI triggers.(for pr triggers will only build the latest commit from the source branch, which is the same with CI trigger. )
You can disable the pr trigger like below:
pr: none
So you can just set the condition like below for the task
- task: taskname
input:
condition: and(eq(variables['Build.SourceBranchName'], 'develop'), eq(variables['Build.Reason'], 'IndividualCI'))
You can also use Webhook to trigger the azure pipeline. And set the condtion to eq(variables['Build.Reason'], 'ResourceTrigger')
resources:
webhooks:
- webhook: bitbucketwebhook
connection: bitbucketwebhook
Please see this thread for more information.

How to trigger Jenkins build on specific branch(release branch) merges to master in Jenkins

I am trying to trigger a build on the master branch when a specific branch is merged into it i.e., To trigger a Jenkins pipeline build when a release branch is merged to the master branch.
What I am able to do is if any branch is merged to master, it is triggering the Jenkins build, but what I want is to build to be only triggered on release branch merges into master. For this I used Generic webhook trigger Plugin(GWT). Not sure if I can use Generic webhook trigger for specific branch merge into master.
I tried the following settings in Jenkins pipeline configuration using GWT plugin.
Optional Filter:
Expression - ^(refs/heads/master)$
This expression is working when I merge any branch into Master and triggering the build. But, I want the merge to happen just when a release branch is merged into Master.
You should use the
when
step in your pipeline. There you use the environment variable which contains you branch name from the generic webhook trigger plugin.

How do I exclude specific source branches from CI triggers for an Azure Repos Git project?

I'm hoping to somehow replicate the functionality of PR triggers which, according to the docs, are currently only supported for GitHub and Bitbucket Cloud repos. I'd like my CI pipelines to not trigger if the change is incoming from certain branches.
I've mostly tried to solve this problem with GitVersion, which is the part of my pipeline that makes it problematic to trigger builds when I'm merging back from a release build or master back onto develop. So far I've had no luck, so now I'm hoping I've overlooked a feature of Azure Pipelines which will help.
My current pipeline trigger:
trigger:
batch: true
branches:
include:
- develop
paths:
exclude:
- ReadMe.md
- development-pipeline.yml
- release-pipeline.yml
- GitVersion.yml
I'd like a pull request which originated in a release branch (can be identified with the regex pattern [Rr]eleases?[\/-]) or master to not trigger my pipeline. In reality, any change to the develop branch triggers the build.
If you just want the develop branch not to trigger ci build, then you can check "Enable continuous integration" option in the Continuous integration of builds Triggers and set exclude develop branch in the branch filters.
If you want some source branches to trigger the CI build of the develop branch, some can't. I am afraid that this feature you want is not achievable. Once your deveop branch
Enable continuous integration, then the deveop branch will trigger the build pipeline once it changes.
If you want to merge the commits on the release or master branch into the develop branch, and create pr does not trigger the CI build, you could enable the build policy in the Build validation in the branch policy.In this way, only after PR is completed will CI build be triggered.But pr build is unavoidable.

Limit which branch is built by Jenkins pipeline?

I am currently configuring a Jenkins server hosted on a Docker container in AWS.
I am using BlueOcean to configure a repository.
Right now, the pipeline scans all branches on a repository to detect Jenkinsfiles and then will automatically build on that branch if it detects changes. I scan the repo every 5 minutes to detect changes.
However, I do not want to be running builds and jobs automatically if it is some random feature branch. I am trying to limit the automatically triggered builds to only changes in staging and master branches.
So my question is, how/where do you configure Jenkins GitHub pipeline to only build on certain branches rather than scanning all branches?
A Multibranch pipeline job is your friend.
Rather than trying to limit which branches Jenkins is polling firstly what I do in my Jenkinsfile is poll source control every minute:
triggers { pollSCM('* * * * *') }
This will poll every branch and create a job where it finds a Jenkinsfile in the location and name you specify in the Multibranch Pipeline job configuration.
Side Note
About the only configuration in a multibranch pipeline is:
Where's the SCM repo?
Workspace relative path and name of Jenkinsfile. (You can call it Bob if you want)
A multibranch pipeline job sets an additional environment variable: BRANCH_NAME which allows you to conditionally perform actions in pipeline like so:
script {
if( "${env.BRANCH_NAME}" == "integration" ) {
//Do something useful
}
}
Using this method you can also decide to do nothing in response to a poll event.
I assume you are using github plugin. I'd suggest configuring a webhook on your repository using Generic Webhook Trigger Plugin - https://wiki.jenkins.io/display/JENKINS/Generic+Webhook+Trigger+Plugin
This plugin is awesome and lets you easily extract the values in the incoming webhook and use those in your pipeline. For ex. you can extract the branch from where the webhook came from and only build if the branch is staging or master
In our setup we use a simple job 'webhook trigger processor' which reads the incoming webhook from all repositories and triggers downstream pipelines using values extracted from webhook.
Pipeline accepts input parameters. So you can create a parameter called branch.
Inside your pipeline you could use regex to match only required branches.

Automatically Build GitHub Branch on Commit

I am new to working with Jenkins pipeline. I am able to use the GitHub Plugin in Jenkins and Webhooks from GitHub to successfully build a specific branch of a repository for a free style job. I can't find documentation that documents how to setup the "Source Code Management" so that only specific branches are build based on the github webhook.
For now I can chain the pipeline job to a free style job so that I can build only specific branches. I would rather have the pipeline job configured specifically for the branch we are trying to build.
Thanks in advance for your help!
SCM Configuration for the pipeline job.
!https://i.stack.imgur.com/0NoOX.png
In order to accomplish this within a Jenkins Pipeline job, you must mark the Pipeline Definition as "Pipeline script from SCM". This will instruct Jenkins to base the pipeline execution based on a Jenkinsfile within the repository. Here, you can also instruct Jenkins which branches to build.
From there, you simply need to make sure that your GitHub pushes are triggering builds within Jenkins correctly, and that's all there is to it!
The goal of the pipeline job was to build and deploy specific branch automatically. The approach was to create a pipeline job and define the branch in the SCM configuration and enable webhooks so that the branch would automatically build when a new commit is pushed. Unfortunately the webhook SCM build for pipeline is broken or is not supported for webhooks.
We have decided to change our approach and use the multibranch pipeline job. This by default build ALL branches that have a jenkinsFile. We are filtering in the job for the specific branches we want automatically build.