GitHub Webhook not triggering the Jenkins Pipeline - jenkins

I want to trigger a pipeline when Pull Request is Merged..ie "action": "closed","merged":"true"
Webhook got 200 response for Jenkins
pipeline.groovy:
pipelineJob(job_name) {
parameters {
stringParam('APP_URL', app_url, 'URL of the Application')
stringParam('APP_MERGE_STATUS', app_merge_status, 'Merge Status to Trigger Job')
booleanParam('MERGED', true, 'Flag to Trigger the job')
stringParam('APP_ARTIFACT_BUCKET', artifact_bucket, 'Bucket url to upload artifacts')
stringParam('payload')
}
triggers {
genericTrigger {
genericVariables {
genericVariable {
key("APP_MERGE_STATUS")
value("\$.action")
expressionType("JSONPath")
}
genericVariable {
key("MERGED")
value("\$pull_request.merged")
expressionType("JSONPath")
}
}
printPostContent(true)
regexpFilterText("\$action")
regexpFilterExpression("")
}
}
Generic Variables I have mentioned are also used to trigger the job without github..[using parameters]
I am not sure how to write the generic trigger variables and regex for the trigger
Scenario: PR is closed and merged

If your Jenkins is exposed to the internet, you can subscribe to the webhook in Github itself
or use jenkins declarative pipeline to make use of

Got the solution..I missed to add "generic-webhook-trigger" in payload url

Related

Jenkins job to catch a particular error message from downstream job and retrigger it

I am trying to retrigger a downstream job if the job fails during the first build with the error "invalid JWT token", I want this job to retrigger again with changed parameters.
I am able to retrigger it with different parameters as of now, but what I want to achieve here is want the job to retrigger only if I get the error as " invalid Jwt token" only.
can someone help me with this, I am trying to make use of try-catch block
this is the pipeline job as of now
I assume you decide on the error by looking at the log of the second Job. If that's the case have a look at the following. Here I'm using propagate: false
pipeline {
agent any
stages {
stage('Job') {
steps {
script {
def jobBuild = build(job: 'SecondJob', wait: true, propagate: false)
def result = jobBuild.getResult()
if(result == "FAILURE"){
def log = jobBuild.getRawBuild().getLog()
if(log.contains("invalid JWT token")){
echo "Rerunning the JOB!!!!"
} else {
error "Downstream Job failed due to other error."
}
}
}
}
}
}
}

Jenkins Pipeline dynamically set parameter based on Artifactory trigger URL

I have a pipeline job that triggers when finding changes in Artifactory at a specific path. I want to pass the Artifactory url value to a parameter (my goal for this job is to allow users to manually build this job and enter a path as well as have this job automatically trigger when changes are found in a specific path in Artifactory and pass that value to the parameter).
node {
def server
def rtTriggerUrl = currentBuild.getBuildCauses('org.jfrog.hudson.trigger.ArtifactoryCause')[0]?.url
stage ('Artifactory configuration') {
server = Artifactory.server 'artifactory-1'
}
stage('Trigger build') {
server.setBuildTrigger spec: "H/2 * * * *", paths: "maven-examplerepo-local/path/to/jar"
}
}
pipeline {
parameters {
string(
name: 'JAR_LOCATION',
defaultValue: rtTriggerUrl,
trim: true,
description: 'Artifactory URL of jar'
I have tried setting it a couple different ways:
defaultValue: rtTriggerUrl
defaultValue: "${currentBuild.getBuildCauses('org.jfrog.hudson.trigger.ArtifactoryCause')[0]?.url}"
However these gave blank or null values (I also tried setting the rtTriggerUrl function before node to see if that'd make it available to the parameter but that didn't work either).
Has anyone figured out how to do this? As a workaround I created an upstream job that triggers when changes are in Artifactory, then it triggers the downstream job and passes the url value to the downstream job's parameter. I wanted to see if I could combine that logic into one job.

Triggering a multibranch pipeline job from github enterprise webhook

I am attempting to trigger multi-branch pipeline jobs from a GitHub enterprise server. I have the webhook configured to send a notification on all events. The event log on github enterprise shows that the requests to Jenkins are successful, however on the multibranch pipeline the event log is empty.
My multibranch pipeline jobs are being created using JobDSL like so:
multibranchPipelineJob("build_${repo}") {
branchSources {
branchSource {
source {
git {
id("${org}.${repo}")
remote("git#${githubEntrerpise}:${org}/${repo}")
}
}
}
}
configure {
def traits = it / sources / data / 'jenkins.branch.BranchSource' / source / traits
traits << 'jenkins.plugins.git.traits.BranchDiscoveryTrait' {}
}
triggers {
periodic(1) // Trigger every min.
}
orphanedItemStrategy { discardOldItems { numToKeep(10) } }
}
Is there anything I am missing here?
I resolved it by changing the webhook endpoint I was using.
Changing it to the following format solved the issue http://[JENINS_HOST]/git/notifyCommit?url=git#[GIT_REPO].git

How to use ${currentBuild.result} to indicate "SUCCESS" not "null"

My Jenkins declarative pipeline has the following post action:
mail to: '<snip>',
subject: "Status of pipeline: ${currentBuild.fullDisplayName}",
body: "${env.BUILD_URL} has result ${currentBuild.result}"
When the build succeeds the content of the email body is:
<job name> has result null
I understand that the value of ${currentBuild.result}" is null when the job succeeds, but this isn't convenient for the user. What is the recommended way of printing "SUCCESS" (or "FAILURE" etc) in the body message?
Use ${currentBuild.currentResult} instead.
currentResult
typically SUCCESS, UNSTABLE, or FAILURE. Will never be null.
The syntax of the available global variables is available at ${YOUR_JENKINS_URL}/pipeline-syntax/globals. See more info about globals in Jenkins documentation.
Also see https://issues.jenkins.io/browse/WEBSITE-364
You can add mail step inside post step in pipeline as below :
pipeline {
agent any
stages {
stage('Example Test') {
steps {
echo 'Hello, JDK'
}
}
}
post {
success {
echo "${env.BUILD_URL} has result success"
}
failure {
echo "${env.BUILD_URL} has result fail"
}
}
}
CurrentBuild contains the following property. You can use them according to your need.
_class,
actions,
artifacts,
building,
description,
displayName,
duration,
estimatedDuration,
executor,
fullDisplayName,
id,
keepLog,
number,
queueId,
result,
timestamp,
URL,
changeSets,
culprits,
nextBuild,
previousBuild,
number

Trigger Input Step in Jenkins via HipChat

I can find tons of information in the internet about integrating HipChat in a scripted Jenkins pipeline. So that works fine for me. But how can I post a status message to HipChat that contains an element to trigger an action back in Jenkins?
Currently I have input steps in Jenkins to approve a deployment to the next stage, e.g. PROD.
I also send a HipChat message if approval is needed which contains a link to JENKINS. That looks like this:
hipchatSend color: "${color}", notify: true, room: "Jenkins Team FooBar", message: "${env.JOB_NAME}#${env.BUILD_NUMBER}: ${message}", textFormat: true
/************** PROD **************/
stage ('Approval for PROD') {
try {
timeout(time: approvalTime, unit: approvalTimeUnit) {
input 'Do you approve the deployment to PROD?'
}
} catch (Exception ex) {
finishBuild = true
}
}
// Stop build if flag was set
if (finishBuild)
return
How can I define actions in that hipChat message? Is there a way that I can approve the next build step within HipChat?
I have not found a tutorial or documentation on how to define other kinds of hipchat messages with this plugin.
I could send a POST request to JENKINS if the message would contain standard HTML. Any ideas on how to do that?
How would it work with cards?
Thanks in advance.

Resources