Bitbucket hook build from branch does not work for Jenkins - jenkins

I have Jenkins with a job for SonarQube analysis, with the following value:
GIT SSH URL - set correctly
Branch Specifier - "**"
In Bitbucket, I have given the following value:
Jenkins URL - http://101.101.101.101:8080/Jenkins
Skip SSL Certificate - Enabled
Omit SHA1 Hash Code - Unchecked
Omit Branch Name - Unchecked
Omit the Trigger Build Button - Unchecked
Advanced Config:
Committers to Ignore - Blank
Branch Options - Build All selected and Blank field
I want Bitbucket to trigger Jenkins only if code is merged (from a pull request) to particular branch.
I tried the following settings in Bitbucket:
Branch Options - Build From selected and "release/Integeration" in the field
Still changes to every branch is triggered.
I tried this in Bitbucket:
Branch Options - Build From selected and "ref/heads/release/Integeration" in the field
Now nothing gets triggered.

Assuming you use HOOK from Bitbucket with the plugin Parameterized Builds for Jenkins and assuming you set a job using that plugin in Bitbucket. If so, did you define the pull request (PR) trigger only?

Related

How to change "default" Jenkins build status label on GitHub?

What do I have:
Jenkins job (Freestyle Project). Job trigger is GitHub Pull Request Builder
GitHub repository
Webhook to trigger the Jenkins job
What do I want: change "default" label from the screenshot below to something more specific, lets say, "Codestyle"
, so the text will be "Codestyle - Build finished."
What have I tried: adding GitHub commit status
It resulted in two build statuses:
Found a workaround (reference):
GitHub Pull Request Builder -> Application Setup:
Job -> Build Triggers -> Trigger Setup
Result:
To keep build statuses, I removed --none-- from "Commit Status Build Triggered" and "Commit Status Build Started" fields.
Build triggers ==> Trigger Setup ==> Commit Status Context ==> Here specify the name and it will appear on the github.

Unable to trigger builds for "release/" branches

We have branches set up like this:
origin/develop
origin/release/1.0.0
origin/release/1.1.0
Bitbucket
In the BitBucket, we have added Hook called "Bitbucket Server Webhooks to Jenkins" & Automatic merging is enabled.
Under the Advanced Configuration:
Branch Options is set to All and refs/remotes/origin/develop refs/remotes/origin/release/* in the input box.
Jenkins
For Jenkins, under "Source Code Management" section and "Branches to Build" option we have added the following:
Branch Specific (blank for 'any'): ${Branch}
Branch Specific (blank for 'any'): **release/*
Problem
Jenkins picks up build when we push to develop. Nothing happens when we push to any of the release branches.
Also tried manually: http://server/job/Jenkins%20Bitbucket%20Test/buildWithParameters?Branch=release/1.0.0, but this triggers a build for release/1.1.0 and not release 1.0.0.
The syntax is of the form: REPOSITORYNAME/BRANCH. In
addition, BRANCH is recognized as a shorthand of */BRANCH, * is
recognized as a wildcard, and ** is recognized as wildcard that
includes the separator /. Therefore, origin/branches* would match
origin/branches-foo but not origin/branches/foo, while
origin/branches** would match both origin/branches-foo and
origin/branches/foo.
Therefore my **release/* needs to be *release/**

Jenkins: how to trigger pipeline on git tag

We want to use Jenkins to generate releases/deployments on specific project milestones. Is it possible to trigger a Jenkins Pipeline (defined in a Jenkinsfile or Groovy script) when a tag is pushed to a Git repository?
We host a private Gitlab server, so Github solutions are not applicable to our case.
This is currently something that is sorely lacking in the pipeline / multibranch workflow. See a ticket around this here: https://issues.jenkins-ci.org/browse/JENKINS-34395
If you're not opposed to using release branches instead of tags, you might find that to be easier. For example, if you decided that all branches that start with release- are to be treated as "release branches", you can go...
if( env.BRANCH_NAME.startsWith("release-") ) {
// groovy code on release goes here
}
And if you need to use the name that comes after release-, such as release-10.1 turning into 10.1, just create a variable like so...
if( env.BRANCH_NAME.startsWith("release-") ) {
def releaseName = env.BRANCH_NAME.drop(8)
}
Both of these will probably require some method whitelisting in order to be functional.
I had the same desire and rolled my own, maybe not pretty but it worked...
In your pipeline job, mark that "This project is parameterized" and add a parameter for your tag. Then in the pipeline script checkout the tag if it is present.
Create a freestyle job that runs a script to:
Checkout
Run git describe --tags --abbrev=0 to get the latest tag.
Check that tag against a running list of builds (like in a file).
If the build hasn't occurred, trigger the pipeline job via a url passing your tag as a parameter (in your pipeline job under "Build Triggers" set "Trigger builds remotely (e.g. from scripts) and it will show the correct url.
Add the tag to your running list of builds so it doesn't get triggered again.
Have this job run frequently.
if you use multibranch pipeline, there is a discover tag. Use that plus Spencer solution

Jenkins with parameterized build

I am configuring a post commit hook and would like to be able to trigger a build on the branch that has been committed.
So far i have set up the post-commit hook file
curl http://jenkins.local:8080/git/notifyCommit?url=GITHUB_URL/REPO_NAME.git
Within Jenkins i have set
This build is parameterized
**String Paramenter**
Name: branch
Branches to build: $branch
How can i get jenkins to build the branch that has just been committed?
See http://<Your Jenkins>/job/<Your job's name>/api/:
Perform a build
If the build has parameters, post to this URL [http://<Your Jenkins>/job/<Your job's name>/buildWithParameters] and provide the parameters as form data.

Jenkins workflow job: Use parameter as branch specifier

I want to migrate our old free style dev builds, in which we use the branch name as a build parameter, to workflow builds.
This works fine so far, the only thing we are really missing is the ability to use the parameter, e.g. "branch_name", as the branch specifier for the Workflow script from SCM section.
On a free style build this works fine.
Any ideas how this could be achieved? We don't want a dev to change the configuration all the time before starting a build.
Try to disable "Lightweight checkout" checkbox.
This behavior is noted when clicking the help question mark for the "Lightweight checkout" option:
Also build parameters will not be substituted into SCM configuration in this mode.
Found that in latest comments of JENKINS-28447
Sounds like JENKINS-28447:
When selecting the "Groovy CPS DSL from SCM" option for a worflow job,
the SCM plugins do not appear to resolve build parameters or
environment variables. I am using the git plugin and when I use it
from other jobs I can specify a build parameter, like "BuildBranch",
and use that when specifying what branch should be built
The workaround would be to use an inline bootstrap script that calls load after checkout, as described in the tutorial.
Alternately, go further by creating a multibranch workflow project, so that each branch is built automatically with its own history.
I have a workflow DSL script described here: https://groups.google.com/forum/#!msg/jenkinsci-users/jSKwSKbaXq8/dG2mn6iyDQAJ
In that script, I have a build parameter called FREEBSD_SRC_URL , which is
passed down to the workflow. Based on different parameters in that URL,
a different branch can be checked out.
If you are using git, you can still use the same technique for passing a build parameter down to the script, but you would need to do things slightly differently. For example, you could define a parameter BRANCH_NAME in your job and do something like this in your workflow script:
String checkout_url = "https://github.com/jenkinsci/jenkins"
String branch_name = "master"
if (getBinding().hasVariable("CHECKOUT_URL")) {
// override default URL from build parameter
checkout_url = CHECKOUT_URL
}
if (getBinding().hasVariable("BRANCH_NAME")) {
// override default branch from build parameter
branch_name = BRANCH_NAME
}
node {
// Do the git checkout
git branch: "${branch_name}", url: "${checkout_url}"
}

Resources