Skip to content

This repository is an attempt to create a jenkins tutorial to setup jenkins with docker , and automate other tasks like executing ansilbe, setup gitlab server, create pipeline scripts and configure remote hosts with jenkins

Notifications You must be signed in to change notification settings

shakyav/jenkins_docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Jenkins setup with docker

Install docker on fedora

  sudo dnf -y install dnf-plugins-core
  • add repo

    sudo dnf config-manager \
      --add-repo \
      https://download.docker.com/linux/fedora/docker-ce.repo
    
  • install docker

     sudo dnf install docker-ce docker-ce-cli containerd.io
    
  • start docker service

     sudo systemctl start docker
    
  • verify installation

     docker run hello-world
    
  • run following command if getting permission denied for "docker run hello-world"

     sudo chmod 666 /var/run/docker.sock
    

Install docker-compose

Create a docker container for jenkins with docker running inside jenkins container

  • create the Docker file to build jenkins container with docker installed and blue ocean plugin

     FROM jenkins/jenkins:lts-jdk11
    
     USER root
    
    
     RUN apt-get update && apt-get install -y apt-transport-https \
            ca-certificates curl gnupg2 \
            software-properties-common
     RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
     RUN apt-key fingerprint 0EBFCD88
     RUN add-apt-repository \
            "deb [arch=amd64] https://download.docker.com/linux/debian \
            $(lsb_release -cs) stable"
     RUN apt-get update && apt-get install -y docker-ce-cli
    
     RUN groupadd docker
    
     RUN usermod -aG docker jenkins
    
     USER jenkins
    
     RUN jenkins-plugin-cli --plugins "blueocean:1.24.6 docker-workflow:1.26"
    
  • docker-compose.yml file to build the dockerized jenkins contianer with docker and blueocean plugin

     version: '3.8'
     networks:
            jenkins-network:
                name: jenkins
     volumes:
            data:
                name: jenkins-data
            certs:
                name: jenkins-docker-certs
     services:  
            dind:
                container_name: jenkins-docker
                image: docker:dind
                privileged: true
                restart: unless-stopped
                networks:
                   jenkins-network:
                          aliases:
                              - docker
                ports:
                   - 3000:3000
                volumes:
                   - data:/var/jenkins_home
                   - certs:/certs/client
                environment:
                   - DOCKER_TLS_CERTDIR=/certs
    
            jenkins:
                container_name: jenkins
                image: jenkins-ansible
                build:
                   context: jenkins-ansible          #this is the path to the directory where I have my Dockerfile stored
                restart: unless-stopped
                networks:
                   - jenkins-network
                ports:
                   - '8080:8080'
                   - '50000:50000'
                   - '3000:3000'
                volumes:
                   - data:/var/jenkins_home
                   - certs:/certs/client:ro
                environment:
                   - DOCKER_HOST=tcp://docker:2376
                   - DOCKER_CERT_PATH=/certs/client
                   - DOCKER_TLS_VERIFY=1
    
  • run the "docker-compose build" commmand to build the images and then "docker-compose up -d" to bring up the containers

  • execute "docker ps" to get the list of srunning containers

  • to verify if the docker is runnnig inside the container , execute "docker exec -it <container_name> bash" to get inside the container shell and execute "docker run hello-world" . If the command is a success then docker daemon is running.

Create CI/CD pipeline for simple NodeJs application

  • create a pipeline project , dashboard -> newitem -> <your_peipeline_name> -> select pipeline from the list -> click OK

    pipeline_create

  • configure the pipeline as shown below, provide the url of the repository for which you want to automate , also mkae sure you have the Jenkinsfile present in the repository to finish the build with success, otherwise create a new Jenkinsfile and commit to the repository

    pipeline_config

  • save the pipeline config and click build now to trigger the pipeline, can also set build triggers using github webhooks and SCM polling which will trigger the pipeline if there's any change detected in the repository . I forked this repo for testing purpose, simple-node-js-react-npm-app

  • refer this repo for Jenkinsfile used for simple nodejs and react app , https://github.com/shakyav/simple-node-js-react-npm-app

  • Once the pipeline is triggered you can view the output as hown below

    pipeline_build

  • You can also view the console output as shown below

    pipeline_console_output

  • Or you can aslo view the pipeline with blueocean plugin as well if installed

    blueocean_pipeline

Create docker container with jenkins and ansible

  • create a directory jenkins-ansible inside jenkns-data and create a Dockerfile inside jenkins-ansible

  • Dockerfile for jenkins container with ansible installed, need root permissions to install ansible hence we used "USER root" inside docker file and then executed the apt-get update and install ansible command and switched back to USER jenkins

     FROM jenkins/jenkins
    
     USER root
    
     RUN apt-get update && apt-get -y install ansible
    
     USER jenkins
    
  • docker-compose file to spin up docker container with jenkins and ansible, volumes section specify that all the data that you have inside jenkins_home directory on your local machine will be reflected in the /var/jenkins_home directory inside the jenkins docker container

     #docker compose file to spin a jenkins container only
    
     version: '3'
     services:
       jenkins:
          container_name: jenkins
          image: jenkins-ansible
          build:
          context: jenkins-ansible
          ports:
          - "8080:8080"     #container port 8080 will be forwarded to the localhost port 8080, localhost_port:container_port
          volumes:
          - $PWD/jenkins_home:/var/jenkins_home
          networks:
          - net
     networks:
       net: 
    

Create docker container with jenkins image

  • create a directory jenkins-data where we will store all the jenkins related files

     mkdir jenkins-data
    
  • create docker-comapose.yml file to build up jenkins container

     #docker compose file to spin a jenkins container only
    
     version: '3'
     services:
       jenkins:
          container_name: jenkins
          image: jenkins/jenkins
          ports:
          - "8080:8080"
          volumes:
          - $PWD/jenkins_home:/var/jenkins_home
          networks:
          - net
     networks:
       net: 
    
  • execute "docker-compose build" to build the container and "docker-compose up -d" to start the container

  • verify docker containers running, "docker ps" command lists all the docker containers running

  • login to the container shell , "docker exec -it jenkins bash" command will ley you acces the shell inside the container, type "exit" to come out

  • access jenkins UI at http://localhost:8080 , setup the jenkins admin as prompted by the jenkins UI

  • install suggested plugins

Jenkins integration with Gmail

  • install the Mailer Plugin if not already installed, go to Manage Jenkins -> Manage Plugins -> type mailer in search bar under available plugin, if not found then check in installed plugins

  • follow these steps to allow gmail to send emails

     1. sing-in to gmail account >> navigate to settings >> privacy and security settings
    
     2. setup two step verification settings (because without two step verification we cannot generate application specific password)
    
     3. after setting up two step verification setting in gmail account navigate back to security and privacy settings
    
     4. click on application specific password >> give the name of the application in the drop down as Jenkins (google by default does not have any specific  application password setting for Jenkins) >> this will generate password note down the password generated
    
  • after installing the plugin go to Manage Jenkins -> configure System -> Email Notification , configure as shown in following screen

emailintegration

Create jenkins job to run a shell scripit with parameters

  • after login to jenkins go to , dashboard -> new item -> enter the job name -> freestyle project -> click OK to save

    login newitem

  • now you'll be redirected to the configure screen

    configurejob

  • execute bash scripts with parameters from jenkins

  • create bash script in local machine

  • copy the script to docker container , container_name:file location wher you want to copy, sample script.sh file is provided in the repository

     docker cp script.sh jenkins:/tmp/script.sh
    
  • add prameters as shown in the following screen, I selected the choice(list) parameter and string parameter

    addparam

  • choice(list) parameter

    choice

  • string parameter, can set default value for this

    string

  • add the shell script in the build step

    executeshell

Note: if jenkins user doesn't have permissions to execute scripts then grant required permissions using chmod command

  • Save the job and go back to the , dashboard -> your job -> click "Build with Parameters" , if you have no parameters setup this button will simply be "build Now"

  • click on the job id -> console output , to view results

    consoleoutput

About

This repository is an attempt to create a jenkins tutorial to setup jenkins with docker , and automate other tasks like executing ansilbe, setup gitlab server, create pipeline scripts and configure remote hosts with jenkins

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published