jenkins Job Status via Curl - jenkins

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

Related

how to trigger and monitor Jenkins job from CI and getting results after is finished

I am trying to implement a script in gitlab CI to trigger a smoke test via Jenkins and then get the results.
So far I am able to trigger the job successfully and I am trying to follow this to implement a monitoring stage and then get the result once the job finishes.
my issue is that I implemented a while loop to monitor if the Jenkins job has finished so far the script is giving either syntax errors (when copied in gitlab) or if run in the terminal I get:
job is building? true
waiting...
parse error: Invalid numeric literal at line 2, column 0
job is still building?
job is building?
This is what I am using so far:
#!/bin/bash
running="true"
while [ "$running" != "false" ]
do
echo "job is building? ${running}"
echo "waiting...";
sleep 2;
running=$(curl -s --user ${EMAIL}:${TOKEN} ${URL}/${var}/lastBuild/api/json | jq .'building')
echo "job is still building? ${running}"
done
echo "Done!"
buildNumber=$(curl -s --user $EMAIL:$TOKEN ${URL}/$ENV-${var}/lastBuild/api/json | jq ".url" | awk -F "/" '{print $(NF-1)}')
echo "getting results for build ${buildNumber}"
curl -s --user ${EMAIL}:${TOKEN} ${URL}/${ENV}-${var}/lastBuild/api/json | jq ".url" | awk -F "/" '{print $(NF-1)}'
curl -v --silent --user ${EMAIL}:${TOKEN} ${URL}/${ENV}-${var}/lastBuild/consoleText 2>&1 | grep -i "finished:"
UPDATE
the script is running now ok in my local terminal
the change was
running="true"
while [ "$running" != "false" ]
do
echo "job is building? ${running}"
echo "waiting..."
sleep 2
curl -s --user $EMAIL:$TOKEN $URL/$ENV-${var}/lastBuild/api/json --output now.txt
running=$(jq .'building' now.txt)
echo "job is still building? ${running}"
done
The problem is still on Gitlab CI as after copy pasting this script I get in the pipeline
/bin/sh: eval: line 149: syntax error: unexpected "done"
I'm guessing that the error
parse error: Invalid numeric literal at line 2, column 0
comes from the line that does the curl
running=$(curl -s --user ${EMAIL}:${TOKEN} ${URL}/${var}/lastBuild/api/json | jq .'building')`
I'm assuming that the error is thrown by jq when it tries to parse the json and fails. Since this line has an error, the variable running never gets properly updated, meaning that the rest of the script doesn't work as intended.
If you fix this line, the rest of your pipeline should work. Consider looking into this question which has a similar problem and some solutions.

Multiple health check curls in docker health check

In order to ensure the health check of my container, I need to perform test calls to multiple URLS.
curl -f http://example.com and curl -f http://example2.com
Is it possible to perform multiple curl calls for a docker health check?
You can set a script as the healthcheck command that contains a more complex logic to perform the healthcheck. That way you can do multiple requests via curl and let the script only return positive if all requests succeeded.
# example dockerfile
COPY files/healthcheck.sh /healthcheck.sh
RUN chmod +x /healthcheck.sh
HEALTHCHECK --interval=60s --timeout=10s --start-period=10s \
CMD /healthcheck.sh
Although I cannot test, I think you can use the following
HEALTHCHECK CMD (curl --fail http://example.com && curl --fail http://example2.com) || exit 1
If you want first to check this command manually (without exit part), you can check the last error code by
echo $? -> in linux
and
echo %errorlevel% -> in windows

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 retrigger the same jenkins build again

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"

Resources