{"id":13903256,"url":"https://github.com/hakdogan/jenkins-pipeline","last_synced_at":"2025-04-06T07:16:01.960Z","repository":{"id":145613121,"uuid":"111801928","full_name":"hakdogan/jenkins-pipeline","owner":"hakdogan","description":":chart_with_upwards_trend: Learn how to implement container technologies with your Jenkins CI/CD workflows to make them easier to manage in this tutorial.","archived":false,"fork":false,"pushed_at":"2022-10-27T11:50:03.000Z","size":5155,"stargazers_count":102,"open_issues_count":0,"forks_count":373,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-03-30T06:09:44.974Z","etag":null,"topics":["continuous-delivery","continuous-integration","docker","docker-compose","jenkins","jenkins-pipeline"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hakdogan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2017-11-23T11:34:17.000Z","updated_at":"2024-09-14T11:23:09.000Z","dependencies_parsed_at":"2023-06-12T21:00:31.923Z","dependency_job_id":null,"html_url":"https://github.com/hakdogan/jenkins-pipeline","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hakdogan%2Fjenkins-pipeline","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hakdogan%2Fjenkins-pipeline/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hakdogan%2Fjenkins-pipeline/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hakdogan%2Fjenkins-pipeline/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hakdogan","download_url":"https://codeload.github.com/hakdogan/jenkins-pipeline/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247445682,"owners_count":20939961,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["continuous-delivery","continuous-integration","docker","docker-compose","jenkins","jenkins-pipeline"],"created_at":"2024-08-06T22:01:57.290Z","updated_at":"2025-04-06T07:16:01.941Z","avatar_url":"https://github.com/hakdogan.png","language":"Java","funding_links":[],"categories":["jenkins"],"sub_categories":[],"readme":"![\"Docker Pulls](https://img.shields.io/docker/pulls/hakdogan/jenkins-pipeline.svg)\n[![Analytics](https://ga-beacon.appspot.com/UA-110069051-1/jenkins-pipeline/readme)](https://github.com/igrigorik/ga-beacon)\n\n# A tutorial about Continuous Integration and Continuous Delivery by Dockerize Jenkins Pipeline\n\nThis repository is a tutorial it tries to exemplify how to automatically manage the process of building, testing with the highest coverage, and deployment phases.\n\nOur goal is to ensure our pipeline works well after each code being pushed. The processes we want to auto-manage:\n* Code checkout\n* Run tests\n* Compile the code\n* Run Sonarqube analysis on the code\n* Create Docker image\n* Push the image to Docker Hub\n* Pull and run the image\n\n## First step, running up the services\n\nSince 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.\n\n``docker-compose.yml``\n```yml\nversion: '3.2'\nservices:\n  sonarqube:\n    build:\n      context: sonarqube/\n    ports:\n      - 9000:9000\n      - 9092:9092\n    container_name: sonarqube\n  jenkins:\n    build:\n      context: jenkins/\n    privileged: true\n    user: root\n    ports:\n      - 8080:8080\n      - 50000:50000\n    container_name: jenkins\n    volumes:\n      - /tmp/jenkins:/var/jenkins_home #Remember that, the tmp directory is designed to be wiped on system reboot.\n      - /var/run/docker.sock:/var/run/docker.sock\n    depends_on:\n      - sonarqube\n```\n\nPaths of docker files of the containers are specified at context attribute in the docker-compose file. Content of these files as follows.\n\n``sonarqube/Dockerfile``\n```\nFROM sonarqube:6.7-alpine\n```\n\n``jenkins/Dockerfile``\n```\nFROM jenkins:2.60.3\n```\n\nIf we run the following command in the same directory as the ``docker-compose.yml`` file, the Sonarqube and Jenkins containers will up and run.\n\n```\ndocker-compose -f docker-compose.yml up --build\n```\n\n```\ndocker ps\n\nCONTAINER ID        IMAGE                COMMAND                  CREATED              STATUS              PORTS                                              NAMES\n87105432d655        pipeline_jenkins     \"/bin/tini -- /usr...\"   About a minute ago   Up About a minute   0.0.0.0:8080-\u003e8080/tcp, 0.0.0.0:50000-\u003e50000/tcp   jenkins\nf5bed5ba3266        pipeline_sonarqube   \"./bin/run.sh\"           About a minute ago   Up About a minute   0.0.0.0:9000-\u003e9000/tcp, 0.0.0.0:9092-\u003e9092/tcp     sonarqube\n```\n\n## GitHub configuration\nWe’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 -\u003e Integrations \u0026 services._ The ``Jenkins Github plugin`` should be shown on the list of available services as below.\n\n![](images/001.png)\n\nAfter this, we should add a new service by typing the URL of the dockerized Jenkins container along with the ``/github-webhook/`` path.\n\n![](images/002.png)\n\nThe next step is that create an ``SSH key`` for a Jenkins user and define it as ``Deploy keys`` on our GitHub repository.\n\n![](images/003.png)\n\nIf everything goes well, the following connection request should return with a success.\n```\nssh git@github.com\nPTY allocation request failed on channel 0\nHi \u003cyour github username\u003e/\u003crepository name\u003e! You've successfully authenticated, but GitHub does not provide shell access.\nConnection to github.com closed.\n```\n\n## Jenkins configuration\n\nWe 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.\n\n![](images/004.png)\n\nWe 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.\n\n```\njenkins      | *************************************************************\njenkins      |\njenkins      | Jenkins initial setup is required. An admin user has been created and a password generated.\njenkins      | Please use the following password to proceed to installation:\njenkins      |\njenkins      | 45638c79cecd4f43962da2933980197e\njenkins      |\njenkins      | This may also be found at: /var/jenkins_home/secrets/initialAdminPassword\njenkins      |\njenkins      | *************************************************************\n```\n\nTo access the password from the container.\n\n```\ndocker exec -it jenkins sh\n/ $ cat /var/jenkins_home/secrets/initialAdminPassword\n```\n\nAfter entering the password, we will download recommended plugins and define an ``admin user``.\n\n![](images/005.png)\n\n![](images/006.png)\n\n![](images/007.png)\n\nAfter 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.\n```\n- /var/run/docker.sock:/var/run/docker.sock\n```\n\nThe 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 -\u003e Global Tool Configuration_ menu.\n\n![](images/008.png)\n\nWe 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](https://github.com/hakdogan/jenkins-pipeline/blob/master/Jenkinsfile)) first runs. We give ``myMaven`` and ``myDocker`` names to the tools. We will access these tools with this names in the script file.\n\nSince 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 -\u003e Credentials -\u003e Global credentials (unrestricted) -\u003e Add Credentials_ menu.\n\n![](images/009.png)\n\nWe use the value we entered in the ``ID`` field to Docker Login in the script file. Now, we define pipeline under _Jenkins Home Page -\u003e New Item_ menu.\n\n![](images/010.png)\n\nIn this step, we select ``GitHub hook trigger for GITScm pooling`` options for automatic run of the pipeline by ``Github hook`` call.\n\n![](images/011.png)\n\nAlso 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 (_[Jenkins file](https://github.com/hakdogan/jenkins-pipeline/blob/master/Jenkinsfile)_).\n\n![](images/012.png)\n\nAfter 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.\n\n![](images/013.png)\n\n## Review important points of the Jenkins file\n\n```\nstage('Initialize'){\n    def dockerHome = tool 'myDocker'\n    def mavenHome  = tool 'myMaven'\n    env.PATH = \"${dockerHome}/bin:${mavenHome}/bin:${env.PATH}\"\n}\n```\n\nThe ``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``.\n\n```\nstage('Push to Docker Registry'){\n    withCredentials([usernamePassword(credentialsId: 'dockerHubAccount', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {\n        pushToImage(CONTAINER_NAME, CONTAINER_TAG, USERNAME, PASSWORD)\n    }\n}\n```\n\n``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 -\u003e Credentials -\u003e Global credentials (unrestricted) -\u003e Add Credentials_ menu. In this way, we access to the username and password information of the account for login.\n\n## Sonarqube configuration\n\nFor ``Sonarqube`` we have made the following definitions in the ``pom.xml`` file of the project.\n\n```xml\n\u003csonar.host.url\u003ehttp://sonarqube:9000\u003c/sonar.host.url\u003e\n...\n\u003cdependencies\u003e\n...\n    \u003cdependency\u003e\n        \u003cgroupId\u003eorg.codehaus.mojo\u003c/groupId\u003e\n        \u003cartifactId\u003esonar-maven-plugin\u003c/artifactId\u003e\n        \u003cversion\u003e2.7.1\u003c/version\u003e\n        \u003ctype\u003emaven-plugin\u003c/type\u003e\n    \u003c/dependency\u003e\n...\n\u003c/dependencies\u003e\n```\n\nIn 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.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhakdogan%2Fjenkins-pipeline","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhakdogan%2Fjenkins-pipeline","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhakdogan%2Fjenkins-pipeline/lists"}