Trigger jenkins job with parameters from shell - jenkins

I am trying to trigger a trigger jenkins job from shell but as of now no sucess.I tried both these methods
curl -u ceadmin:ceadmin -X POST http://abc-lnx:8080/job/ci_demo/build --data token=ci_build -data-urlencode json='{"parameter": [{"name":"Branch", "value":"master"}, {"name":"verbosity", "value":"high"}]}'
curl -X POST http://abc-lnx:8080/job/ci_demo/buildWithParameters?token=ci_build&Branch=master
I have defined ce_admin as token in my job. also ce_admin is a admin user in Jenkins.Anonymous user do not have any permission other then read on jobs and views.
What am i missing?

This works for me:
#parameters:
username=user
password=password
jenkins_url=example.com/build
some_token=hi
job_name=dostuff
curl -u $username:$password -X POST https://$jenkins_url/job/$job_name/build\?token\=$some_token
So this should work for your example (I escaped "?", and "&"):
curl -u ceadmin:ceadmin -X POST http://abc-lnx:8080/job/ci_demo/buildWithParameters\?token=ci_build\&Branch=master

Related

Masking Basic Auth header when using CURL to trigger remote jenkins job

I want to trigger remote Jenkins job from my container(k8s).
Currently, I'm using:
curl -k -X POST -u $USER:$JENKINS_TOKEN "${JENKINS_URL}/job/{$JOB_NAME}/buildWithParameters?token=12345"
But this information($USER,$JENKINS_TOKEN) is displayed in ArgoUI, is there any secure/other way to save credentials for remote trigger?
You can try one of the following.
Save the password in a file called password-file and read from that
curl -k -X POST -u $USER:$(cat .password-file)"${JENKINS_URL}/job/{$JOB_NAME}/buildWithParameters?token=12345"
Accept credentials from the STDIN.
curl -k -X POST "${JENKINS_URL}/job/{$JOB_NAME}/buildWithParameters?token=12345" -K- <<< "--user $USER:$JENKINS_TOKEN"
You can also try using --netrc-file option with curl where you can store the username and password in a file itself.
file
machine JENKINS_HOST login USERNAME password PASSWORD
Curl Command
curl -k -X POST --netrc-file my-password-file "${JENKINS_URL}/job/{$JOB_NAME}/buildWithParameters?token=12345"

Decline Pull request in Bitbucket when Building fail in Jenkins

I have triggered a build in Jenkins after creating a new pull request in bitbucket.
I want to automatically decline the pull request right away when building fail in Jenkins.
How do I do it and how can I set up it.
Is it possible to use a shell script in your case?
Something like this:
#!/usr/bin/env bash
if test $1 -ne 0; then
curl https://api.bitbucket.org//2.0/repositories/{workspace}/{repo_slug}/pullrequests/{pull_request_id}/decline \
-u $3:$4 \
--request POST \
--header 'Content-Type: application/json' \
--data '{}'
fi
exit $1
Then, you can call the script as:
chmod +x decline-pull-request.sh
decline-pull-request.sh $(status) $(pullRequestId) $(bitbucketUsername) $(bitbucketPassword)

Trigger Jenkins build via curl with additional parameter

I'm trying to trigger the Jenkins build via curl command with additional parameter as described in the following image:
I'm using --data-urlencode json='{"parameter": [{"name":"text", "value":"develop:test"}]}' but it doesn't work.
curl -X POST --user "${userName}:${password}" -H "Jenkins-Crumb:33c13aed5548ad43bf5e2eb276cf21af" "JENKINS_URL/job/JOB_NAME/buildWithParameters" --data-urlencode json='{"parameter": [{"name":"text", "value":"develop:test"}]}'
JenKins still build with the default parameter instead:
Any solution? Thanks.

Docker Hub Remtoe Build Triggers

https://docs.docker.com/docker-hub/builds/#remote-build-triggers
Docker hub now has a build system in place. One of the ways to trigger a container to be built is using Remote build triggers. COmmands such as the following:
$ curl --data build=true -X POST https://registry.hub.docker.com/u/svendowideit/testhook/trigger/be579c82-7c0e-11e4-81c4-0242ac110020/
Their website shows a few paramters that can be passed in. But does not explain their meaning, nor do they provide a list all possible parameters.
What are all the possible parameters and what are their meanings?
it works for branches for sure, not sure about tags:
curl -H "Content-Type: application/json" \
--data '{"source_type": "Branch", "source_name": "develop"}' \
-X POST "$DOCKERHUB_TRIGGER";
Try source_type = Tag

How to get lines of code within post trigger using Xcode Server?

My current shell script is following:
#!/bin/sh
LINES_OF_CODE=10792
curl -i -X POST -H "Content-Type:application/json" http://a:bc#lb.mycompany.org/api/public/metrics/ -d '{"project_public_id":"myprojectid", "type":"loc", "value":'$LINES_OF_CODE', "platform":"ios"}'
but it is hardcoded... is there any way to count number of lines dynamically?

Resources