DevOps Jenkins Setup

DevOps Jenkins Setup: DevOps Jenkins is a tool which is used to integrate and deploy the code between multiple applications. It integrates the code from Git repository and deploys through cloud services into another application. First, you have to download and install Jenkins. Follow the installation process as shown below. 1)Give the “initial admin password”.
While installing your Jenkins you will see a window as shown below
2) After installation configure your username and password. Set up a build pipeline with Jenkins and Amazon ECS: Configuring the Jenkins with AWS is a major step in CI/CD. Let’s see how to configure the built pipeline with using Jenkins and Amazon EC2. We will be using a sample Python application, available on GitHub. The repository contains a simple Dockerfile that uses a python base image and runs our application.
FROM python:2-onbuild CMD [ “python”, “./application.py” ]
In order to push the code into repository, the build pipelines create a Docker image using of Dockerfile. Set up the build environment: In the next process using of Amazon Linux AMI launch the Amazon EC2 instance and install and configure the required packages. Install and configure Jenkins, Docker and Nginx: First, you just update your repositories and install Docker, Nginx, and Git.
# yum update –y # yum install -y docker nginx git
In order to install Jenkins on Amazon Linux, we have to add the Jenkins repository and install Jenkins from there.
# wget -O /etc/yum.repos.d/jenkins.repo:  http://pkg.jenkins-ci.org/redhat/jenkins.repo # rpm –import: http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key # yum install Jenkins
Jenkins typically uses port TCP/8080, we will configure Nginx as a proxy. Edit the Nginx config file (/etc/nginx/nginx.conf) and change the server configuration as shown below:
server { listen       80; server_name  _; location / { proxy_pass http://127.0.0.1:8080; } }
We will be using Jenkins to build our Docker images, so we have to add the jenkins user to the docker group. A reboot is required for these changes to take effect.
# usermod -a -G docker Jenkins
Start the Docker, Jenkins and Nginx services and make sure those are running after a reboot:
# service docker start # service jenkins start # service nginx start # chkconfig docker on # chkconfig jenkins on # chkconfig nginx on
We can launch the Jenkins instance complete with all the required plugins with this CloudFormation template. Point your browser to the public DNS name of your EC2 instance and you should be able to see the Jenkins home page:
The Jenkins installation is currently accessible through the Internet without any authentication. Before going to the next level Jenkins must be secured. Select Manage Jenkins on the Jenkins home page, click Configure Global Security and then enable Jenkins security by selecting the Enable Security checkbox.
Once this is complete, save your changes. Now you will be asked to provide a username and password for the user to log in. Click on Create an account, provide your username – i.e. admin – and fill in the user details. Now you will be able to log in securely to Jenkins. Install and configure Jenkins plugins: The final step in setting up our build environment is to install and configure the Jenkins plugins required to build a Docker image and publish it to a Docker registry. We need a plugin to interact with the code repository of our choice, GitHub in our case. On Jenkins dashboard select Manage Jenkins and click Manage Plugins. go to tab, search for and select the following plugins: Docker Build and Publish plugin dockerhub plugin Github plugin Then click the Install button. After the plugin installation is completed, select Manage Jenkins from the Jenkins dashboard and click Configure System. Look for the Docker Image Builder section and fill in your Docker registry (DockerHub) credentials:
Configure the Jenkins build Go to Jenkins dashboard, click on New Item, select the Freestyle project job, add a name for the job, and click OK. Configure the Jenkins job: Under GitHub Project, add the path of your GitHub repository – e.g. https://github.com/awslabs/py-flask-signup-docker. In addition to the application source code, the repository contains the Dockerfile used to build the image, as explained at the beginning of this walkthrough. Under Source Code Management provide the Repository URL for Git, e.g. https://github.com/awslabs/py-flask-signup-docker. In the Build Triggers section, select Build when a change is pushed to GitHub. In the Build section, add a Docker build and publish steps to the job and configure it to publish to your Docker registry repository (e.g. DockerHub) and add a tag to identify the image (e.g. v_$BUILD_NUMBER).
The Repository Name specifies the name of the Docker repository where the image will be published; This is composed(if not a technical term then replace with “consist”) of a username (dstroppa) and an image name (flask-signup). In our case, the Dockerfile sits in the root path of our repository, so we won’t specify any path in the Directory Dockerfile is in the field. Note, the repository name needs to be the same as what is used in the task definition template in flask-signup.json. Add an Execute Shell step and add the ECS CLI commands to start a new task on your ECS cluster.
The below script is Execute shell step.
#!/bin/bash SERVICE_NAME=”flask-signup-service” IMAGE_VERSION=”v_”${BUILD_NUMBER} TASK_FAMILY=”flask-signup” # Create a new task definition for this build sed -e “s;%BUILD_NUMBER%;${BUILD_NUMBER};g” flask-signup.json > flask-signup-v_${BUILD_NUMBER}.json aws ecs register-task-definition –family flask-signup –cli-input-json file://flask-signup-v_${BUILD_NUMBER}.json # Update the service with the new task definition and desired count TASK_REVISION=`aws ecs describe-task-definition –task-definition flask-signup | egrep “revision” | tr “/” ” ” | awk ‘{print $2}’ | sed ‘s/”$//’` DESIRED_COUNT=`aws ecs describe-services –services ${SERVICE_NAME} | egrep “desiredCount” | tr “/” ” ” | awk ‘{print $2}’ | sed ‘s/,$//’` if [ ${DESIRED_COUNT} = “0” ]; then DESIRED_COUNT=”1″ fi aws ecs update-service –cluster default –service ${SERVICE_NAME} –task-definition ${TASK_FAMILY}:${TASK_REVISION} –desired-count ${DESIRED_COUNT}
To trigger the build process on Jenkins upon pushing to the GitHub repository we have to configure a service hook on GitHub. Go to the GitHub repository settings page, select Webhooks and Services and add a service hook for Jenkins (GitHub plugin). Add the Jenkins hook url: http://:@/github-webhook/.
Next, we have to configure a Jenkins job in such a way that whenever a change is committed in GitHub repository it will trigger the build process on Jenkins. From your local repository, push the application code to GitHub:
# git add * # git commit -m “Kicking off Jenkins build” # git push origin master
This will trigger the Jenkins job. After the job is completed, point your browser to the public DNS name for your EC2 container instance and verify that the application is running correctly. Summary: In this walkthrough, we witness how Jenkins is used to automating the deployment of an ECS service.
Nitesh

Nitesh

Author

Bonjour. A curious dreamer enchanted by various languages, I write towards making technology seem fun here at Asha24.