All Projects → vineet68sharma → CI-CD-Docker

vineet68sharma / CI-CD-Docker

Licence: other
No description or website provided.

Programming Languages

java
68154 projects - #9 most used programming language
HTML
75241 projects
Dockerfile
14818 projects
shell
77523 projects

Projects that are alternatives of or similar to CI-CD-Docker

fire-cloud
基于Spring Cloud的微服务业务框架
Stars: ✭ 16 (-23.81%)
Mutual labels:  jenkinsfile, jenkins-pipeline
ods-jenkins-shared-library
Shared Jenkins library which all ODS projects & components use - provisioning, SonarQube code scanning, Nexus publishing, OpenShift template based deployments and repository orchestration
Stars: ✭ 51 (+142.86%)
Mutual labels:  sonarqube, jenkins-pipeline
jenkins-blue
Quickly provision jenkins blue ocean on kubernetes with persistent configuration
Stars: ✭ 20 (-4.76%)
Mutual labels:  jenkinsfile, jenkins-pipeline
jenkinz
JenkinZero (jenkinz) is a zero configuration Jenkinsfile validation tool.
Stars: ✭ 13 (-38.1%)
Mutual labels:  jenkinsfile, jenkins-pipeline
eryajf.github.io
📝 大千世界,何其茫茫。谨此笔记,记录过往。凭君阅览,小站洛荒。如能收益,莫大奢望
Stars: ✭ 159 (+657.14%)
Mutual labels:  jenkinsfile, jenkins-pipeline
learn-ansible-and-jenkins-in-30-days
Ansible + Jenkins in 30 days tutorial.
Stars: ✭ 35 (+66.67%)
Mutual labels:  jenkinsfile, jenkins-pipeline
openshift-wiki
Gitbook URL of WIKI
Stars: ✭ 16 (-23.81%)
Mutual labels:  jenkinsfile, jenkins-pipeline
jenkins-pipeline-shared-library-template
Project template for developing shared Jenkins pipeline libraries.
Stars: ✭ 46 (+119.05%)
Mutual labels:  jenkinsfile, jenkins-pipeline
slackbridge
Bridge between IRC and Slack running on Kubernetes
Stars: ✭ 12 (-42.86%)
Mutual labels:  jenkinsfile
ForgeModdingSkeleton
Skeletons for building Forge mods
Stars: ✭ 21 (+0%)
Mutual labels:  sonarqube
advanced-spring-scaffold
This project provides an advanced baseline to help you kick start a Spring project.
Stars: ✭ 21 (+0%)
Mutual labels:  sonarqube
sonar-auth-gitlab-plugin
Use GitLab OAuth login in SonarQube login page
Stars: ✭ 97 (+361.9%)
Mutual labels:  sonarqube
jenkinsexample
Jenkins example setup discussed on thoughts-on-cpp
Stars: ✭ 18 (-14.29%)
Mutual labels:  jenkinsfile
terraform-provider-sonarqube
Terraform provider for managing Sonarqube configuration
Stars: ✭ 26 (+23.81%)
Mutual labels:  sonarqube
sonar-cobertura
Cobertura plugin for SonarQube
Stars: ✭ 30 (+42.86%)
Mutual labels:  sonarqube
softvis3d
No description or website provided.
Stars: ✭ 55 (+161.9%)
Mutual labels:  sonarqube
go-microepoch
A complete DevOps cycle for Building and Deploying a Go Application to Kubernetes cluster.
Stars: ✭ 34 (+61.9%)
Mutual labels:  devops-pipeline
sonar-scanner-ant
SonarQube Scanner for Ant
Stars: ✭ 13 (-38.1%)
Mutual labels:  sonarqube
python-sonarqube-api
Python wrapper for the SonarQube (Community Edition and Enterprise Edition) and SonarCloud API.
Stars: ✭ 107 (+409.52%)
Mutual labels:  sonarqube
jenkins-pipeline-global-library-chefci
Jenkins Pipeline's "Workflow Global Libs" for Chef CI
Stars: ✭ 60 (+185.71%)
Mutual labels:  jenkins-pipeline

Automated CI/CD using Docker and Jenkins

This repository is a tutorial it tries to exemplify how to automatically manage the process of building, testing with the highest coverage, and deployment phases.

Our goal is to ensure our pipeline works well after each code being pushed. The processes we want to auto-manage:

  • Code checkout
  • Run tests
  • Compile the code
  • Run Sonarqube analysis on the code
  • Create Docker image
  • Push the image to Docker Hub
  • Pull and run the image

First step, running up the services

Since one of the goals is to obtain the sonarqube report of our project, we should be able to access sonarqube from the jenkins service. Docker compose is a best choice to run services working together. We configure our application services in a yaml file as below.

docker-compose.yml

version: '3.2'
services:
  sonarqube:
    build:
      context: sonarqube/
    ports:
      - 9000:9000
      - 9092:9092
    container_name: sonarqube
  jenkins:
    build:
      context: jenkins/
    privileged: true
    user: root
    ports:
      - 8080:8080
      - 50000:50000
    container_name: jenkins
    volumes:
      - /tmp/jenkins:/var/jenkins_home #Remember that, the tmp directory is designed to be wiped on system reboot.
      - /var/run/docker.sock:/var/run/docker.sock
    depends_on:
      - sonarqube

Paths of docker files of the containers are specified at context attribute in the docker-compose file. Content of these files as follows.

sonarqube/Dockerfile

FROM sonarqube:6.7-alpine

jenkins/Dockerfile

FROM jenkins:2.60.3

If we run the following command in the same directory as the docker-compose.yml file, the Sonarqube and Jenkins containers will up and run.

docker-compose -f docker-compose.yml up --build
docker ps

CONTAINER ID        IMAGE                COMMAND                  CREATED              STATUS              PORTS                                              NAMES
ca8f0b63469f       pipeline_jenkins     "/bin/tini -- /usr..."   About a minute ago   Up About a minute   0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp   jenkins
f5bed5ba3266        pipeline_sonarqube   "./bin/run.sh"           About a minute ago   Up About a minute   0.0.0.0:9000->9000/tcp, 0.0.0.0:9092->9092/tcp     sonarqube

GitHub configuration

We’ll define a service on Github to call the Jenkins Github webhook because we want to trigger the pipeline. To do this go to Settings -> Integrations & services. The Jenkins Github plugin should be shown on the list of available services as below.

After this, we should add a new service by typing the URL of the dockerized Jenkins container along with the /github-webhook/ path.

The next step is that create an SSH key for a Jenkins user and define it as Deploy keys on our GitHub repository.

If everything goes well, the following connection request should return with a success.

ssh [email protected]
PTY allocation request failed on channel 0
Hi <your github username>/<repository name>! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

Jenkins configuration

We have configured Jenkins in the docker compose file to run on port 8080 therefore if we visit http://localhost:8080 we will be greeted with a screen like this.

We need the admin password to proceed to installation. It’s stored in the /var/jenkins_home/secrets/initialAdminPassword directory and also It’s written as output on the console when Jenkins starts.

jenkins      | *************************************************************
jenkins      |
jenkins      | Jenkins initial setup is required. An admin user has been created and a password generated.
jenkins      | Please use the following password to proceed to installation:
jenkins      |
jenkins      | 45638c79cecd4f43962da2933980197e
jenkins      |
jenkins      | This may also be found at: /var/jenkins_home/secrets/initialAdminPassword
jenkins      |
jenkins      | *************************************************************

To access the password from the container.

docker exec -it jenkins sh
/ $ cat /var/jenkins_home/secrets/initialAdminPassword

After entering the password, we will download recommended plugins and define an admin user.

After clicking Save and Finish and Start using Jenkins buttons, we should be seeing the Jenkins homepage. One of the seven goals listed above is that we must have the ability to build an image in the Jenkins being dockerized. Take a look at the volume definitions of the Jenkins service in the compose file.

- /var/run/docker.sock:/var/run/docker.sock

The purpose is to communicate between the Docker Daemon and the Docker Client(we will install it on Jenkins) over the socket. Like the docker client, we also need Maven to compile the application. For the installation of these tools, we need to perform the Maven and Docker Client configurations under Manage Jenkins -> Global Tool Configuration menu.

We have added the Maven and Docker installers and have checked the Install automatically checkbox. These tools are installed by Jenkins when our script(Jenkins file) first runs. We give myMaven and myDocker names to the tools. We will access these tools with this names in the script file.

Since we will perform some operations such as checkout codebase and pushing an image to Docker Hub, we need to define the Docker Hub Credentials. Keep in mind that if we are using a private repo, we must define Github credentials. These definitions are performed under Jenkins Home Page -> Credentials -> Global credentials (unrestricted) -> Add Credentials menu.

We use the value we entered in the ID field to Docker Login in the script file. Now, we define pipeline under Jenkins Home Page -> New Item menu.

Also Dont forget to mark env variables for Docker username and Docker password .

In this step, we select GitHub hook trigger for GITScm pooling options for automatic run of the pipeline by Github hook call.

Also in the Pipeline section, we select the Pipeline script from SCM as Definition, define the GitHub repository and the branch name, and specify the script location ([https://github.com/vineet68sharma/CI-CD-Docker/blob/master/Jenkinsfile)).

Create a repository in docker hub so the built image is pushed to docker registry with its credentials (need to be filled in jenkinsFile)

After that, when a push is done to the remote repository or when you manually trigger the pipeline by Build Now option, the steps described in Jenkins file will be executed.

After that, when a Deployment is done to the remote repository or when you manually trigger the pipeline by Build Now You can see the Workflow step by step .

After Deployment is done and Container is running , You can check it by URL and Port number on which the service is running

http://ec2-50-16-10-93.compute-1.amazonaws.com:8090/

Review important points of the Jenkins file

stage('Initialize'){
    def dockerHome = tool 'myDocker'
    def mavenHome  = tool 'myMaven'
    env.PATH = "${dockerHome}/bin:${mavenHome}/bin:${env.PATH}"
}

The Maven and Docker client tools we have defined in Jenkins under Global Tool Configuration menu are added to the PATH environment variable for using these tools with sh command.

stage('Push to Docker Registry'){
    withCredentials([usernamePassword(credentialsId: 'dockerHubAccount', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
        pushToImage(CONTAINER_NAME, CONTAINER_TAG, USERNAME, PASSWORD)
    }
}

withCredentials provided by Jenkins Credentials Binding Plugin and bind credentials to variables. We passed dockerHubAccount value with credentialsId parameter. Remember that, dockerHubAccount value is Docker Hub credentials ID we have defined it under Jenkins Home Page -> Credentials -> Global credentials (unrestricted) -> Add Credentials menu. In this way, we access to the username and password information of the account for login.

Sonarqube configuration

For Sonarqube we have made the following definitions in the pom.xml file of the project.

<sonar.host.url>http://sonarqube:9000</sonar.host.url>
...
<dependencies>
...
    <dependency>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sonar-maven-plugin</artifactId>
        <version>2.7.1</version>
        <type>maven-plugin</type>
    </dependency>
...
</dependencies>

In the docker compose file, we gave the name of the Sonarqube service which is sonarqube, this is why in the pom.xml file, the sonar URL was defined as http://sonarqube:9000.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].