<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Amsal Khan</title>
    <description>The latest articles on DEV Community by Amsal Khan (@amsal).</description>
    <link>https://dev.to/amsal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F652360%2F905245d3-9e6c-44c0-9b90-d0201f9af470.png</url>
      <title>DEV Community: Amsal Khan</title>
      <link>https://dev.to/amsal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amsal"/>
    <language>en</language>
    <item>
      <title>Complete Automation Integrating Docker, Github with Jenkins Multibranch CI/CD Pipeline on AWS EC2</title>
      <dc:creator>Amsal Khan</dc:creator>
      <pubDate>Mon, 27 Sep 2021 18:45:35 +0000</pubDate>
      <link>https://dev.to/amsal/complete-automation-integrating-docker-github-with-jenkins-multibranch-ci-cd-pipeline-on-aws-ec2-3l0o</link>
      <guid>https://dev.to/amsal/complete-automation-integrating-docker-github-with-jenkins-multibranch-ci-cd-pipeline-on-aws-ec2-3l0o</guid>
      <description>&lt;h2&gt;
  
  
  What is CI/CD?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;CI stands for Continous Integration&lt;/li&gt;
&lt;li&gt;CD stands for Continous Delivery or Continous Deployment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let me explain through a example: &lt;/p&gt;

&lt;p&gt;When you make a commit and push sources to Git SCM(Source Code Management like Github, GitLab etc), then automatic builds and tests are run with the latest changes, this is knows as Continous Integration&lt;/p&gt;

&lt;p&gt;With Continous Delivery, code changes are automatically built, tested, and prepared for a release to production. Continuous delivery expands upon continuous integration by deploying all code changes to a testing environment and put on a manual approval for adding them in production envirment.&lt;/p&gt;

&lt;p&gt;Continous Deployment expands upon continuous delivery and pushed to production environment automatically without any approval.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd1.awsstatic.com%2Fproduct-marketing%2FDevOps%2Fcontinuous_integration.4f4cddb8556e2b1a0ca0872ace4d5fe2f68bbc58.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fd1.awsstatic.com%2Fproduct-marketing%2FDevOps%2Fcontinuous_integration.4f4cddb8556e2b1a0ca0872ace4d5fe2f68bbc58.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Pipeline and MultiBranch Pipeline?
&lt;/h2&gt;

&lt;p&gt;A DevOps pipeline is a set of automated processes and tools that allows both developers and operations professionals to work cohesively to build and deploy code to a production environment&lt;br&gt;&lt;br&gt;&lt;br&gt;
Jenkins MultiBranch pipeline allows us to automatically create a pipeline for each branch on your source control repository.&lt;br&gt;Multibranch pipeline works using along with Jenkinsfile that is usually stored along with our source code inside a version control repository.&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Jenkins
&lt;/h2&gt;

&lt;p&gt;Jenkins is an open source automation server.&lt;br&gt;
The &lt;a href="https://www.jenkins.io" rel="noopener noreferrer"&gt;Jenkins&lt;/a&gt; project was started in 2004 (originally called Hudson) by Kohsuke Kawaguchi, while he worked for Sun Microsystems. It was mainly developed for Continuous Integration, but in recent days, Jenkins orchestrates the entire software delivery pipeline — called continuous delivery and Continuous Deployment (CD). Presently, it is among one of the most popular Devops tools.&lt;/p&gt;
&lt;h2&gt;
  
  
  Docker
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/docker/docker" rel="noopener noreferrer"&gt;Docker&lt;/a&gt; is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.&lt;/p&gt;

&lt;p&gt;It is a tool that is designed to benefit both developers and system administrators, making it a part of many DevOps (developers + operations) toolchains. For developers, it means that they can focus on writing code without worrying about the system that it will ultimately be running on. It also allows them to get a head start by using one of thousands of programs already designed to run in a Docker container as a part of their application. For operations staff, Docker gives flexibility and potentially reduces the number of systems needed because of its small footprint and lower overhead.&lt;br&gt;
Here, we will be integrating Docker with Jenkins.&lt;/p&gt;

&lt;p&gt;Pre-Requisites&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;RHEL8 (you may install any other OS but steps will differ)&lt;/li&gt;
&lt;li&gt;Docker&lt;/li&gt;
&lt;li&gt;Jenkins&lt;/li&gt;
&lt;li&gt;MobaXterm SSH Client (or any other whichever you prefer)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Deploy a AWS EC2 Instance of RHEL8-OS from AWS Console.&lt;br&gt;&lt;br&gt;
Make sure to add Inbound rules for 8080 port for Source 0.0.0.0/0&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Execute these commands to Install Docker:&lt;br&gt;
&lt;code&gt;sudo dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo&lt;/code&gt;&lt;br&gt;
&lt;code&gt;sudo dnf install docker-ce&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Execute these commands to Install Wget, OpenJDK, Jenkins:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo dnf update -y
sudo dnf install wget -y
sudo wget http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo -O /etc/yum.repos.d/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
sudo  dnf install -y java-11-openjdk-devel
sudo dnf install http://repo.okay.com.mx/centos/8/x86_64/release/daemonize-1.7.8-1.el8.x86_64.rpm
sudo dnf install -y jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Give Jenkins user sudo priviledges:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vi -f /etc/sudoers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;and add the following line in the bottom of file:&lt;br&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jenkins ALL= NOPASSWD: ALL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Lets Configure Jenkins now:
Open jenkins on web-browser using URL:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://aws_ip_address:8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;br&gt;&lt;br&gt;
    * You will need to get the token from&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/var/lib/jenkins/secrets/initialadminpassword
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo cat /var/lib/jenkins/secrets/initialadminpassword
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and copy the text and paste that on Jenkins webpage.&lt;br&gt;&lt;br&gt;
    * Select Install Suggested Plugins&lt;br&gt;&lt;br&gt;
    * Create a first admin user&lt;br&gt;&lt;br&gt;
    * Save and Finish&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a Personal Access Token by visiting &lt;a href="https://github.com/settings/tokens" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Set all permissions and set Expiration to No Expiration.
And then go to Manage Jenkins-&amp;gt;Configure System
In the Add GitHub Server:
Enter Name:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Personal_Access_Token_USER
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;API URL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://api.github.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add-&amp;gt;Jenkins, select Kind-&amp;gt;Secret Text&lt;br&gt;&lt;br&gt;
Enter Secret as your Personal Access Token which you created earlier.&lt;br&gt;&lt;br&gt;
ID (optional):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;b91c96c3-7a3f-4e08-b22d-c1100dd49eb9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enter same ID if you are going to use my Jenkinsfile from this project or edit the ID in the Jenkinsfile acccordingly.&lt;br&gt;&lt;br&gt;&lt;br&gt;
Go to Manage Jenkins-&amp;gt;Manage Credentials-&amp;gt;Jenkins-&amp;gt;Global credentials(unrestricted)-&amp;gt;Add Credentials&lt;br&gt;&lt;br&gt;
Username: Your GitHub Username&lt;br&gt;&lt;br&gt;
Password: Enter the Personal Access Token which was generated earlier&lt;br&gt;&lt;br&gt;
ID (optional):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;434b0b23-9deb-4ee6-85d4-43c4c23513bb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enter same ID if you are going to use my Jenkinsfile from this project or edit the ID in the Jenkinsfile acccordingly.&lt;br&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Download and Modify Jenkinsfile
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What it does?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Well, Jenkinsfile is the ultimate pipeline file through which Jenkins will automatically detect the whole automation process and follow the steps mentioned in it.&lt;br&gt;&lt;br&gt;
Download my &lt;a href="https://github.com/Amsal1/devops_ci_cd/blob/master/Jenkinsfile" rel="noopener noreferrer"&gt;Jenkinsfile&lt;/a&gt;&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FGWC3pIL.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FGWC3pIL.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Replace my git repo url with yours throughout the file. Also change UserIdentity block with your GitHub Details in class GitSCM of Code Checkout&lt;br&gt;&lt;br&gt;
Push this modified Jenkinsfile to your both branches in your repo. It would be better if you add it in master branch and create dev branch using that but you may only do this on a new repo. For a older repo, you may need to use combinations of few git commands including git reset, git rebase, git checkout and git branch. Text me if you face any diffcuilties😄&lt;br&gt;&lt;br&gt;
Also remember, I am using master and dev branch. If you're using any any other branch instead of 'dev' then, make changes accordingly in Jenkinsfile.&lt;br&gt;&lt;br&gt;
Also open inbound ports 82 and 83 in AWS EC2 Console like you did for port 8080 in Step 1&lt;br&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select New Item:
Enter Item Name:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git_job_pipeline
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Select Multibranch pipeline and click OK&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Select Add Sources-&amp;gt;GitHub&lt;br&gt;&lt;br&gt;
Select and set Credentials and set your repo name. &lt;br&gt;&lt;br&gt;
In my case: &lt;a href="https://github.com/Amsal1/devops_ci_cd/" rel="noopener noreferrer"&gt;https://github.com/Amsal1/devops_ci_cd/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click Save and it will done rest😄&lt;/p&gt;

</description>
      <category>jenkins</category>
      <category>docker</category>
      <category>devops</category>
      <category>multibranch</category>
    </item>
  </channel>
</rss>
