Before we start learning on the jenkins job, let's understand one thing from the docker.
We are doing below things to make you understand that there are two different entities
Let's do basic things for understanding the concept
docker psinside terminal/putty
[[email protected] ~]# docker exec -it jenkins bash [email protected]:/$
lscommand to list files
Now open the jenkins on the UI and log into it using the credential you have created.
echo Hello worldcommand in the command input box
instead of showing the out on console we can rediret the out to a file. For example, we can create the file with touch command (vi command will not work inside the docker container by default)
First, switch to the Jenkins container; In the below command, we are writing "chercher tech" to file called sample using
touch command and we are using
cat command to show the content of the sample file.
[[email protected] ~]# docker exec -it jenkins bash [email protected]:/$ echo "chercher tech" > /tmp/sample [email protected]:/$ cat /tmp/sample chercher tech
Similar to above we can create files in jenkins; sometimes we required to redirect the output to file. In such cases, this example will help.
echo "todays date and time is : $(date)" > /tmp/datetime.txt
In the above topic we have seen how to eecute a command from jenkins; sometimes we might need to execute bunch of commands. In such cases, it is better create a file and place those commands or script in it.
lets create sample script in jenkins container, we cannot create files inside the docker conatiner (jenkins container is docker conatainer). so We have to create the file in VM (centOS here) and then copy the file to jenkins container.
Create a file called demo
[[email protected] jenkins-data]# vi demo.sh
Place the below content in it; In the below code, we are exoecting two parameter along with the filename for the execution.
#!/bin/bash NAME=$1 SITE=$2 echo "hello $NAME, your website is $SITE"
Provide the executable permission to the file
chmod +x demo.sh
To execute the above file we have to use below
# ./filename param1 param2 ./demo.sh karthiQ CherCherTech
Now copy the file into jenkins container
#docker cp srcFile targetContainer:targetFolder docker cp ./demo.sh jenkins:/tmp/
Edit the Build step like below
Build the Jenkins Job and Goto the Console output
There will be times where you have to change the parameters used in the script. In the above example we have used "Larry" and "Google" as parameters and which are hardcoded; But if you are working for an application and if it's URL/Username/Password keeps changing then you cannot hard code them.
In such cases, we have to change it based on the build. For this Purpose, Jenkins provides a way for parameterization.
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.