Passing arguments from a file to shell in Pipeline - shell

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'
"""

Related

error in executing interactive bash script to run docker containers

Hello guys i am new in shell scripting and I tried run the script below but have following error message:
#!/bin/bash
echo "Enter the name of the image whose container you want to run: "
read container
echo "Enter a name for this container: "
read name
echo "do you want to run in detatch mode? "
read d
if [ $d -eq yes ]
then
docker run --name $name -P -d $container
elif [ $d -eq no ]
then
docker run --name $name -P $container
else
echo "invalid input"
fi
This produces the following error messages:
./main.sh: line 9: [: yes: integer expression expected
./main.sh: line 12: [: yes: integer expression expected
-eq compares two integers
use = in order to compare two strings
for more info please refer to: https://stackoverflow.com/a/20449556/9881735
for example:
[ $d = "yes" ]

suppress extra o/p generated by gcloud command

I am using gcloud commands to deploy simple VMs. I am using startup script to configure all the required packages on the machine. Our packages are fetched from nexus. Sometimes our changes or nexus network issues result in failure of startup script. I configured a block of code to validate startup script return code. I run gcloud create command and provide startup script, the code block to validate startup script output looks like this..
echo "Step 2: sleep till startup script finishes"
## block to check return code of the VM startup script
MAX_WAIT_TIME=1200
WAIT_TIME=0
RC_CODE=""
echo "[INFO] waiting for startup-script return code"
while [ -z $RC_CODE ] && [ $WAIT_TIME -le $MAX_WAIT_TIME ]
do
RC_CODE=$(gcloud compute instances get-serial-port-output gce-$GCP_ZONE-d-$APP-dev \
--project=$GCP_PROJECT_ID \
--zone=$GCP_ZONE | awk '/^[0-9]{4}/ && /google_metadata_script_runner/ && /startup-script-url exit status/ {sub("\r", "", $NF); print $NF}')
if [[ -z $RC_CODE ]] ; then
echo -n "."
WAIT_TIME=$((WAIT_TIME+10))
sleep 10
else
if [[ $RC_CODE -eq 0 ]] ; then
echo -e "\n[INFO] startup script completed with return code $RC_CODE."
break
else
echo -e "\n[INFO] Startup script completed with return code $RC_CODE."
exit $RC_CODE
fi
fi
done
# to check timeout scenario
if [[ -z $RC_CODE ]]
then
echo "[INFO] Startup script timed out after $((MAX_WAIT_TIME/60))."
echo "[INFO] Startup script completed with return code 1."
exit 1
fi
My output looks like this,
+ ./sh/create-dev-vm-app.sh
Deleted [https://www.googleapis.com/compute/v1/projects/project-name/zones/europe-west1-c/instances/gce-europe-west1-c-d-myapp-dev].
Created [https://www.googleapis.com/compute/v1/projects/project-name/zones/europe-west1-c/instances/gce-europe-west1-c-d-myapp-dev].
waiting for startup-script return code
Specify --start=2473 in the next get-serial-port-output invocation to get only the new output starting from here.
.
Specify --start=32157 in the next get-serial-port-output invocation to get only the new output starting from here.
.
Specify --start=37602 in the next get-serial-port-output invocation to get only the new output starting from here.```
.
Startup script completed with return code 0.
How can I suppress the these 'Specify' lines from appearing on o/p screen? AND/OR print all startup-script messages received from get-serial-port-output output after I receive the return code.
Swallow standard error to avoid the extraneous output:
gcloud ... 2>/dev/null | awk ...

I'm not able to run the MVN goals through shell script

I just used the below shell script in Post Build activity in JENKINS "Execute Shell" cmd
#!/bin/bash
mvn --version
export M2_HOME=/opt/maven/maven-3.3.3 # your Mavan home path
export PATH=$PATH:$M2_HOME/bin
mvn --version
echo $HOME
echo $WORKSPACE
file=$WORKSPACE/XXXX/XXXX-reports
cd $file
cp XXXX-1.html test.html
fail=`grep "test-method.*FAIL" results.xml | sed -e 's/^.*test-
instance-name="\(.*\) ' | tr '\n' ','`
echo $fail
count=0
while [ $count -lt 5 ]
do
if [ ! -z "$fail" -a "$fail" != " " ];
then
echo `$M2_HOME/bin/mvn clean test -f ../../pom.xml -DInclude=${fail}`
cp XXXX-report.html ReReport_$count.html
retry=`expr $count + 1`
fi
done
In the above shell script I'm trying to run the mvn command and continuing to run 5 times to increase pass count in UNIT script execution.
But It's not working. It's failed to execute the maven goals. So, I just comment the executable and added echo to debug.
But no luck.
Any leads
I suggest that you use this following script that modifies yours. Add your parameters there.
The product log file: mvn_jenkins.log will allow you to easily find problems as suggested by mjuarez:
#!/bin/bash
MVNCMD=$(command -v mvn)
MVNPARAMS=" -version" # replace with your appropriate params
LOGFILE="mvn_jenkins.log"
COUNT=0
while [ $COUNT -lt 5 ]; do
echo "Execute: $MVNCMD $MVNPARAMS" >> $LOGFILE
echo "-------" >> $LOGFILE
$MVNCMD $MVNPARAMS 2>&1>> $LOGFILE
echo "" >> $LOGFILE
COUNT=$((COUNT + 1))
done
Explanation:
MVNCMD=$(command -v mvn): put the full qualified name of maven execute file in MVNCMD var
$MVNCMD $MVNPARAMS 2>&1>> $LOGFILE: Execute mvn command with parameters and redirect error output messages (2) and regular output messages (1) to the log file.
About your maven parameters I don't understand why you use -DskipTests=true.

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

Curl command in bash script error

I'm trying to run a curl command within a bash script and I'm coming across the following error:
curl: (26) failed creating formpost data
When I echo the curl command and run that from the command line, that has no problems running. I've ensured there are read permissions for everyone on the file and the folder it's in, as well as using relative and absolute paths.
Here's a sample of the script:
#!/bin/bash
usage(){
echo "usage: <command> options: <u|p|f> "
}
while getopts u:p:f: option
do
case "${option}"
in
u) USERNAME=${OPTARG};;
p) PORT=${OPTARG};;
f) FILE=$OPTARG;;
esac
done
if [ $OPTIND -eq 1 ]; then
#echo "No options were passed";
usage
exit
fi
echo "Please enter your password: "
read PASSWORD
TEST="curl -u $USERNAME:$PASSWORD -F file=#\"$FILE\" -F force=true -F install=true http://localhost:$PORT/test.jsp"
echo $TEST
$TEST
echo $?
Thanks!

Resources