How to parse jenkins artifactory trigger plugin output? - jenkins

I have setup "Enable Artifactory trigger" on my jenkins job. The job gets triggered automatically whenever there is an update in the artifactory path.
The trigger job output provides the artifact that triggered the build.
What i need is to parse the artifact and use as an environment variable?
Jenkins console log of artifactory trigger job
The build was triggered by a change in Artifactory.
The artifact which triggered the build is: http://xxx.xxx.com/artifactory/api/storage/artifactory-snapshot/ABC/exp-abc-12-5-br/abc-12-5-99-015/up-image.tgz
[EnvInject] - Loading node environment variables.
Building remotely on slave in workspace /data/engit-private-jenkins/workspace/Tools/test_artifactory_trigger
[test_artifactory_trigger] $ /bin/sh -xe /tmp/jenkins6159521155154940953.sh
+ echo 'hello world'
hello world
Finished: SUCCESS
How do I access the http artifactory url in the above jenkins console log in the same job? I dont see it getting stored in the environment

We can get the URL of the file in Artifactory that triggered the job using :
environment {
RT_TRIGGER_URL = "${currentBuild.getBuildCauses('org.jfrog.hudson.trigger.ArtifactoryCause')[0]?.url}"
}
Build trigger documentation link

Related

Jenkins simple pipeline not triggered by github push

I have created a jenkins pipeline job called "pipelinejob" with the below script:
pipeline {
agent any
stages {
stage ('Setup'){
steps{
//echo "${BRANCH_NAME}"
echo "${env.BRANCH_NAME}"
//echo "${GIT_BRANCH}"
echo "${env.GIT_BRANCH}"
}
}
}
}
Under General, I have selected "GitHub project" and inserted my company's github in the form:
https://github.mycompany.com/MYPROJECTNAME/MY_REPOSITORY_NAME/
Under Build Triggers, i have checked "GitHub hook trigger for GITScm polling
I have created a simple job called "simplejob" with same configuration as 1) and 2)
In my company's Github, i have created a webhook like "jenkins_url/jenkins/github-webhook/"
I commit a change in "mybranch" in "MY_REPOSITORY_NAME"
My simple job "simplejob" is triggered and built successfully
My pipeline job "pipelinejob" is not triggered
In Jenkins log i see the below:
Sep 12, 2019 2:42:45 PM INFO org.jenkinsci.plugins.github.webhook.subscriber.DefaultPushGHEventSubscriber$1 run
Poked simplejob
Nothing regarding my "pipelinejob".
Could you please point me to the right directions as to what to check next?
P.S. I have manually executed my "pipelinejob" successfully
I wasted two days of work on this, as none of the previous solutions worked for me. :-(
Eventually I found the solution on another forum:
The problem is that if you use a Jenkinsfile that is stored in GitHub, along with your project sources, then this trigger must be configured in the Jenkinsfile itself, not in the Jenkins or project configuration.
So add a triggers {} block like this to your Jenkinsfile:
pipeline {
agent any
triggers {
githubPush()
}
stages {
...
}
}
Then...
Push your Jenkinsfile into GitHub
Run one build manually, to let Jenkins know about your will to use this trigger.
You'll notice that the "GitHub hook trigger for GITScm polling" checkbox will be checked at last!
Restart Jenkins.
The next push should trigger an automated build at last!
On the left side-pane of your pipeline job, click GitHub Hook log. If it says 'Polling has not run yet', you will need to manually trigger the pipeline job once before Jenkins registers it to poke on receiving hooks.
Henceforth, the job should automatically trigger on GitHub push events.
I found an answer to this question with scripted pipeline file. We need to declare the Github push event trigger in Jenkins file as follows.
properties([pipelineTriggers([githubPush()])])
node {
git url: 'https://github.com/sebin-vincent/Treasure_Hunt.git',branch: 'master'
stage ('Compile Stage') {
echo "compiling"
echo "compilation completed"
}
stage ('Testing Stage') {
echo "testing completed"
echo "testing completed"
}
stage("Deploy") {
echo "deployment completed"
}
}
}
The declaration should be in the first line.
git url: The URL on which pipeline should be triggered.
branch: The branch on which pipeline should be triggered. When you specify the branch as master and make changes to other branches like develop or QA, that won't trigger the pipeline.
Hope this could help someone who comes here for an answer for the same problem with Jenkins scripted pipeline :-(.
The thing is whenever you create a pipeline job for git push which is to be triggered by github-webhook, first you need to build the pipeline job manually for one time. If it builds successfully, then Jenkins registers it to poke on receiving hooks. And from the next git push, your pipeline job will trigger automatically.
Note: Also make sure that the pipeline job built manually for the first time should be built successfully, otherwise Jenkins will not poke it. If it fails to build, you can never trigger the job again.

Can a freestyle Jenkins job configured with only Deploy war to container plugin be parameterized + corresponding groovy syntax in jenkins pipeline

I tried/google to get equivalent groovy snippet which would do exact activity which "Deploy war to container" does for a Freestyle job.
Whatever I found in more than a week perm-comb are concluding to use SSH-Agent (which requires a private key generation on unix system, which I cannot do).While "Deploy war to container" takes only username and password and deploys war to remote Unix server.[This username and password is unix system user.]
Leaving this request I tried to create a parameterized Jenkins freestyle job which has only a post build action.In that post build action "Deploy war to container" section should accept $warFilename , $tomcatUrl like.
But this job is not working.
Jenkins job config image
I tried below two syntax ,First one is for calling parameterized job.
2nd One is for using username_pasword to execute scp.
stage ('deploy:tomcat') {
steps{
build job: 'TOMCAT_DEPLOYMENT', parameters: [string(name: 'WAR_FILES', value: "app_Pipeline/build/libs/Test.war"), string(name: 'TOMCAT_PATH', value: 'http://ip:port')]
}
}
stage ('deploy:tomcat') {
steps{
sshagent(['SSH_AGENT_cred']) {
sh 'scp /some-path/build/libs/Test.war admin#destinationIp:/apache-tomcat-8/webapps/'
}
}
}
1) Can we have a groovy syntax which can deploy war on remote server.(using only username and password , no private key related prerequisites ) ?
or
2) A jenkins freestyle job which can be generic enough to take war file path and tomcat details to deploy ?

Jenkins Pipeline as Code with Docker Error

For one of my projects that I have on GitHub, I wanted to build it as a docker image and push it to my docker hub. The project is a sbt one with a Scala codebase.
Here is how my JenkinsFile is defined:
#!groovy
node {
// set this in Jenkins server under Manage Jenkins > Credentials > System > Global Credentials
docker.withRegistry('https://hub.docker.com/', 'joesan-docker-hub-credentials') {
git credentialsId: '630bd271-01e7-48c3-bc5f-5df059c1abb8', url: 'https://github.com/joesan/monix-samples.git'
sh "git rev-parse HEAD > .git/commit-id"
def commit_id = readFile('.git/commit-id').trim()
println comit_id
stage "build" {
def app = docker.build "Monix-Sample"
}
stage "publish" {
app.push 'master'
app.push "${commit_id}"
}
}
}
When I tried to run this from my Jenkins server, I get the following error:
java.io.FileNotFoundException
at jenkins.plugins.git.GitSCMFile$3.invoke(GitSCMFile.java:167)
at jenkins.plugins.git.GitSCMFile$3.invoke(GitSCMFile.java:159)
at jenkins.plugins.git.GitSCMFileSystem$3.invoke(GitSCMFileSystem.java:161)
at org.jenkinsci.plugins.gitclient.AbstractGitAPIImpl.withRepository(AbstractGitAPIImpl.java:29)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.withRepository(CliGitAPIImpl.java:65)
at jenkins.plugins.git.GitSCMFileSystem.invoke(GitSCMFileSystem.java:157)
at jenkins.plugins.git.GitSCMFile.content(GitSCMFile.java:159)
at jenkins.scm.api.SCMFile.contentAsString(SCMFile.java:338)
at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:101)
at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:59)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:232)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:404)
Finished: FAILURE
Since this is running inside a VM on Azure, I thought the VM was not able to reach outside, but that seems not to be the case as I was able to ssh into the VM and git pull from the Git repo. So what is the problem here? How could I make this work?
for me unchecking "lightweight checkout" fixed the issue
I experienced the exact same error. My setting:
Pipeline build inside a dockerized Jenkins (version 2.32.3)
In the configuration of the job, I specified a check out into a subdirectory: Open the configuration, e.g. https://myJenkins/job/my-job/configure. At the bottom, see section Pipeline -> Additional Behaviours -> Check out into a sub-directory with Local subdirectory for repo set to, e.g., my-sub-dir.
Expectation: Upon check out, the Jenkinsfile ends up in my-sub-dir/Jenkinsfile.
Via the option Script path, you configure the location of the Jenkinsfile so that Jenkins can start the build. I put my-sub-dir/Jenkinsfile as value.
I then received the exception you pasted in your question. I fixed it by setting Script Path to Jenkinsfile. If you don't specify a sub-directory for check out, then still try double checking values for Script Path.
Note: I have another Jenkins instance at work. There I have to specify Script Path including the custom check out sub-directory (as mentioned in Expectation above).
GO TO Job-->Config-->Pipline and uncheck checkbox lightweight checkout"
lightweight checkout : selected, try to obtain the Pipeline script contents >directly from
the SCM without performing a full checkout. The advantage of this mode
is its efficiency; however, you will not get any changelogs or polling
based on the SCM. (If you use checkout scm during the build, this will
populate the changelog and initialize polling.) Also build parameters
will not be substituted into SCM configuration in this mode. Only
selected SCM plugins support this mode.

Pushing to GitLab repo does not trigger the Jenkins build

gitlab plugin Version: 1.4.2
jenkins Version: 2.7.4
gitlab Version: GitLab Community Edition 8.11.4
I have followed the plugin documentation and setup the webhook accordingly
(https://github.com/jenkinsci/gitlab-plugin).
Added gitlab repo to the jenkins job, the connection test succeeds.
Building the jenkins job manually also succeeds (The code is fetched
from the repo correctly so no issues there)
Added the webhook for jenkins. Testing the webhook also succeeds
(returns HTTP200). But on the jenkins side. nothing happens as a
result of the test even after it was performed after a change to the
repo (the jenkins log and gitlab plugin log show no activity)
When I try to test the whole setup. I make a new push to the gitlab
repo to see if it triggers a new build on jenkins. But nothing
happens. Can anybody help me out with this? I am not sure what is
wrong here as both the test hook and test gitlab connection show
success.
Thankyou in advance.
Naveed
In Jenkins you install and configure (global and job) Gitlab Hook Plugin
in your webhook can you make this :
URL : http://your-jenkins-server/gitlab/notify_commit or http://your-jenkins-server/gitlab/build_now.
Trigger : you check Push Events
and try again
To trigger a specific job the URL is:
http://your-jenkins-server/gitlab/build_now/job_name
job_name is the name of the job created in jenkins
I followed the instructions here and everything worked quite well: https://github.com/jenkinsci/gitlab-plugin/wiki/Setup-Example. It is possible to give back the results of the jenkins job to GitLab pipelines.
You can also push back the results using the jenkins pipeline:
node {
gitlabBuilds(builds: ['Build', 'Test', 'QA']) {
stage('Build') {
gitlabCommitStatus(name: 'Build') {
sh "your execution"
}
}
// The rest of the stages here...
}
}

How to start a Jenkins multibranch pipeline build from the command line (cli)?

Currently I can start a Jenkins job using the cli.
Example:
java -jar jenkins-cli.jar -s http://buildserver:8080 build Job_Name
I am playing around with the Jenkins multibranch pipeline feature and have not figured out how to start this type of job using the above command.
Any ideas how I can start a pipeline build via above cli?
On a multibranch pipeline, the job name is made of both the project name and the branch because a job is actually a build on one branch, you can see the global pipeline as just a container.
In the end, if your pipeline configuration is named your-project and you want to launch the job for the newfeature branch, you should do :
java -jar jenkins-cli.jar -s http://buildserver:8080 build your-project/newfeature
Also, the full project name is shown by Jenkins as shown above :

Resources