How to Setup a CI/CD pipeline using Github, Jenkins, Ansible and Apache web Server.
GitHub is a web-based platform for version control and collaborative software development. It allows multiple developers to work on projects simultaneously, managing changes, branching, and merging. It uses Git, a distributed version control system, to track changes to code, facilitating collaboration and maintaining a history of project modifications.
Jenkins is an open-source automation server that aids in building, testing, and deploying software projects. It supports continuous integration and continuous delivery (CI/CD) pipelines, automating tasks such as code compilation, testing, and deployment. Jenkins provides an extensible plugin architecture that allows integration with various tools and services, enabling the creation of efficient and automated software development workflows.
Ansible is an open-source automation tool designed for configuration management, application deployment, and task automation. It uses a declarative language to define tasks and configurations, which are then executed on remote servers. Ansible helps streamline complex IT operations by automating repetitive tasks, managing configurations consistently, and orchestrating deployments across various environments.
Apache Web Server (officially called Apache HTTP Server) is a widely used open-source web server software. It serves web content over the internet using the HTTP and HTTPS protocols. Apache is highly customizable and supports various modules that enhance its functionality, such as handling dynamic content, authentication, URL rewriting, and load balancing. It is known for its stability, security, and performance, making it a popular choice for hosting websites and web applications.
In order to setup the Lab work we need to deploy 4 instances and instances should be inside the same virtual private network.
Firstly, we need to setup a password less authentication between the Developer Instances (GitHub Instance in our case) and GITHUB.
for that we need to create a ssh-key using ssh-keygen and add it to the deploy keys tab in our GitHub repo.
Copy the contents Public Key from given path which /home/ubuntu/.ssh/id_rsa.pub in our case nd add it to the deploy keys section present in GitHub-repo->settings->deploy Keys.
After that you will be able setup the password less authentication between the developer instance and the remote GITHUB repo which enables developers to push any kind of the changes to code repo. But before pushing the changes you need to configure the global credentials.
2. You need to install Jenkins on to the Jenkins server that enables you to create a freestyle job.
To install Jenkins you can primary execute this shell script.
#!/bin/bash
sudo apt update
sudo apt install openjdk-11-jdk -y
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins -y
systemctl start jenkins
systemctl enable jenkins
sudo cat /var/snap/jenkins/2126/secrets/intialAdminPasswordYou need to provide certain permission in order to make this bash executable which can be achieved using chmod command.
3. Now we have to setup ansible in the ansible server and along with that we need to install Apache httpd server on the web server to expose our website to the users.
In order to install the ansible you can run the following bash script.
#!/bin/bash
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansibleThen you can install Apache httpd server using following command :- sudo apt install apache2
4. In order to execute the Jenkins job we need to first setup the password less authentication between Jenkins and Ansible server and then between Ansible and Web server.
In order to set up the password less authentication tunnel follow the below procedure.
- First you need to Vi /etc/ssh/sshd_config file and enable permitrootlogin to yes and passwordAuthentication to yes.
- Reset the password for the root user with the command passwd root.
- Restart sshd service with the command:- systemctl restart sshd.
- Copy the ssh key from Jenkins server to ansible server and similarly for ansible and web server.
In order to create a public key use the command ssh-keygen to generate a new key and to copy the key from source to destination server use the following command.
ssh-copy-id root@<private Ip of the destination server>
5. In order to publish the files over ssh using the Jenkins job you need to install a plugin.
Similarly add the Ansible Server in the Add servers section.
6. Create the freestyle job in the Jenkins Dashboard. Note :- Jenkins will be accessible in the following path :- http://<public ip of the Jenkins server>:8080.
here I am using a public repository therefore we don't need add the GitHub credentials but in case of private repo you need to add your GitHub account credentials in Jenkins while configuring the job.
We can use the following command to copy all the website content from the Jenkins workspace to Ansible server.
rsync -avh /var/lib/jenkins/workspace/<Job name>/* root@<private IP of ansible server>:/opt
You can use the ping module to test the connection between the ansible master servers and slave web servers.
Note:- that before testing the ping module you need to establish password authentication between the ansible master server and the web server.
You need to write a playbook.yml file inorder to copy all the contents of the website from the /opt folder to /var/www/html folder.
---
- name: Copy Multiple Files to Web Server
hosts: all # Replace with the name of your target web server group or hostname
become: yes # Enable privilege escalation to perform tasks as a superuser
vars:
source_dir: /opt # Replace with the source directory path on the Ansible master
dest_dir: /var/www/html # Replace with the destination directory on the web server
files_to_copy:
- index.html
- style.css
- js.js
- README.md
# Add more filenames as needed
tasks:
- name: Copy files to web server
copy:
src: "{{ source_dir }}/{{ item }}"
dest: "{{ dest_dir }}/{{ item }}"
loop: "{{ files_to_copy }}"
become: yes
you can use the following command to run the playbook file :- ansible-playbook <path of the playbook.yml file>
In order to enable the Continuous delivery action we need to mention the following command in the Jenkins Job post build actions section -> select send artifacts over ssh.
Then in order to automate the whole process you need to add a web hook following the steps shown below.
While creating the webhook you need to add a secret which you can generate in the following way:- go to Jenkins Dashboard -> Click on your username -> configure -> generate Api token.
That it, now you can commit new changes to GitHub repo and the the changes will automatically be deployed into the webserver.
As you can see the build started because of the latest commit automatically.
Thank you.
Hey Siba,
ReplyDeleteDev here I have some doubts related to the above blog . If you are free someday we can sit and discuss comfortably.