Setting up JENKINS

Setup Jenkins for continuous integration and continuous Deployment for your Project. (mainly Focusing on freestyle and Pipeline project)

Shivam Garg
FAUN — Developer Community 🐾

--

FreeStyle Job

1. Add a new task in Jenkins dashboard: by clicking on New Item on top-left side of the Jenkins Dashboard.

2. Write the name for your Job And Select FreeStyle.

3. Type in the git repository URL

4. And before doing it we have to make RSA key pair for allowing Jenkins to clone the git repository for creating the build

a. Now generate RSA key pair by typing the command ssh-keygen, refer github article.

b. The control will ask you to enter the name for your rsa_key pair and after that type in any passphrase if you want

c. By typing “cat <-yourkeyname->.pub” you can get the public part of your key, that you need to copy at GitHub, to link it with your server, and authenticate cloning.

d. Go to settings and then click on SSH and GPG keys, there you’ll find a button for New SSH Key, click on it

e. Now type the name and paste the public part of your key in the given input box

5. Now, return to the Jenkins job configuration, where you left at step-3, under Source Code Management, Select git, and type the git URL for repository

6. Click on Add button to add new Jenkins key for authenticating Jenkins job with GitHub,

a. here first select the SSH username with the private key option in Kind key,

b. Then define the scope as Global,

c. Type in the username for your private key,

d. select enter directly in front of Private key,

e. And then enter the private part of the key that you created in step 4(a) by command cat <-private keyname>. And click Save

7. In the Build Triggers option, select the GitHub hook trigger for the GITScm polling option.

8. Add the build steps under Build, by clicking on Add build steps -> execute a shell.

a. Then add build steps for your project, and click apply and then save.

9. You will get a job dashboard window like this,

10. After all these steps, add a webHook in the GitHub repository.

a. Go to your repository dashboard, and click on settings -> webhooks.

b. Click on Add a webHook button, You’ll get a window to define your webhook, type a name for your webhook, Then the URL for the Jenkins server under payload URL, followed by /github-webhook/, click Add Webhook to save your webhook.

c. If it shows green tick it means, github webhook is working fine, but if it shows the error, then check the URL for your Jenkins server,

10. Open the Job, and click on Build Now, For running the for the first time

11. If succeeds, then it implies that your build is working, now you can build your job, by a push on the repository.

Pipeline Job

  1. Here also all the steps are the same, the only difference is that we have to include a jenkinsfile in our project, which will define how every step is going to run.
  2. First, create a job by typing a name and selecting the type as pipeline.

3. In the Build Triggers option, select the GitHub hook trigger for the GITScm polling option.

4. Now you have two options for a pipeline build

5. If you select pipeline script then you’ll type the jenkinsfile script in the given input box.

6. And If you select Pipeline script from SCM you’ll have to use a Jenkins file in your project.

7. Select git as SCM,

a. Type the git repository URL, and select the RSA key, for credentials, that we have defined in freestyle steps 4–5,

b. Then select the branch on which you want to monitor the push,

c. Type the path for your jenkinsfile script in script path.

7. Your Jenkins file should look like this.

How to write jenkinsfile script

There are four main stages,

  1. Check the SCM stage, where you check the source code management system, in our case git. This step is required to clone the git repository
  2. Then the second step is installing dependencies, like node_module in angular, we do it by npm install or npm ci.
  3. Then the third step, here we define the build stage, for our project, in case of angular, we run ng build --prod , we can add a few more tags like--base-href to define the value of the base URL for your project.
  4. Then the fourth step is to deploy. Here we define the steps for deploying our project to localhost or remote host.in case of angular, a dist folder is generated. We just have to move that dist folder to the running server.

i.) For localhost:

a. Remove: sh "rm -fr ~/tomcat/webapps/←project directory →"

b. Make folder: sh "mkdir ~/tomcat/webapps/←project directory →"

c. Copy: sh "cp -r dist/* ~/tomcat/webapps/←project directory →"

ii.) For the remote host:

a. Remove: sh "sshpass -p password ssh <-user-name->@<-IP-> rm -fr /←location →/←project name →"

b. Make folder: sh "sshpass -p password ssh <-user-name->@<-IP-> mkdir /←location →/←project name →"

c. Copy: sh "sshpass -p password scp -r dist/* <-user-name->@<-IP->:/←location →/←project name →"

Follow us on Twitter 🐦 and Facebook 👥 and join our Facebook Group 💬.

To join our community Slack 🗣️ and read our weekly Faun topics 🗞️, click here⬇

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--