I have created a webhook in gitlab to trigger jenkins job and i have selected push events.
But problem is job is getting trigger even then there is push in feature branch and i want it to trigger only when push in master branch.
I am using generic webhook trigger plugin and used below code in jenkinsfile.
properties ([
pipelineTriggers([
[$class: 'GenericTrigger',
genericVariables: [
[key: 'ref', value: '$.ref']
],
regexpFilterText: 'BRANCH: $ref END',
regexpFilterExpression: "BRANCH: refs/heads/master END"
]
])
])
What i am doing wrong here as job is geeting trigger even for feature branches.
Related
To setup Bitbucket and Jenkins Pipeline , I am using Generic Webhook Trigger Plugin in Jenkins.
I Enable it in the pipeline job.
Configure hte token string
Add the plugin endpoint in Bitbucket.
JENKINS_URL/generic-webhook-trigger/invoke?token=whatever_you_picked
this is my pipeline code for cloning the repo
pipeline{
parameters {
gitParameter branchFilter: 'origin/(.*)', defaultValue: 'dev', name: 'BRANCH_NAME', type: 'PT_BRANCH'
}
stage("clone"){
checkout([
$class: 'GitSCM',
branches: [[name: "{parms.BRANCH_NAME}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: "${GIT_CREDENTIAL_ID}", url: "${REPO_URL}"]]
])
}
It is always cloning the dev repo , whenever any branch code is pushed to bitbucket , but I want to clone that repository that is just pushed recently . There is probably a way from JSONfile , but i am not getting how to do that
We're building our branches using a "MultiBranch Pipeline" project which includes a jenkinsfile in each branch. The MultiBranch Pipeline acts as both a folder and a triggerable job. The jenkinsfile in Bitbucket gets merged like all other code.
Hello World Pipeline
Have your Generic Webhook Trigger Plugin trigger the MultiBranch Pipeline job. This will then scan all branches in your repository and trigger a build for any which have changes since it was last scanned.
In the Jenkinsfile, you can use env.BRANCH_NAME set by Jenins at build time to pull the branch code you're building at the moment.
pipeline {
agent any
stages {
stage('Prebuild') {
steps{
script{
if(env.BRANCH_NAME == 'master'){
git credentialsId: 'myCreds', url: 'myGitUrl'
}
else{
git branch: env.BRANCH_NAME, credentialsId: 'myCreds', url: 'myGitUrl'
}
}
}
}
}
}
What I am trying to do doesn't seem so complex but not so easy, but what I've read by now seems to make it look like I am launching rockets.
Basically let's say I have
my-deploys-repo with branches: master, develop
my-tools-repo with whatever branches
Inside my-tools-repo I have: Jenkinsfile-push-events ( jenkins pipeline )
Inside my-deploys-repo I have ON DEVELOP BRANCH: Jenkinsfile-deploy (which also gets some params, if it matters )
How can I trigger Jenkinsfile-deploy job from my-deploys-repo's develop branch FROM Jenkinsfile-push-events on my-tools-repo?
I understood that normally I'd do something like (inside Jenkinsfile-push-events)
stage('Deploy') {
steps {
script {
build job: '../my-deploys-repo/Jenkinsfile-deploy'
}
}
But it laying on another branch seems like a problem.
A Job in Jenkins has its definition in its properties and that already includes Jenkinsfile, so you cannot trigger a "Jenkinsfile" without defining a Job that uses that Jenkinsfile first.
If you have two branches, you need a Multibranch Pipeline job.
Let's say you created a new Multibranch Pipeline job — say MyJob is its name — that is configured to use your repo (my-deploys-repo.git) and your path to Jenkinsfile (Jenkinsfile-deploy.groovy). You can then trigger that job by:
build job: "MyJob/develop", wait: false, propagate: false,
parameters: [
string(name: 'PARAM_1', value: "1"),
string(name: 'PARAM_2', value: "maybe"), //etc.
]
I'm setting up Jenkins pipeline for my .Net Core application.
Jenkins multibranch pipeline build gets trigger on Git commit if I am configuring the checkout SCM in multibranch Pipeline configuration. But multibranch Pipeline build is not getting trigger on git commit if I checkout SCM explicitly inside Jenkins Declarative Pipeline script.
Is there any way to resolve this issue?
Below is the checkout command which I am using inside the script:
checkout([$class: 'GitSCM', branches: [], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CleanCheckout'], [$class: 'PruneStaleBranch']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'credential-id', url: 'my/git/ssh/url']]])
To get the build triggered on new git commit you should first enable SCM polling in your pipeline script by adding proper triggers directive to your Jenkinsfile:
triggers {
pollSCM 'H/2 * * * *'
}
This will poll your SCM for any changes every two minutes. If a change is detected since the last build your job will be triggered to build the changes.
Polling is the easiest way to get what you want. However, you should consider using post-commit hook instead of polling. Using polling Jenkins has to periodically check SCM for the changes. In case of post-commit hook, Jenkins will be notified about the changes by SCM if necessary. It's prefered over polling because it will relieve the number of required requests and the traffic from Jenkins towards SCM repository.
After configuring post-commit hook, the triggers directive should be modified by providing an empty string as cron parameter to pollSCM trigger.
triggers {
pollSCM ''
}
It can be confusing but this empty string is required to enable post-commit hooks requests to be handled by the job. It's also not very well documented by Jenkins docs.
So, we are currently using Multibranch pipeline to run our Continuous Integration process and the last stage is publish the deployable artifact in our JFrog Artifactory "dev" repository and this works!
My problem is that, if I want to automatically trigger a new Jenkins job to take that deployable artifact and deploy it into an integration server and run functional tests then I imagine I would do something like this at the end of my Jenkinfile:
stage("trigger artifact deployment") {
build job: deploymentPipeline,
parameters: [[$class: 'StringParameterValue', name: 'deployableArtifactId', value: "${name}-${version}"],
[$class: 'StringParameterValue', name: 'projectName', value: name],
[$class: 'StringParameterValue', name: 'projectVersion', value: version]],
...
wait: false
}
This approach works! However and because it's a Multibranch pipeline, I would have to hardcode the Jenkins job I want to trigger which I really rather not do but I don't know what else to try as I don't think there would be another way to get the info I need to find the artifact to deploy (ID, version, name, etc.), right?
If you just published it to artifactory, why do you need to find it again? I would add Artifactory properties to the file on upload to enable easier retrieval again.
https://www.jfrog.com/confluence/display/RTF/Properties
My Jenkinsfile has two SCM checkouts, primary, and secondary. I only want to have the build triggered when commits are made in primary. I've set the poll argument in the obvious way, but it does not seem to be honored; the build gets triggered when commits are made to either repository.
node {
stage("checkout") {
checkout scm: [$class: "MercurialSCM", source: "/var/jenkins_home/hg/primary", subdir: "hg/primary", clean: true], poll: true
checkout scm: [$class: "MercurialSCM", source: "/var/jenkins_home/hg/secondary", subdir: "hg/secondary", clean: true], poll: false
}
stage("do something") {
echo 'Hello World'
sh 'sleep 30s'
echo 'Done'
}
}
I could not figure out how to do this from within Jenkinsfile only.
To solve this problem for myself, I ended up creating a separate Jenkins job (Free Style project, not pipeline) which was setup to Poll SCM on the primary repo. This job does nothing except in the Post-build Actions it triggers my actual Jenkins Pipeline job which loads the Jenkinsfile like you showed.
The trigger passes a Predefined parameter set to the change ID that the poller found. In my case it was Git so I set change=${GIT_COMMIT}.
In my Pipeline job, I created a String parameter called change.
In my Jenkinsfile, I used env.change in the checkout line to checkout the specific commit.