Skip to content

yewin-mm/jenkins-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jenkins-server

Contributors Forks Stargazers Issues LinkedIn

Jenkins CI Server (photo from Google)

jenkins-server

  • This is a guide to create Jenkins server with Docker compose.
  • You can check out step-by-step guide in Instruction and Setting up sections.

Table of Contents

⚡️About The Project

This is the guide to setup Jenkins Server with docker compose.
Jenkins help to automate the software for building, testing, deploying, Continuous Integration and Continuous Delivery (CI/CD).
There are many ways to install Jenkins Server like installer, install with docker, install with brew (MacOs).
Below guild is to install Jenkins Server with Docker.
You can see sample Jenkins Pipeline projects as Spring Boot Jpa Docker Jenkins Pipeline application for using this Jenkins Server.

🪓 Built With

This project is built with

🔥 Getting Started

See the Prerequisites sections for basic knowledge and go as per below Instruction section.

🔔 Before you begin

If you are new in Git, GitHub and new in Spring Boot configuration structure,
You should see basic detail instructions first in here Spring Boot Application Instruction
If you are not good enough in basic Docker Compose commands, you can learn some commands in below projects.
Click below links.

🥡 Clone Project

  • Clone the repo
    git clone https://github.com/yewin-mm/jenkins-server.git

🔑 Prerequisites

Prerequisites can be found here, Spring Boot Application Instruction.
You need to install Docker in your machine. Get Docker.
If you don't know about basic docker command, see here. Spring Boot Docker Sample
You can learn sample docker-compose commands here. Spring Boot Docker Compose Sample.
You can see sample Jenkins Pipeline projects as Spring Boot Jpa Docker Jenkins Pipeline application for using this Jenkins Server.

📝 Instruction

  • Make sure docker is running in your machine.
  • Go to your cloned folder (clone this repo) with cmd (for window) or terminal (for Mac and Linux).

Run Jenkins Server

  • Below commands need to type in your CMD or Terminal where path should be in your application folder.

  • Before you run below docker-compose up -d,

    • You need to check your db.
    • If you run your database like this ways Run MySQL DB as Container
      it's ok, you can skip below steps and type below up command under Run Jenkins Server title.
    • Because I run MySQL as container and give network name as mysql, so it's ok to connect to my MySQL db container which running with network `mysql.
    • So, if you run your db as container and gave network name not like mysql you need to change that your network name in my docker-compose file.
    • If not change your network name, it will get error.
    • Because if you run Jenkins Server as container, that container can't connect to DB container if those containers are not going under same network.
    • So, you need to run your Jenkins Server as same network with your DB container.
    • But if you run your database as normal (eg. run with installer), you need to change a bit in this Docker compose file.
    • Open your cloned docker-compose.yml file with text editor like sublime text or notepad++, etc.
    • Comment out - mysql under networks tag under services section.
    • Uncomment jenkins, aliases and - docker
    • Comment out - mysql, external: true under networks section (which is below in that file).
    • Uncomment jenkins, driver and name.
    • After that, Save the file and type below up command.
  • Run Jenkins Server (with port 9000)

    • docker-compose up -d
    • After that you can call jenkins server portal with http://localhost:9000/ through browser. (if you run jenkins at vm server, you need to add server ip or domain instead of localhost).

Unlock Jenkins

  • After you get in jenkins server portal through browser by http://localhost:9000/,
  • You will see below unlock screen to unlock jenkins server.

Unlock Jenkins

  • Type below command in CMD or Terminal.
    • docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
  • Then you will get initial password like below

Sample initial password

  • Copy initial password which appeared in your CMD or Terminal by typing above command.
  • Paste in jenkins server portal to unlock jenkins server.
  • Please note that initial password is no need to use in the future. It's need only one time (this time).

Install Suggested Plugins

  • After you've unlocked jenkins server portal,
  • Choose Install Suggested Plugins as below picture.
  • This step will take a few minutes depend on your internet connection.
  • If anything failed, you can retry download plugins or if you saw error in home page,
    you can type docker compose down and clear volume Jenkins volume by typing docker volume rm jenkins-data and docker volume rm jenkins-docker-certs and type docker compose up -d --build again.
    Please note that deleting Jenkins volume will clear all Jenkins data and so, if you can install plugins successfully or if anything is not wrong or if no need to clear data, don't delete volume.

Install Suggested Plugins

Create Admin User

  • After you've installed suggested plugins in jenkins server portal,
  • Create admin user and you can give user name as you want.

Sample Create Admin User

Configure Portal Access Link

  • After you've created admin user,
  • You can see Instance Configuration wizard like below.

Configure Link

  • Just click Save and Finish button to go with localhost:9000 which port I put in docker compose file.
  • After that, Click Start using Jenkins.
  • After that, you can see the Jenkins server portal like below.

Sample Jenkins Server Portal Dashboard.

  • Finally, you can successfully set up Jenkins Server with docker.

Run Script

  • This part is important after you shut down or restart your computer or restart docker desktop daemon.

  • Because after your restart your computer or decker daemon, the connection for docker daemon is lost, and so, you won't able to run docker login in pipeline script.

  • It will get permission denied errors.

  • Go got Jenkins Container by below command.

  • Type

      docker exec -u root -it jenkins bash 
  • After you get inside jenkins container by root user,

  • Type

      sh script.sh
  • Then, Type exit to leave Jenkins container.

Setting Up

  • You can add tools, pre-defined credentials in Jenkins Server by below.
  • In my case, I need maven to build my applications and docker hub credentials to accessing docker hub for pushing, pulling images (here, that could be any image repository instead of docker hub).

Add Maven Tools and Plugins

  • You can add maven tools to build your applications by using Maven.
  • There are 3 ways to use maven with Jenkins
    1. you can use docker maven image as agent (this way need to install docker cli, but you can use different maven version as you want and can use share volume storage which not to download dependency every time)
    2. you can use maven as tools and install in Jenkins (this way is easy and with that way, you need to use only specific maven version which you installed in Jenkins)
    3. you can link to dockerfile which pull maven image from docker hub. (this way is a bit hard to handle for beginners)

Add Maven Tools
  • Here, I used way 2 as that is easy and understandable
  • In Jenkins Server Portal,
  • Go to Manage Jenkins sub menu -> Global Tool Configuration -> under Maven section, add like below picture

    Add Maven Tools as global

  • Please note that, you need to start with Capital letter in name field like Maven-{any}
  • After that Click save.

Add Maven Plugins
  • You need below three maven plugins to use maven in your pipeline.

  • Go to Manage Jenkins sub menu -> Manage Plugins -> under Available Plugins section,

    • Search Maven Integration, Pipeline Maven Integration and Pipeline Utility Steps (for reading pom file)
    • Select those plugins and click Install without restart.

    Search Maven Plugins

  • Click Restart Jenkins when installation is complete and no jobs are running to apply plugins.

Add Maven Plugins

  • This step take a few minutes depend on your internet connection.
  • If downloading plugins was success and not able to restart Jenkins server (if too long), you can restart docker desktop or compose down and up to restart Jenkins by manually.

Add Docker Hub Credentials and Plugins

  • You can add cloud image repository like Docker Hub credentials as pre-defined to upload your application image.
  • Here, image repository can be anything like your cloud server, Docker Hub, etc.
  • Here, I will test with Docker Hub.

Generate Access Token
  • Go to Docker Hub cloud and login with your account. Docker-Hub
  • Go to Account Setting under your profile.
  • Click Security tag and click New Access Token.
  • Fill the name as you want in Access Token Description field. You can give only read and write permission if your pipeline don't want to delete image in docker hub.
  • After that Click Generate.

Generate Access Token

  • You can see generate Token and click Copy and Close.

    Access Token Sample

  • Please note that token will use when you try to connect your docker hub and so, it's better saving in your notepad.

  • You can delete and creat new token as you want. Please don't share your token to others as high security risk.

Add Docker Hub Credentials in Jenkins
  • You need to add global credentials to access your cloud repository from Jenkins pipelines.
  • Go to Manage Jenkins sub menu -> Manage Credentials -> click (global) under domain -> click Global credentials (unrestricted) -> Add Credentials

Add Global Credentials

  • Add credentials like below,
  • Here, username will be your docker hub username and password will be docker hub token that you already generate in above step. ID will be used in pipeline script.

Add Docker Hub Credentials in Jenkins

  • After that click create.
  • Here, you can change your private cloud repository if you have and I showed with docker hub public cloud as demo.
  • Here, you can add other credentials like Github if your project is private. Because if private repository, Jenkins pipelines won't be pull without credentials.

Add Docker Plugins
  • You need to install Docker plugins in Jenkins Server to use docker command in pipelines.
  • Go to Manage Jenkins sub menu -> Manage Plugins -> under Available Plugins section,
    search Docker Pipeline and select that plugin and click Install without restart.

Search Docker Plugins

  • Click Restart Jenkins when installation is complete and no jobs are running to apply plugins.

    Add Docker Plugins

  • If downloading plugins was success and not able to restart Jenkins server (if too long), you can restart docker desktop or compose down and up to restart Jenkins by manually.

Create repository to store image
  • Here, I will push my image to Docker hub and so, I need to create repository to store my image in Docker Hub.
  • Go to Docker Hub cloud and login with your account. Docker-Hub
  • Click Repositories tab in above.
  • Give name and click Create.

Create Repository

  • Now, you are done for setting require configuration to run Java Project by using Docker and push to Docker Hub. Here, you can add more plugins as you want.
  • Let's go to Testing in below.

Testing Pipelines

  • You can test pipeline by creating sample pipeline scripts in Jenkins Server.
  • There are two types of pipelines scripts which used groovy languages.
  1. Scripted Pipeline
  2. Declarative Pipeline
  • Here, I will use Declarative Pipeline as it's easy to understand and modern way.

Testing Maven in Pipeline

  • Below are testing for using maven in pipelines

Testing with Maven Tools
  • Click New Item in your Jenkins Dashboard Portal.

  • Enter test-maven-with-maven-tools in field. (you can give any name you want)

  • Choose Pipeline and Click Ok.

    Create Pipeline

  • Copy below scripts and paste under Pipeline Script section.

    pipeline {
      agent any
      tools {
          // you need to add Global Tools Configuration in Jenkins Server.
          // go to Jenkins Portal (Manage Jenkins -> Global Tools Configuration -> Add maven -> give name (ref - https://github.com/yewin-mm/jenkins-server#maven)
          maven 'Maven-3.9.0'
      }
    
      stages {
          stage('Print Maven Version') {
              steps {
                  sh 'mvn -v'
              }
          }
      }
    }

Maven Tools Script Sample

  • Here, tools name maven 'Maven-3.9.0' is need to be same with your Maven tools name which you gave in above Add Maven Tools section.
  • Click Save.
  • After you have entered that above test-maven-with-maven-tools pipeline job, Click Build Now and refresh page.
  • There you will see job is running with build number #1.
  • Click Console Output which can be reach by clicking down arrow button beside #1 job like above picture, to see console output.

Maven Tools Console

  • There you can see pipeline console output and can see maven version which we printed in above script.

Maven Tools Console

  • After that click test-maven-with-maven-tools (top left corner) and you can see pipeline jobs are success with green line.
  • Please note that above sample pipeline script has only one stages call Print Maven Version, you will see only two stages in screen and one is for installing declarative maven tools.

Success Maven Tools Testing

  • After you can go to Dashboard and you can see one pipeline is showing in the dashboard.
  • You can enter that pipeline again and build again, you will see it's running with build number #2.

Testing with Docker Agent
  • Click New Item in your Jenkins Dashboard Portal.

  • Enter test-maven-with-docker-agent in field. (you can give any name you want)

  • Choose Pipeline and Click Ok.

    Create Pipeline

  • Copy below scripts and paste under Pipeline Script section.

    pipeline {
      // run docker as agent to pull maven image from docker hub
      agent {
          docker {
              // use alpine maven image for reducing size and light weight
              image 'maven:3.9.0-eclipse-temurin-11-alpine'
              // mount volume with .m2 folder to reduce repeatative downloading jar file
              args '-v $HOME/.m2:/root/.m2'
          }
      }
      stages {
          stage('Print Maven Version') {
              steps {
                  sh 'mvn -v'
              }
          }
      }
    }

Maven Docker Agent Script

  • Click Save.
  • After you have entered that above test-maven-with-docker-agent pipeline job, Click Build Now and refresh page.
  • There you will see job is running with build number #1.
  • Click Console Output which can be reach by clicking down arrow button beside #1 job like above maven tools step, to see console output.
  • There you can see pipeline console output and can see maven version which we printed in above script.

Maven Docker Agent Console

  • After that click test-maven-with-docker-agent (top left corner) and you can see pipeline jobs are success with green line.
  • Please note that above sample pipeline script has only one stages call Print Maven Version, you will see one stage.

Success Maven Docker Agent Testing

  • After you can go to Dashboard and you can see two pipeline is showing in the dashboard (one is for testing maven with tools and one is for testing maven with docker agent).
  • You can enter these pipeline again and build again to test.

Testing Docker Hub Login

  • Here, we will try to Login to docker hub from jenkins server by using pipeline script.

  • Click New Item in your Jenkins Dashboard Portal.

  • Enter test-docker-login in field. (you can give any name you want)

  • Choose Pipeline and Click Ok.

  • Copy below scripts and paste under Pipeline Script section.

    pipeline {
      environment {
        // that is we already predefined with name `docker-hub` in jenkins Dashboard -> Manage Jenkins -> Credentials (ref - https://github.com/yewin-mm/jenkins-server#docker)
        DOCKER_HUB_CREDENTIALS = credentials('docker-hub')
      }
      agent any
      stages {
        stage('Test Username') {
          steps{
            script {
              // print build number from jenkins
              echo "Jenkins build number: ${env.BUILD_NUMBER}"
              // we already defined DOCKER_HUB_CREDENTIALS variable and USR is provided from jenkins to get username from that credentials
              echo "docker-hub Username: $DOCKER_HUB_CREDENTIALS_USR"
            }
          }
        }
        stage('Test Login to Docker Hub') {         
          steps{
          sh 'echo $DOCKER_HUB_CREDENTIALS_PSW | docker login -u $DOCKER_HUB_CREDENTIALS_USR --password-stdin'
          echo 'Login Success' 
          }     
        }  
      }
    
    }

Docker Login Script

  • Here, credentials id 'docker-hub is the ID which we pre-define in above Add Credentials in Jenkins section.
  • Click Save.
  • After you have entered that above test-docker-login pipeline job, Click Build Now and refresh page.
  • There you will see job is running with build number #1.

Docker Login Console

  • Click Console Output which can be reach by clicking down arrow button beside #1 job like above picture, to see console output.
  • There you can see pipeline console output and can see maven version which we printed in above script.

Docker Login Console

  • Go back to your docker login pipeline, and you will see Test Username and Test Login to Docker Hub with green line.
  • You can go back to dashboard now.

Testing Maven Pipeline Utility

  • Here, we will try to read pom file properties by using Maven Pipeline Utility plugin which plugins we already installed in above.

  • Click New Item in your Jenkins Dashboard Portal.

  • Enter read-pom in field. (you can give any name you want)

  • Choose Pipeline and Click Ok.

  • Copy below scripts and paste under Pipeline Script section.

    // declare variable as global (not declare in script tag) to use in other stages
    def readPom
    pipeline {
      agent any
      tools {
          // you need to add Global Tools Configuration in Jenkins Server.
          // go to Jenkins Portal (Manage Jenkins -> Global Tools Configuration -> Add maven -> give name (ref - https://github.com/yewin-mm/jenkins-server#maven)
          maven 'Maven-3.9.0'
      }
      stages {
          stage('Git Clone') {
              steps {
                  git branch: 'master', url: 'https://github.com/yewin-mm/spring-cloud-config-sample-microservice-a'
              }
          }
          stage('Read pom file') {
              steps {
                  script {
                      // for using readMavenPom, you need to add Pipeline Utility Steps plugin in Jenkins Server (ref - https://github.com/yewin-mm/jenkins-server#maven)
                      readPom = readMavenPom file: 'pom.xml';
                  }
              }
          }
          stage('Print Values') {
              steps {
                  echo "Application Name: ${readPom.name}"
                  echo "Artifact Id: ${readPom.artifactId}"
                  echo "Application Version: ${readPom.version}"
              }
          }
      }
    }
  • Click Save.

  • There, you can use any of your Github projects link to clone.

  • After you have entered that above read-pom pipeline job, Click Build Now and refresh page.

  • You can see console log like above steps and there you will see your application name, artifact id and version which we print in above pipeline script.

  • You can test other pipeline scripts by testing like above.

  • You can also generate pipeline tags by using Pipeline Syntax.

Please Don't Forget to run Script after everytime you start your computer or start docker daemon again as this Run Script section.

Have Fun and Enjoy in Learning Code

✉️ Contact Me

Name - Ye Win
LinkedIn profile - Ye Win
Email Address - yewin.mmr@gmail.com
WhatsApp - +959252656065
Website - My Website

Project Link: Jenkins Server

🥰 Becoming a Sponsor

If you like any of my projects or if you want to support my work, please kindly consider becoming a sponsor.
It gives me great motivation and I can relentlessly maintain my projects and contribute to the open-source community.

Buy Me A Coffee

⭐ Contributing

Contributions are what make the open source community such an amazing place to be learnt, inspire, and create. Any contributions you make are greatly appreciated.
If you want to contribute....

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/yourname)
  3. Commit your Changes (git commit -m 'Add some AmazingFeatures')
  4. Push to the Branch (git push -u origin feature/yourname)
  5. Open a Pull Request

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published