- What is Jenkins
- What is CICD
- Continuous Integration
- Continuous Delivery
- Continuous Deployment
- Difference between CD and CDE
- Pipeline
- Why Jenkins
- Setting up Jenkins
- Creating a Jenkins Server
- Setting up a webhook with Jenkins
- Merging Github with automation
- Automate Jenkins with AWS
- How to reset password in Jenkins
Jenkins is an open source automation server, Jenkins provides hundreds of plugins to support building, deploying and automating any project.
It offers a simple way to set up continuous integration (CI), continuous delivery (CD).
Continuous Integration and Continuous Delivier is considered as the backbone of DevOps practices and automation, it plays a vital role in DevOps culture.
Developers merge/commit code to the main branch multiple times a day, full automated build and test processes which gives them feedback within a few minutes, by doing this, you are effectively avoiding the integration mayhem that happens when developers wait for the release day to merge all their changes into the release branch.
Is the practice of automating the integration of code changes from multiple contributors into a single software project. It's a primary DevOps best practice, allowing developers to frequently merge code changes into a central repository where builds and tests then run.
It's an extension of continuous integration to make sure that you can release new changes to your customers quicker in a much more sustainable method. So on top of your continuous integration, you also automated your release process and can deploy you application at any point of time by clicking a button. Deployment will have to be done manually but that can also be done automatically
This goes futher than Continuous Delivery by deploying the actually application online automatically, so each change that passes all the stages of your production pipeline is released to your customers, there is no human interaction so this lowers the chance for human error, and even one failed test will prevent the new change being deployed to production.
The main difference is that on continuous delivery, you would have to deploy the application manually whereas continuous deployment is an extension of continuous delivery and deploys the application automatically as well as testing the application too.
A CI/CD pipeline is all about automation: Intialising the code builds, automated testing, and automated deployment to staging or production enviroments. This saves the teams a lot of time as they don't have to create the tests and this process is incredibly fast.
Jenkins has been adopted by the likes of Facebook, Netflix and Ebay because of it's incredible advantages. Jenkins is an open source automation server in which the central build and the continuous integration take place. It works on Windows, macOs, and Linux. It has a user-friendly interface, easy installation, extensible with a ton of plugin resource, and easy environment configuration.
- Create a key and add it to the repo as deploy key
- Add the github url -> HTTPS to Jenkins
- Go to the bottom of Office 365 connector where it says
Restrict where this project can be run
and typesparta-ubuntu-node
- Source Code management
- Click Git
- Add repo url -> SSH
- Create crendentials
- Kind -> SSH Username with private key
- ID -> Name of the key
- Username -> Make it your own so mine is
eng114-florent
- Private Key -> Click Enter Directly -> Add -> Paste the entire private key - Everything
- Build Environment
- Select Provide Node && npm bin/ folder to PATH
So we are going to be setting up a Jenkins server from scratch using AWS EC2 Instance
AWS EC2 Instance setup
- So you want to create an ubuntu 18.04 LTS t2.micro instance
- Security Group really depends on what you need but for me, I will be using,
- You want port 80 open for nginx
- You want port 8080 open for Jenkins
- You want port 3000 open for Node app
- You want port 22 open for SSH
- You can name this something distinguishable, I named mine ->
eng114-florent-jenkins-server
You know what to SSH into your EC2 Instance
- Run
sudo apt update -y && sudo apt upgrade -y
- Run
sudo apt install openjdk-8-jdk
to install Java 8 - Run
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
to import GPG keys of the Jenkins repo - Run
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
- Run
sudo apt update
# Run all these commands one by one, in order
sudo apt update -y && sudo apt upgrade -y
sudo apt install openjdk-8-jdk
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt update -y
sudo apt install jenkins
Once that's done, you now want to grab the public ip of the instances like this ec2-public-ip:8080
and you will be greeted with something that looks like this
To get through, you need to type in the terminal sudo cat /var/lib/jenkins/secrets/initialAdminPassword
.
Copy the password and paste it.
Once you are in, you should then see this
You can select either but for now, I chose Install suggested plugins
and I also installed
- NodeJS
- Office 365 Connector
- Amazon EC2 Instance
There is a couple more steps to setup NodeJs but they are very short.
- Go to Manage Jenkins
- Then go to Global Tool Configuration
- Scroll down to NodeJS and click it
Add NodeJS
if you don't have one and give it a name with the version you want to use - I'm using NodeJS version 13.3.0 as this works with my app folder
That's it for NodeJS.
- Add the url so it should look like this `http://jenkins-ip:8080/github-webhook/
- Go onto Jenkins
- Select your project
- Select configure
- Scroll down to Build Triggers
- Select Github hook trigger for GITScm polling
- Now your webhook is fully configured
So we want to create a dev branch and work from there
- Jenkins
- Go to Configure
- Scroll down to Source Code Management
- On branches to build, you want it to listen to your dev branch so change main to dev
- Scroll down to Add post-build action, and add Git publisher
- Tick Push Only If Build Succeeds and Merge Results
- Add branch
- Branch to push:
refs/heads/main
- Target remote name:
origin
Then once you set this up, test it by:
- Go to your new branch
- Add a random file in that branch
- Add it, commit it and push it.
- Then go to Jenkins and watch it finish
- You can go to console output to see if everything looks good
- Go to Github and select your main branch and see if the file is there
If everything is there, Well Done!
You now successfully automated merging dev and main branch together.
So now that you got the dev and main branch merged together, lets get that connected to EC2.
- You want to now create a new job and name it
something-cd
. - Go to
Source Code Management
- Add your repo via ssh address with the correct credentials
- Select the main branch as that has the merged dev branch from the last job
- Go to
Build Triggers
- Select
Build after other projects are built
Projects to watch
and add your CI job- Tick
Trigger only if build is stable
- Select
- Go to build and add
Execute shell
- Transfer all the files you need to the EC2 Instances
scp -v -r -o StrictHostKeyChecking=no app/ ubuntu@ec2-ip:/home/ubuntu/
- SSH into the EC2 machine
ssh -A -o "StrictHostKeyChecking=no" ubuntu@ec2-ip << EOF
- Update and Upgrade
- Give the right priviledges to the script file if you have one then run the script.
Here is how to reset Jenkins Admin Password
First, you want to create a backup copy of Jenkins config file as after resetting the Jenkins admin password, we need to restore the previous settings
cp /var/lib/jenkins/config.xml /var/lib/jenkins/config.xml.back
Open the config file at /var/lib/jenkins/config.xml
and disable the security
<useSecurity>false</useSecurity>
Restart Jenkins service - may require sudo
systemctl restart jenkins
Now go to Jenkins UI and reset the admin password
- Go to
Manage Jenkins
->Security
->Configure Global Security
->Authentication
- Select
Security Realm
and click onSave
- Go to
People
-> Click on a username for which you want to change the password -admin
->Configure
-> Enter a new password in thePassword
andConfirm password
fields and click onSave
Once the admin password is reset, restore the previous /var/lib/jenkins/config.xml
file and restart Jenkins - May require sudo
mv /var/lib/jenkins/config.xml.back /var/lib/jenkins/config.xml
systemctl restart jenkins