How can I retrigger the same jenkins build again - jenkins

I am connected between jenkins and gerrit successfully. The gerrit trigger is on every new change. Sometimes the build is not started (no matter what is the reason).
How can I start the exact same build by passing a variable from gerrit?
I want to clone exactly the same changes as they are pushed to gerrit.
For now I can start the build with this command:
curl -u user:password jenkins_url/job/job_name/build?token
Is there a way to start this build with gerrit param?
For example
curl -u user:password jenkins_url/job/job_name/build?token param=change-id

Yes, you can trigger a build passing a parameter with the following command:
curl -u USER:PASS "https://JENKINS-SERVER/JOB-PATHNAME/buildWithParameters?token=TOKEN&PARAM-NAME-1=PARAM-VALUE-1&PARAM-NAME2=PARAM-VALUE-2"

Related

Jenkins CLI - How to build project ignore trigger remote job

Here is my Jenkins trigger setting
and param
And I use the following command to build the project and got success.
$ java -jar jenkins-cli.jar -s http://localhost:8080 -auth USERNAMR:PASSWOED build PROJECT_NAME -p branchName=master -p buildEnv=UAT
If build success it will trigger another Jenkins to build next platform, I just want to build single one don't tigger another platform build, can do it using CLI command only?

How to fetch war file from Jfrog artifactory inside dockerfile ? getting HTTP 401 error

I have created a declarative jenkins pipeline and one of it's stages is as follows:
stage('Docker Image'){
steps{
bat 'docker build -t HMT/demo-application:%BUILD_NUMBER% --no-cache -f Dockerfile .'
}
}
This is the docker file:
FROM tomcat:alpine
RUN wget -O /usr/local/tomcat/webapps/launchstation04.war http://localhost:8082/artifactory/demoArtifactory/com/demo/0.0.1-SNAPSHOT/demo-0.0.1-SNAPSHOT.war
EXPOSE 9100
CMD /usr/local/tomcat/bin/cataline.bat run
I am getting the below error.:
[91m/bin/sh:
01:33:28 [0mThe command '/bin/sh -c wget -O /usr/local/tomcat/webapps/launchstation04.war http://localhost:8082/artifactory/demoArtifactory/com/demo/0.0.1-SNAPSHOT/demo-0.0.1-SNAPSHOT.war' returned a non-zero code: 127
UPDATE:
I have updated the command to
RUN wget -O /usr/local/tomcat/webapps/launchstation04.war -U jenkinsuser:Learning#% http://localhost:8082/artifactory/demoArtifactory/com/demo/0.0.1-SNAPSHOT/demo-0.0.1-20200823.053346-18.war
There is no problem in my command.Jfrog artifactory was unable to authorize this action.So I added username and password details but it still didn't work.
Error:
wget: server returned error: HTTP/1.1 401 Unauthorized
It didnt work after modifiying the password policy to unsupported.But it worked when I allowed anonymous access.
How to provide access using credentials.
Need more clarification on your question. Not sure where you are using curl command.
Image tomcat:alpine doesn't contains curl command. Unless you install it manually.
bash-4.4# type curl
bash: type: curl: not found
bash-4.4#
If your ask is regarding the sh -c option, if the script is invoked through CMD option, yes it will use sh. Instead you can give a try with ENTRYPOINT.
You can provide username & password via command line:
wget --user user --password pass
Using curl :
curl -u username:password -O
But void using special characters:
Change your password to another once in: [a-z][A-Z][0-9]
Try an API Key instead of password, I have a feeling that "#" may be throwing you off. Quotes can help there too or separating the password with -p
Also look at the request logs for whether the entry comes as 401 for the user, or anonymous/unauthenticated
Lastly, see if you can cURL from outside the image and then ADD the file in, as that will remove any external factors that may vary from the host (where I assume the command works)

Trigger parameterized builds through api calls

I am trying to trigger the Jenkins builds through API calls everything is working without parameters.
If i am trying with parameters job is not getting triggered.
curl -I -u tmp:apitoken http://jenkinurl:8080/job/test02/build/buildWithParameters?token=rahul&branchName=rahul
Used wrong syntax.
curl -I -u tmp:apitoken "http://jenkinurl:8080/job/test02/buildWithParameters?token=rahul&branchName=rahul"
If in the case of Multiple parameters
curl -I -u tmp:apitoken http://jenkinurl:8080/job/test02/build/buildWithParameters?token=rahul&param1=value&param2=value

How can I run a Curl command from Jenkins A in order to trigger a build in Jenkins B

I want to trigger a build in Jenkins B by running build in Jenkins A, I know that I can use a Curl command from Jenkins A but actually I couldn't figure it how and where to write the command, inside the pipeline or as power-shell script?
Here is described how it works.
You need to download the jenkins-cli.jar from the jenkins server you want to trigger a job on.
wget http://YOUR_JENKINS_HOSTNAME/jnlpJars/jenkins-cli.jar
You have to verify that you can authenticate to the jenkins server.
ssh-keygen -t rsa && cat ~/.ssh/id_rsa.pub
Copy the public key to http://YOUR_JENKINS_HOSTNAME/user/YOUR_USERNAME/configure. On the same page you can generate a API Token. You will need it for the remote call. You can save it as file with the following command:
echo 'YOUR_USERNAME:YOUR_API_TOKEN' > jenkins_secret
Now you can call the remote jenkins server and trigger builds
java -jar ./jenkins-cli.jar -s http://YOUR_JENKINS_HOSTNAME list-jobs
To integrate it to your pipeline just add a sh step and take the last line (the jenkins-cli.jar call) and add it to your pipeline.

jenkins Job Status via Curl

I need to get job build status failure or success via curl command.
I tried this :
curl --silent http://user:TokenID#Jenkins-BuildURL/job/job_number/api/json | jq -r '.result'
Unable to execute the curl.
Try below Command :
FYI , you are missing JOB_NAME in your curl command
curl --silent http://user:TokenID#Jenkins-BuildURL/job/${JOB_NAME}/${BUILD_NUMBER}/api/json
Note : JOB_NAME,BUILD_NUMBER are jenkins Environment variables , when executed from jenkins job it will pick latest job details
and you can always pass your credentials using '-u' option :
Example :
curl --silent -u username:user_pwd http://Jenkins-BuildURL/job/${JOB_NAME}/${BUILD_NUMBER}/api/json
And simple trick would be first check in browser if the Url is valid or not , if it valid half of the problem is eliminated , then we can focus on curl command

Resources