• We can use Jenkins to run custom shell commands
  • Here, we are using a standalone installation of Jenkins on Ubuntu 19.04
  • Let's say we want to install nginx on the same machine
  • To check if nginx is already installed

networkandcode@jenkins-practice:~$ apt-cache policy nginx

nginx:

Installed: (none)

Candidate: 1.15.9-0ubuntu1

Version table:

1.15.9-0ubuntu1 500

500 http://us-central1.gce.archive.ubuntu.com/ubuntu disco/main amd64 Packages

networkandcode@jenkins-practice:~$ which nginx

networkandcode@jenkins-practice:~$

 

  • Two things confirm nginx is not present:
    • The Installed: (none) in the first command
    • no output in the 'which' command
  • The 'which' command is also useful for this check, if we have installed an application directly with out the use of apt
  • Let's create a directory to keep shell scripts, under the .jenkins directory

networkandcode@jenkins-practice:~$ mkdir ~/.jenkins/shellscripts

 

  • Let's create a shell script containing a single command to install nginx

networkandcode@jenkins-practice:~$ cat > ~/.jenkins/shellscripts/install-nginx.sh

apt install nginx -y

^C

 

  • To give executable permission, so that script can be run

networkandcode@jenkins-practice:~$ chmod +x ~/.jenkins/shellscripts/install-nginx.sh

 

  • Let's create a new job on Jenkins
  • Note Jobs and Projects mean the same in Jenkins
  • On the Jenkin's dashboard select New Item > Name it for example 'job1-install-nginx' > Freestyle project > Ok
  • Then, General > Advanced > Use custom work space > Directory > shellscripts
  • Then, General > Build > Add build step > Execute shell > ./install-nginx.sh
  • Save
  • The Job is now created
  • Let's build it a.k.a run the shell script
  • Click 'Build now' on the left panel of the page
  • Build 1 is scheduled to run
  • But there was a red circle near #1 (Build 1) when I clicked it and further on Console Output, I saw the following error

E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)

E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?

 

  • I should now edit the file to put sudo as a prefix to the command
  • It should now look like:

networkandcode@jenkins-practice:~$ cat ~/.jenkins/shellscripts/install-nginx.sh

sudo apt install nginx -y

 

  • Let's run the build again
  • Cick Back to Project and see that you are at the right project - click Build Now
  • Build 2 is now successful as there was blue circle near #2
  • Let's verify the installation on nginx on the linux terminal, like we did before

networkandcode@jenkins-practice:~$ apt-cache policy nginx

nginx:

Installed: 1.15.9-0ubuntu1

Candidate: 1.15.9-0ubuntu1

Version table:

*** 1.15.9-0ubuntu1 500

500 http://us-central1.gce.archive.ubuntu.com/ubuntu disco/main amd64 Packages

100 /var/lib/dpkg/status

networkandcode@jenkins-practice:~$ which nginx

/usr/sbin/nginx

 

  • So the job and hence the installation is successful

--end-of-post--