We are currently using Bamboo in our company but need Jenkins for a specific task the Bamboo machine is not capable of. Is it possible to set up a Jenkins build job and trigger it remotely out of Bamboo, so not everybody has to figure out the new interface?
You can run Jenkins jobs remotely, even jobs that require parameters, by means of the REST API, find more information in the documentation here.
You can add a Bamboo task in your job to run a shell script that actually consumes the Jenkins API in that case you might need curl to be installed (in case of using a python script you can use requests, etc)
Example: To just run a Jenkins job that does not requires any params:
curl -X POST --user USER:TOKEN JENKINS_URL/job/JOBNAME/build
Example: To run a Jenkins job that requires params:
curl -X POST JENKINS_URL/job/JOB_NAME/build \
--user USER:TOKEN \
--data-urlencode json='{"parameter": [{"name":"id", "value":"123"}, {"name":"verbosity", "value":"high"}]}'
Related
I need to use it in Jenkins pipeline without using the plugin.
Is there a way to fetch artifact from JFrog using API?
I have a Jenkins job where I have to draft a shell script which fetches the artifact from jfrog. If it was in Jenkins-file I would have used the plug-in. Is there a API way of doing it so that I can put that API in my shell script and run it from Jenkins pipeline?
You can use the AQL(Artifactory Query Language) to get the artifacts through api. Like this
curl -u<user>:<password> -X POST -k -H 'Content-Type:text/plain' -i https://<artifactory_host>/artifactory/api/search/aql
If you want to filter the result then you can add condtions to api by passing json
curl -u<user>:<password> -X POST -k -H 'Content-Type:text/plain' -i https://<artifactory_host>/artifactory/api/search/aql --data 'items.find({"name" : {"$match":"*.jar"}}).include("name")'
Our Jenkins + Bitbucket cloud integration already works and uses a multi-branch pipeline job to notify Bitbucket about the build status of a pull request.
Now I want to enhance it and add preview environments, for example at pr150.testing.company.com so that we can test a live production build before merging. I planned on using docker-compose to dynamically start/stop the preview environments.
Now, Jenkins needs to comment the Bitbucket pull request with the link to the preview environment. I know that the Bitbucket API supports creating pull request comments.
I imagine comments like this:
This example is taken from Jenkins-X
Does any Bitbucket plugin for Jenkins support automatically creating such comments?
Edit: To clarify, a plugin that automatically comments on the pull request would be enough. It's no problem to create the content of the comment on our end.
I couldn't find a plugin but you can execute shell commands as part of the jobs build to do it. Since Jenkins works off commits and not pull requests it's a bit of a faf. You need to get the pull request ID from the branch name using the API first. Using the REST 1.0 API you can do it this way.
BranchName=`echo ${GIT_BRANCH} | sed 's/origin\///'`
PullRequestID=`curl -s --request GET --url '{bitbucket_url}/rest/api/1.0/projects/{project_key}/repos/{repo_key}/pull-requests?State=OPEN&at=refs/heads/'${BranchName}'&direction=OUTGOING' --header 'Content-Type: application/json' -u username:password | sed -n 's/.*"values":\[{"id":\([0-9]*\).*/\1/p'`
echo '{"text": "Here's my comment with hyperlink"}' > comment.json
curl --request POST --url '{bitbucket_url}/rest/api/1.0/projects/{project_key}/repos/{repo_key}/pull-requests/'$PullRequestID'/comments' --header 'Content-Type: application/json' -u username:password -d #comment.json
rm comment.json
Notes:
The remote name might not be origin but something project specific. Check the console log to find it out
Will need changing for 2.0 API but concept should remain the same
Im Trying to do the following:
Job X (Jenkins Job) on server A running a Groovy script and the output of that script is a string.
My goal is to pass this string to job Y (Jenkins Job) on server B.
What is the best way to pass this string to a remote Jenkins Job?
Thanks
You can use any one of the two options:
Option 1: Remote Trigger Plugin
Option 2: Using API
curl -v -X POST jenkins_url/job/job_name/buildWithParameters
--data token=your_token
--user jenkins_user_email:jenkins_user_password
--data-urlencode json= '{"parameter1":"value1", "parameter2":"value2"}'
I have a requirement where i need to trigger the build jobs remotely using curl command. I am unable to pass the branch/tag name as a parameter to trigger the build.
I used the below command :
& $CURLEXE -k -X POST $dst_job_url --user username:token --data-urlencode json='{"parameters": [{"name":"branch","branch":"branches"}]}'
If i run the above command it was triggers the build for the trunk ( default ).
You omitted the URL, so it's hard to be certain. Jenkins has two urls for building: "build" and "buildWithParameters". If you're not using buildWithParameters, switching to it will probably help.
See:
How to trigger Jenkins builds remotely and to pass parameters
I have a jenkins Job that do two steps.
1) Get source code from GIT
2) Run the source code analisys using SONAR-RUNNER
The Job works fine and I'd like to copy/create a similar Job.
I tried to create a new Job using the command: curl -s --data-binary #config.xml -H "Content-Type: application/xml" -X POST http://localhost:8080/createItem?name=SONAR-NEW
It does create a new Job, however the SONAR-RUNNER part is empty and if I ran the build it doesn't make the SONAR part. The funny thing is that if I check the config.xml generated the SONAR-RUNNER configuration part is there.
The same thing happens if I try to copy the JOB, I tried it using:
1) jenkins-cli, or
2) curl --data "name=NEWJOBNAME&mode=copy&from=SONAR" -X POST http://localhost:8080/createItem
If I have a different build step, for example a "Execute SHell", I am able to copy/create the JOB.
So my question is how can I create or copy a JOB that as a build step using "Execute SonarQube Scanner"