How catch curl response into variable in Jenkinsfile - jenkins

I want to curl an URL and capture the response into a variable.
when I curl a command and echo its output I get the correct response as below
sh 'output=`curl https://some-host/some-service/getApi?apikey=someKey`;echo $output;'
I want to catch the same response into a variable and use that response for further operation
Below is my Jenkinsfile
pipeline {
agent {
label "build_2"
}
stages {
stage('Build') {
steps {
checkout scm
sh 'npm install'
}
}
stage('Build-Image') {
steps {
echo '..........................Building Image..........................'
//In below line I am getting Output
//sh 'output=`curl https://some-host/some-service/getApi?apikey=someKey`;echo $output;'
script {
//I want to get the same response here
def response = sh 'curl https://some-host/some-service/getApi?apikey=someKey'
echo '=========================Response===================' + response
}
}
}
}
}
Can you please tell me what changes I need to do in my Jenkinsfile

If you want to return an output from sh step and capture it in the variable you have to change:
def response = sh 'curl https://some-host/some-service/getApi?apikey=someKey'
to:
def response = sh(script: 'curl https://some-host/some-service/getApi?apikey=someKey', returnStdout: true)
Reference: https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#sh-shell-script

Related

How to get a json list from the curl in jenkins pipeline groovy script

Using curl in jenkins pipeline script getting below json file , after that How to get the list values of dependencies list and trigger those jobs?
{
"Dependencies": [
"Dep1",
"Dep2",
"Dep3"
]
}
My current program is looking like this which is not working. And after getting values I need to form the Jenkins jobs and trigger from pipeline
pipeline {
agent {
label 'Dep-Demo'
}
stages{
stage('Getting Dependecies jason-object'){
steps {
script {
final String url = "https://***************/repository/files/sample.jason/raw?ref=main"
withCredentials([usernamePassword(credentialsId: 'api', passwordVariable: 'PASSWORD', usernameVariable: 'USELESS')]) {
sh ''' echo $PASSWORD '''
def response = sh(script: "curl -s --header \"PRIVATE-TOKEN: $PASSWORD\" $url ", returnStdout:true)
echo "***** $response"
def jsonObj = readJSON text: response.join(" ")
echo jsonObj
Check the following example.
script {
def jString = '''
{
"Dependencies": [
"Dep1",
"Dep2",
"Dep3"
]
}
'''
def jsonObj = readJSON text: jString
jsonObj.Dependencies.each { dep ->
echo "Building ${dep}"
//Building the Job
build job: dep
}
}

Jenkinsfile groovy unable to use arguments in method

How do I call a method with arguments from a Jenkinsfile.
def upload_nightly_build(local_filename, remote_filename)
{
sh 'curl --output $local_filename http://someserver:8000/firmware/$remote_filename'
sh 'curl -F upload_file=#$local_filename http://someserver:8000/frontend/file_upload_handler'
sh 'rm $local_filename'
}
pipeline
{
agent
{
dockerfile
{
dir 'dockerfiles'
filename 'Dockerfile-integration.tests'
}
}
stages
{
stage('upload binaries')
{
steps
{
dir ("firmware")
{
upload_nightly_build('iobox-1024-nightly.bin', 'iobox-1024.bin')
}
}
}
}
}
Tried so far
encapsulate usage of arguments in method with braces
define arguments as String
define arguments with def
used named arguments using Map
Whatever I try, in Jenkins console output I will always see
+ curl --output http://someserver:8000/firmware/
curl: no URL specified!
Sigh... found it myself eventually..
sh "curl --output $local_filename http://someserver:8000/firmware/$remote_filename"
Double quote the sh argument... I should've known...

Jenkinsfile post always directive with multiple steps

I am wondering if it's possible to use various steps block on inside a post step.
Here's the actual code:
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'bash testing.sh'
}
}
}
post {
always {
steps {
sh 'bash cleaning-procedure-1.sh'
}
steps {
sh 'bash cleaning-procedure-2.sh'
}
steps {
sh 'bash general-cleaning.sh'
}
}
}
}
One of the errors that Jenkins gets:
WorkflowScript: 291: Missing required parameter: "delegate" # line 291, column 13.
step {
Is it possible to create different steps inside a POST - ALWAYS block on Jenkins?
steps blocks are not allowed inside a post directive. If you want to use the sh method, then you can invoke it directly outside of the steps scope:
post {
always {
sh 'bash cleaning-procedure-1.sh'
sh 'bash cleaning-procedure-2.sh'
sh 'bash general-cleaning.sh'
}
}
You can use a script block to have multiple actions, like
post {
always {
script {
junit '**/build/junit.xml'
xunit (tools: [CTest(pattern: '**/build/ctest/**/*.xml')] ...)
}
}
}

Return value of .exe using bat script in Jenkins

I am rather new to Jenkins. I am trying to run a UnitTest.exe and to check the value of the return code of it. In the JenkinsFile:
stage('Unit Tests')
{
steps{
script{
def statusCode = bat script: "UnitTest.exe", returnStatus:true
echo statusCode
}
}
}
I am able to see the output of the program (all the std::cout), but I am unable to print out the return value statusCode
as #zett42 said,
instead of using echo statusCode using
echo "$statusCode"
instead should do the trick

Jenkins pipeline to SSH into an instance and call a function

How to create a function def test() which does some steps after sshing into an instance
I have something like this:
#!/usr/bin/env groovy
def test() {
cd $testPath
mv test*.txt archiveFiles
sh "someScript.sh"
}
pipeline {
agent java
parameters {
string(
name: 'testPath',
defaultValue: '/home/ubuntu/testFiles',
description: 'file directory'
)
}
stages {
stage(test) {
steps{
script{
sh "ssh ubuntu#IP 'test()'"
}
}
}
}
}
I am trying to ssh into an instance and do the steps in the function test() by calling it
I am getting an error like this:
bash: -c: line 1: syntax error: unexpected end of file
ERROR: script returned exit code 1
We use the SSH plugin as follows:
steps {
timeout(time: 2, unit: 'MINUTES') {
sshagent(credentials: ['local-dev-ssh']) {
sh "ssh -p 8022 -l app ${ENVIRONMENT_HOST_NAME} './run-apps.sh ${SERVICE_NAME} ${DOCKER_IMAGE_TAG_PREFIX}-${env.BUILD_NUMBER}'"
}
}
}

Resources