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
I am currently using Gerrit server side hook script to trigger Jenkins job to run whenever there is a new patch set submitted to Gerrit server. I do not use Jenkins Gerrit event plugin because I do not want to have any Gerrit missed event happen.
However, I face some difficulties on how to pass the rating back to Gerrit server if the Jenkins job run successfully or fail. How can I achieve this result? Is there anyone trying to achieve the same thing as me? Please advise.
Thanks in advance
You can make a script to vote on Gerrit using the REST API. For example, the following command will set Code-Review=1:
curl --request POST --user USER:PASS --data #- --header Content-Type:application/json https://GERRIT-SERVER/a/changes/CHANGE-NUMBER/revisions/PATCHSET-NUMBER/review <<EOF
{
"message": "So far so good",
"labels": {
"Code-Review": 1,
}
}
EOF
See more info about the REST API here.
Subject is self-explanatory. I'd like to add post-build actions for many Jenkins jobs, instead of configuring one by one. I added Configuration Slicing plugin but if I'm not mistaken it doesn't modify post-build actions.
Any ideas?
Thanks in advance
If Configuration Slicing Plugin doesn't satisfy your requirments, then you should fall back to SED.
From Jenkins Issues:
You will need to write a script that will loop through your Jenkins
jobs and SED the value with a new one then use POST
https://support.cloudbees.com/hc/en-us/articles/218353308-How-to-update-job-config-files-using-the-REST-API-and-cURL
Get current config
curl -X GET
http://developer:developer#localhost:8080/job/test/config.xml -o
mylocalconfig.xml
Post updated config
curl -X POST http://developer:developer#localhost:8080/job/test/config.xml
--data-binary "#mymodifiedlocalconfig.xml"
For the Pos-build actions is the markups between <publishers>...</publishers> in the config.xml
I've to perform an HTTP post of an artifact (Jenkins build) to an Internet website. Nevertheless my Company has got a proxy in front of Jenkins.
Does anybody know how to authenticate against an http proxy during a Jenkins task ?
Thanks and Regards
Just add the following lines in execute shell field of Post-build Actions :-
curl -x <proxy_host:proxy_port> -X POST <url_of_website> --data-binary <your_artifact_location>
I am invoking a Jenkins job remotely using:
wget http://<ServerIP>:8080/job/Test-Jenkins/build?token=DOIT
Here Test-Jenkins job is invoked and DOIT is the security token that I have used.
Now I need to pass some parameters to the build.xml file of this job i.e. Test-Jenkins.
I have not yet figured out how to pass the variables yet.
See Jenkins documentation: Parameterized Build
Below is the line you are interested in:
http://server/job/myjob/buildWithParameters?token=TOKEN&PARAMETER=Value
In your Jenkins job configuration, tick the box named "This build is parameterized", click the "Add Parameter" button and select the "String Parameter" drop down value.
Now define your parameter - example:
Now you can use your parameter in your job / build pipeline, example:
Next to trigger the build with own/custom parameter, invoke the following URL (using either POST or GET):
http://JENKINS_SERVER_ADDRESS/job/YOUR_JOB_NAME/buildWithParameters?myparam=myparam_value
To add to this question, I found out that you don't have to use the /buildWithParameters endpoint.
In my scenario, I have a script that triggers Jenkins to run tests after a deployment. Some of these tests require extra info about the deployment to work correctly.
If I tried to use /buildWithParameters on a job that does not expect parameters, the job would not run. I don't want to go in and edit every job to require fake parameters just to get the jobs to run.
Instead, I found you can pass parameters like this:
curl -X POST --data-urlencode "token=${TOKEN}" --data-urlencode json='{"parameter": [{"name": "myParam", "value": "TEST"}]}' https://jenkins.corp/job/$JENKINS_JOB/build
With this json=... it will pass the param myParam with value TEST to the job whenever the call is made. However, the Jenkins job will still run even if it is not expecting the parameter myParam.
The only scenario this does not cover is if the job has a parameter that is NOT passed in the json. Even if the job has a default value set for the parameter, it will fail to run the job. In this scenario you will run into the following error message / stack trace when you call /build:
java.lang.IllegalArgumentException: No such parameter definition: myParam
I realize that this answer is several years late, but I hope this may be useful info for someone else!
Note: I am using Jenkins v2.163
You can simply try it with a jenkinsfile. Create a Jenkins job with following pipeline script.
pipeline {
agent any
parameters {
booleanParam(defaultValue: true, description: '', name: 'userFlag')
}
stages {
stage('Trigger') {
steps {
script {
println("triggering the pipeline from a rest call...")
}
}
}
stage("foo") {
steps {
echo "flag: ${params.userFlag}"
}
}
}
}
Build the job once manually to get it configured & just create a http POST request to the Jenkins job as follows.
The format is http://server/job/myjob/buildWithParameters?PARAMETER=Value
curl http://admin:test123#localhost:30637/job/apd-test/buildWithParameters?userFlag=false --request POST
To pass/use the variables, first create parameters in the configure section of Jenkins. Parameters that you use can be of type text, String, file, etc.
After creating them, use the variable reference in the fields you want to.
For example: I have configured/created two variables for Email-subject and Email-recipentList, and I have used their reference in the EMail-ext plugin (attached screenshot).
When we have to send multiple trigger parameters to jenkins job, the following commands works.
curl -X POST -i -u "auto_user":"xxxauthentication_tokenxxx" "JENKINS_URL/view/tests/job/helloworld/buildWithParameters?param1=162¶m2=store"
You can trigger Jenkins builds remotely and to pass parameters by using the following query.
JENKINS_URL/job/job-name/buildWithParameters?token=TOKEN_NAME¶m_name1=value¶m_name1=value
JENKINS_URL (can be) = https://<your domain name or server address>
TOKE_NAME can be created using configure tab
curl -X POST -u Admin:<api_token> http://localhost:8080/job/<job_name>/buildWithParameters\?ColorValue\=sddddsd
curl -H "Jenkins-Crumb: <your_crumb_data>" -u "<username>:<password>" "http://<your_jenkins_url>?buildWithParameters?token=<your_remote_api_name>?<parameterA>=<val_parameter_A>&<parameterB>=<val_parameterB>"
You can change following parameters as you want:
<your_crumb_data>
<username>
<password>
<your_jenkins_url>
<your_remote_api_name>
<parameterA>
<parameterB>
<val_parameter_A>
<val_parameter_B>
Note: Placing double quotes may be critical. Pay attention, please.
2023 Update
It has now changed how Jenkins Authenticates.
You need to pass 2 tokens to execute your job remotely.
You need:
apiToken to authenticate your identity. This value is created from JENKINS_URL/me/configure . Also check here for documentation
Another Job authentication token which you create when you enable 'Trigger builds remotely'.
Below is a sample you can tweak to get it done.
PARAM1_VALUE=<param1_value>
PARAM2_VALUE=<param2_vale>
USERNAME=dummy_user_name
JENKINS_URL="http://10.xxx.x.xxx:8080"
JOB_TOKEN="<value>" # you create this token when you enable Job>Configure>Build Triggers>Trigger builds remotely
LOGIN_API_TOKEN="<value>" #get this value from JENKINS_URL/me/configure
curl -L --user $USERNAME:$LOGIN_API_TOKEN "$JENKINS_URL/job/JobName/buildWithParameters?token=$JOB_TOKEN¶m1_name=$PARAM1_VALUE¶m2_name=$PARAM2_VALUE"