Giter Club home page Giter Club logo

jenkins-pipeline-with-docker's Introduction

Devops pipeline with SonarQube, Jenkins, Nexus and Ansible with all components running on docker

This repo is a easy way to set up an entire devops CI/CD pipeline with nothing other than docker and an ubuntu machine.

The setup is preconfigured to work with npm as the build and package tool. The dockerfiles and ansible playbook are attached for anyone who wants to poke around

All the services are running on docker containers including ansible hosts

Components used

  • Git : Source Code Management
  • Jenkins : Continuous Integration port: 8080
  • SonarQube : Static analysis port: 9000
  • Sonatype nexus : Artifactory port: 8081
  • Ansible cluster : Deployment (1 master, 3 hosts)
  • Docker : Container runtime
  • npm : To build the source code
  • serve : To serve the final application ports: 4002, 4004, 4006 one per ansible host

you should atleast have 20gb free disk space and 8gb of ram since sonatype nexus requires atleast 4gb spare ram


Our setup involves three ansible host containers, individual containers running sonarqube and nexus and another running both ansible and jenkins

This is how you implement:

  • Clone the repo, navigate to the directory pipeline-on-ubuntu, and run the setup.sh script file. Your setup is done, You are just configuring your project away from an up and running pipeline.
    $ git clone <url>

    $ cd pipeline-on-ubuntu/

    $ ./setup.sh
  • Your jenkins and nexus initial passwords will be fetched for you at the end of the execution of the script. Incase the nexus password is not fetched, wait for a moment and run
  $ docker container exec pipelineonubuntu_nexus_1 cat /nexus-data/admin.password

  • Navigate to localhost:9000 → login User: admin Password: admin

    • Under my account → security → generate a security token → copy to clipboard
  • Navigate to localhost:8081 → login (User: admin, password will be fetched by the script)

    • Under server administration and configuration create a a new repository with recipe: raw (hosted)
  • Navigate to localhost:8080 → login (initial admin password will be fetched by the script) and setup Jenkins. Install additional plugins

    • Sonarqube scanner
    • Ansible
    • Gitlab, gitlab hook (Incase you are using gitlab, like we are)
    • Nexus artifact uploader
  • In your gitlab.com account → Settings → SSH Keys → add the public key of a key pair


  • Manage jenkins → Configure system

    • SonarQube servers
      • Enable injection of SonarQube server configuration as build environment variables
      • Add SonarQube →
        • Give a name
        • Server URL: http://172.24.0.4:9000
        • Sonarqube authentication token → Add → kind: secret text → copy the generated SonarQube token → add the cred and use it
    • Gitlab
      • Enable authentication for '/project' end-point (uncheck if checked by deafult)
  • Manage jenkins → Global tool configuration → SonarQube scanner

    • Add sonarqube scanner → Give a name → save

  • New Item → freestyle project → Ok
    • Source Code Management

      • Git
      • Repository URL: ssh clone url
      • Credentials → Add → kind: SSH Username with private key
        • Username: gitlab username
        • Private key → Enter directly → Add → private key of the key pair of which public key is configured in gitlab → Add cred and use it → Save

    • Build triggers

      • Build when a change is pushed to GitLab. GitLab webhook URL: http://localhost:8080/project/_jenkins-project-name_ → Enabled GitLab triggers

        • Push Events
        • Accepted Merge Request Events
        • Approved Merge Requests (EE-only)
        • Comments
      • Navigate to gitlab.com → project repo → Settings → Integrations

        Hook executed successfully: HTTP 200 
        

    • Build → Add build step

      • Execute SonarQube Scanner → Analysis properties
        sonar.projectKey =  < Sonarqube project key >
        sonar.projectName =  < Sonarqube project name >
        sonar.projectVersion =  < Project version >
        sonar.sources =  < path to src/ dir >
        

    • Build → Add build step

      • Execute shell → Command
        npm install
        npm run build
        zip -r build.zip build/
        

    • Build → Add build step → Nexus artifact uploader

      • Nexus Version: NEXUS3
      • Protocol: HTTP
      • Nexus URL: 172.24.0.12:8081
      • Credentials: Add → kind → Username and Password → Nexus username and password → Add and use it
      • GroupId: < nexus group id >
      • Version: < version of code >
      • Repository: < name of the repository that was created in nexus >
      • Artifacts
        • ArtificatId: $BUILD_ID
        • Type: zip
        • Classifier: build
        • File: build.zip

    • Build → Add build step → Invoke Ansible Playbook

      • PlaybookPath: /pb1.yml
      • Inventory → File or host list
        • File path or comma separated host list : /inventory.txt
  • Save

In your ubuntu machine change the name of the jenkins project name in the playbook inside the ansible container

    $ docker container exec -it pipelineonubuntu_jekns-ans-master_1 bash
    # nano /pb1.yml

        ...
        tasks:
          - name: copy zip file
            copy:
              src: /var/jenkins_home/workspace/ <JENKINS PROJECT NAME> /build.zip
        ...
    # exit
  • Jenkins dashboard → project → Build Now (To test)

  • Go to your project directory → make a change → commit → push to git

The pipeline will be triggered by default by the gitlab webhook → the code will undergo static analysis by sonarqube of which the result will be available in sonarqube dashboard → will be built by npm and compressed → stored in nexus → then deployed in the three ansible hosts and served at ports 4002, 4004 and 4006 of localhost

jenkins-pipeline-with-docker's People

Contributors

binums avatar kumarahul98 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.