DEV Community

samsha1
samsha1

Posted on

Getting Started in CI/CD for Beginners

CI/CD for begineer

What is CI/CD?

CI/CD stands for Continuous Integration and Continuous Deployment. Continuous Integration means building your application continuously. Consider, a scenario where developer makes some changes in source code. Now Continuous Integration must be able to fetch that source code and prepare a Build.Build also involves compiling and validating your code, code review, Unit Testing and Integration Testing, also packaging your application.

After Continuous Integration we have Continuous Delivery. Until now your product is ready, tested and ready for delivery. Consider, a Continuous Integration tools like Jenkins which deploy into the test servers to perform a user acceptance testing and once this is done it will be deployed onto the prod server for release. If this step is done manually then it is called Continuous Delivery but yes if its done automatically then its Continuous Deployment.

Understanding the WorkFlow

First, Let's have simple Hello world project in our local machine and initialize git. Once done push our code to remote repository. Next, We will write simple Jenkins Job so that It will trigger and deploy our code to dev server.We will create two different aws instance for jenkins and dev. Remember, we will run jenkins application inside Docker Container so that we donot have to go through manual installation.

CI/CD workflow

Let's Bring It To Life

Inside Local Machine

Step 1: Let's create simple Hello World Project written in php.

Step 2: Initialize git, commit and push your changes to remote repository.

Inside GitHub

Create a webhook so that jenkins jobs would trigger or look for changes whenever code is pushed to specified branch.

Go to Settings -> Webhooks -> Add Webhooks.

Payload URl:The URL is in the form $JENKINS_BASE_URL/github-webhook/ — for example: https://ci.example.com/jenkins/github-webhook/.

Content Type: application/json

Secret Key: Secret Key so that github can access jenkins.

Which events would you like to trigger this webhook? : Depends on events you prefer. I have just checked for pushed event i.e. job is triggered whenever something is pushed to github master branch. (For specific branch name you can mention it from jenkins).

Update Webhooks. That's it inside Github.

Inside Jenkins Instance

Step 1: ssh to your aws Jenkins instance

```

ssh -i




Step 2: Run update your packages.

sudo apt-get update




Step 3: Install docker


sudo apt-get install docker.io




Step 4: Add Docker to your user group and login again after exit


sudo usermod -aG docker ubuntu




Step 5: Let's pull docker image for jenkins:


docker pull jenkins/jenkins




Step 6: Type


docker images



to see pulled image from jenkins/jenkins repository

Step 7: Run Docker image:


docker run -d -p 8080:8080 --name




Step 8:Type


docker ps



to see running container.

Step 9: Once you setup jenkins inside docker. Try, accessing from browser


        ```
<instance public ip >:<external-port-number>

Note: Before accessing, open the add inbound rules in aws. choose security groups(launch-wizard). click Edit->Add Rules, set custom tcp rules and _external port range (my case 8080)_also http rule then save

Voila! you can then run jenkins, Install suggested plugin (default). Fill the required credential and set your first job set!

Step 1: create Freestyle Project.

Step 2: Add some Description and give the github repository url from where jenkins would fetch your project.

Remember, to setup webhook inside github. Go to settings->webhooks->add payload url i.e /github-webhook/

Step 3: Write some shell script to deploy your code inside dev-server.

To establish, a connection between jenkins and dev instance we need to add jenkins public key to dev authorized key.

In jenkins instance run ssh-keygen to generate the .pub key and copy public key from .ssh/ to dev ./ssh/authorized_keys.

instance = '13.232.89.159' 

ARCHIVE_FILENAME = cicd.zip

zip --symlinks -x *.git* -r $ARCHIVE_FILENAME . #compress all the files and create zip file

echo "--------Copying Files to remote dev server from jenkins ----------"

scp -o StrictHostKeyChecking=no index.tar.gz ubuntu@$instance:/home/ubuntu/

echo "--------Finished Copying----------"

echo "Entering to Dev Instance"

ssh -o StrictHostKeyChecking=no ubuntu@$instance '

  mv $ARCHIVE_FILENAME /var/www/html #move zip file from current directory to /var/www/html

  cd /var/www/html

  unzip $ARCHIVE_FILENAME

  rm -rf $ARCHIVE_FILENAME #remove the file once done

' 

Once, the scripting part is done let's save and build our job. After successful job build we can access our deployment server from our browser and see the changes made.

This is just a simple example we have done, just to understand the workflow of CI/CD.

You are awesome!

Top comments (10)

Collapse
 
alex_barashkov profile image
Alex Barashkov

Thank you for the article! It's quite ironic I guess name it CI/CD for beginners and use Jenkins. I don't want to complain about Jenkins, it's great tool, but not not for 2019. For beginners I would recommend better to use Gitlab or Drone CI, they just simpler.

Collapse
 
bauripalash profile image
Palash Bauri 👻

Typo Found!
Begineers should be Beginners

Collapse
 
tnypxl profile image
tnypxl

Nay. Begineers should be Bengineers. "Beginning engineers". See it. See what I did there? ;)

Collapse
 
mrtnrdl profile image
Martin Riedel

Nice primer! And an advice for beginners: It's possible to automate a ton of security checks - look into DevSecOps when you want to make sure you're not only delivering fast, but also secure.

Collapse
 
sanjaygyawali profile image
sanjaygyawali

Great

Collapse
 
peterwitham profile image
Peter Witham

As someone that is just starting to appreciate Docker and a daily user of Jenkins management, I really love this walkthrough.

Thank you!

Collapse
 
samsha1 profile image
samsha1

Glad you liked!! :)

Collapse
 
devqx profile image
Oluwaseun Paul

Create a webhook so that jenkins jobs would trigger or look for changes whenever code is pushed to specified branch. The step for this is not listed , i feel the tutorial is not complete. thanks

Collapse
 
samsha1 profile image
samsha1

Thanks, @devqx I will update the steps..

Collapse
 
benalidjamel profile image
benali djamel

thank you