Shell cmds in Jenkins Pipeline doesnt execute - shell

I want to execute a cURL command, extract Json variable(using jq) & save in variable in Jenkins Pipeline. In Freestyle project under Build when I select Execute Shell and give below commands & I am getting valid Success output with all the values.
,
deployment_info=$(curl -H "Authorization: Basic a123=" "https://api.sample.com/v1")
rev_num=$(jq -r .environment[0].revision[0].name <<< "${deployment_info}" )
env_name=$(jq -r .environment[0].name <<< "${deployment_info}" )
api_name=$(jq -r .name <<< "${deployment_info}" )
org_name=$(jq -r .organization <<< "${deployment_info}" )
declare -r num1=1
pre_rev=$(expr "$rev_num" - "$num1")
echo $rev_num
echo $api_name
echo $org_name
echo $env_name
echo $pre_rev
Now I want to execute the same set of commands in a Pipeline. So this is my Pipeline,
def deployment_info
def rev_num
def env_name
def org_name
def api_name
def pre_rev
def num1
node {
stage('Integration Tests') {
sh "deployment_info=\$(curl --header 'Authorization: Basic abc123=' 'https://api.sample.com/v1')"
sh "rev_num=\$(jq -r .environment[0].revision[0].name <<< \"${deployment_info}\")"
sh "env_name=\$(jq -r .environment[0].name <<< \"${deployment_info}\" ) "
sh "api_name=\$(jq -r .name <<< \"${deployment_info}\" ) "
sh "org_name=\$(jq -r .organization <<< \"${deployment_info}\" )"
sh "declare -r num1=1"
sh "pre_rev=\$(expr \"$rev_num\" - \"$num1\")"
sh "echo $rev_num"
sh "echo $api_name"
sh "echo $org_name"
sh "echo $env_name"
sh "echo $pre_rev"
}
}
The cURL cmd is getting executed & a valid JSON response is seen in console, but after that, I am getting this error,
[Pipeline] sh
[curlpip] Running shell script
++ jq -r '.environment[0].revision[0].name'
+ rev_num=null
[Pipeline] sh
[curlpip] Running shell script
++ jq -r '.environment[0].name'
+ env_name=null
[Pipeline] sh
[curlpip] Running shell script
++ jq -r .name
+ api_name=null
[Pipeline] sh
[curlpip] Running shell script
++ jq -r .organization
+ org_name=null
[Pipeline] sh
[curlpip] Running shell script
+ declare -r num1=1
[Pipeline] sh
[curlpip] Running shell script
++ expr null - null
expr: non-integer argument
+ pre_rev=
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 2
Finished: FAILURE
Any help is appreciated.

Related

Jenkins shell script returned exit code 1 when the searched string isn't exsist

I'm tring the following at Jenkins in aim to search strings of failures in jobs.
This will run on daily basis.
def sd = "2020" + "${env.START_DATE}" + "0000"
def ed = "2020" + "${env.END_DATE}" + "2359.59"
pipeline {
agent {label "master"}
stages {
stage('Build') {
steps {
print(sd)
sh 'echo "Hello World"'
sh """
pwd
#rm end-time start-time
#rm $WORKSPACE/$PARSING_OUTPUT
touch -t $sd $WORKSPACE/start-time
touch -t $ed $WORKSPACE/end-time
find /var/lib/jenkins/jobs/. -type f -newer $WORKSPACE/start-time ! -newer $WORKSPACE/end-time -name '*' -exec grep $SEARCH_STRING /dev/null {} + >> $WORKSPACE/$PARSING_OUTPUT
ls -ltr
"""
}
post {
always {
echo "sending mail"
//mail to: 'e#s.com',
//subject: "Parse Jenkins log",
//body: "TBD"
//body: "${env.BUILD_URL} has result ${currentBuild.result}"
}
}
}
}
}
My problem is that if the string I'm looking for isn't exsist. the job fail..
Console output display ERROR: script returned exit code 1.
I tried adding #!/bin/sh that will allow me to execute with no option - didn't help.
any suggestions ?
There are multiple way to achive above
use set +e # Disable exit on non-zero
sh ''''
set +e
..
'''
use OR || with cmd
sh''''
$CMD || echo "string doesn't exist."
'''
use like below
sh (
script: 'YOUR SCRIPT',
returnStatus: true
)

How to Run "docker exec -it <container_name > bash" using Gradle Build?

I want to run " docker exec -it bash -c './tmp/script.sh' " this command using gradle from build.gradle. But this command does not work though "docker ps -a" command works fine
I tried in different way, but no luck
1) "docker-compose exec resin_container bash -c './resin/bin/resin-shutdown.sh'".execute().waitFor()
2) CommandLine "docker-compose exec resin_container bash -c './resin/bin/resin-shutdown.sh'"
3) exec{
executable = 'sh'
args "docker-compose exec resin_container bash -c './resin/bin/resin-shutdown.sh'"
}
Full code on build.gradle are as bellow
task dockerDeploy(dependsOn: 'tarArtifact') {
group = "dev"
description = 'Create exploded-war, tar it and deploy to docker'
doLast {
if (devEnvironment) {
if ("docker inspect -f '{{.State.Running}}' resin_container".execute().text.contains('true')) {
println "SHUTTING DOWN Resin Service"
"docker-compose ps".execute().waitFor()
//"docker-compose exec resin_container bash -c './resin/bin/resin-shutdown.sh'".execute().waitFor()
def resin_shutdown = "docker exec -it resin_container bash -c \"./resin/bin/resin-shutdown.sh\""
println "${resin_shutdown}"
exec{
//commandLine = 'docker ps'
commandLine = resin_shutdown
}
println "RESTARTING resin_container"
"docker-compose restart resin_container".execute().waitFor()
} else {
println "STARTING services"
"docker-compose up -d".execute()
}
}
}
}
Expected result like bellow . In here I run the command from shell terminal, but I want to run the same command using gradle build
uzzal$ docker exec -it resin_container bash -c "./resin/bin/resin-
shutdown.sh"
+ GREEN='\033[1;32m'
+ NC='\033[0m'
+ echo 'NODE_NAME : '
NODE_NAME :
+ pushd .
+ echo 'Waiting for server to stop'
Waiting for server to stop
+ sleep 3
+ pgrep -fl -U 999 resin.jar
+ awk '{ print $1 }'
+ xargs kill -SIGTERM
+ sleep 3
+ echo -e '\033[1;32m DONE SHUTTING DOWN \033[0m '
DONE SHUTTING DOWN
+ popd
uzzal$
But Output comes as
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':app:shutResin'.
A problem occurred starting process 'command 'docker exec -it resin_container bash -c "./resin/bin/resin-shutdown.sh"''
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

Issues while create a file in shell scripting in jenkins pipeline script

I am trying to create a multi line file in Jenkins pipeline script using the below commands.
sh "echo \"line 1\" >> greetings.txt"
sh "echo \"line 2\" >> greetings.txt"
echo "The contents of the file are"
sh 'cat greetings.text'
sh 'rm -rf greetings.txt'
Unforunately , I am not able to create the file named greetings.txt. Can any one please let me know, where I am going wrong.
Results in Jenkins console:
[tagging] Running shell script
+ echo 'line 1'
[Pipeline] sh
[tagging] Running shell script
+ echo 'line 2'
[Pipeline] echo
The contents of the file are
[Pipeline] sh
[tagging] Running shell script
+ cat greetings.text
cat: greetings.text: No such file or directory
Any suggestions would be helpful.
Thanks!
This can be solve this by using single quotes with sh, so you don't need to use escaping. Also you have to create an initial file with > and add content with >>:
pipeline{
agent any
stages{
stage('write file'){
steps{
sh 'echo "line 1" > greetings.txt'
sh 'echo "line 2" >> greetings.txt'
echo "The contents of the file is"
sh 'cat greetings.txt'
sh 'rm -rf greetings.txt'
}
}
}
}
output:
[test] Running shell script
+ echo line 1
[Pipeline] sh
[test] Running shell script
+ echo line 2
[Pipeline] echo
The contents of the file is
[Pipeline] sh
[test] Running shell script
+ cat greetings.txt
line 1
line 2
[Pipeline] sh
[test] Running shell script
+ rm -rf greetings.txt
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
It's not finding a file named greetings.text because you didn't create one (a litte typo in the extension in your cat line). Try sh 'cat greetings.txt', or even better adjusted your script:
sh "echo \"line 1\" >> greetings.txt"
sh "echo \"line 2\" >> greetings.txt"
echo "The contents of the file are"
sh 'cat greetings.txt'
sh 'rm -rf greetings.txt'
If you want to use multiline commands, you can also use this syntax:
sh """
echo \"line 1\" >> greetings.txt
echo \"line 2\" >> greetings.txt
echo "The contents of the file are:"
cat greetings.txt
rm -rf greetings.txt
"""
From the last example, this should generate an output like:
Running shell script
+ echo 'line 1'
+ echo 'line 2'
+ echo 'The contents of the file are:'
The contents of the file are:
+ cat greetings.txt
line 1
line 2
+ rm -rf greetings.txt

Jenkins, how to add variable content to file

How can I add variable content in file?
sh "ssh root#${host} 'echo '$vari' > text.txt'"
This gives empty file
Without variable it works:
sh "ssh root#${host} 'echo some text > text.txt'"
You can use the writeFile command offered by Jenkins:
writeFile file: "text.txt", text: YOUR_VARIABLE
This are 2 ways how you can write the content of a variable to a file:
pipeline {
agent any
environment {
VAR = "hello world!"
}
stages {
stage('Write to file'){
steps{
sh 'echo "${VAR}" > test.txt'
sh "echo ${VAR} >> test.txt"
sh 'cat test.txt'
}
}
}
}
Output:
[test] Running shell script
+ echo hello world!
[Pipeline] sh
[test] Running shell script
+ echo hello world!
[Pipeline] sh
[test] Running shell script
+ cat test.txt
hello world!
hello world!

Passing arguments from a file to shell in Pipeline

Basically I'm trying to pass properties to shell from a file.
I have a file "docker_info" with following content in workspace
IMAGE_NAME='Docker-image'
IMAGE_VERIONS='Docker-1.3.5'
Here is my pipeline script:
node
{
load "${WORKSPACE}/docker_info"
sh " echo ${IMAGE_NAME}" // here getting expected output: Docker-image
stage('Executing SHELL SCRIPING TO CHECK DOCKER IMAGE')
sh '''
echo "$DOCKER_IMAGE"
if [ -z "${IMAGE_NAME}" ] //(also tried "$IMAGE_NAME")
then
echo "Docker image not found."
exit 0
fi
echo "${IMAGE_NAME}:started pushing image"
'''
}
OUTPUT:
[Test_BUILD_PIPELINE] Running shell script
+ echo Docker-image
Docker-image
**Entering stage Executing SHELL SCRIPTING TO CHECK DOCKER IMAGE**
Proceeding
[Pipeline] sh
[Test_BUILD_PIPELINE] Running shell script
+ echo ''
+ '[' -z '' ']'
+ echo 'Docker image not found. Skip pushing Docker image'
Docker image not found. Skip pushing Docker image
+ exit 0
Kindly note after entering into the stage I Won't see the expected value(Docker-image)instead displaying: echo ' '
I tried with several ways but that haven't worked.
sh '''
. /path/to/the/docker_info
if [ -z "$IMAGE_NAME" ]
then
echo "Docker image not found"
exit 0
fi
echo "$IMAGE_NAME:started pushing image"
'''
Try
sh """
echo '$DOCKER_IMAGE'
if [ -z '$IMAGE_NAME' ]
then
echo 'Docker image not found.'
exit 0
fi
echo '$IMAGE_NAME:started pushing image'
"""

Resources