CI/CD For Java Project Using Git, Jenkins, Ansible, Docker Hub & Docker

0 179

Before you start:

  1. Make sure you have Jenkins installed, here the reference link
  2. Make sure you have pushed java code to your git repo, here the java code
  3. Make sure you have Ansible and Docker installed on same host
    1. Install Ansible
    2. Install Docker
  4. Make sure you have Docker is installed on one more host, here the reference link
  5. Enable passwordless authentication between Ansible & Docker EC2 instance, here the reference link
  6. Make sure you have installed & configuredbelow plugin:
    1. Configure Git Plugin On Jenkins
    2. Install & configure Maven build tool on Jenkins

Here we start:

  1. On Ansible server, login with ansadmin and then docker to docker hub:
    docker login
  2. Add ansadmin username to Docker (default) group
    usermod -aG docker ansadmin
  3. On Jenkins, install “publish Over SSH”
    Manage Jenkins > Manage Plugins > Available > Publish over SSH
  4. Enable connection between Ansible and Jenkins
    Manage Jenkins > Configure System > Publish Over SSH > SSH Servers

    • SSH Servers:
      Hostname:<DockerServerIP>
      username: ansadmin
      password: *******
    • SSH Servers:
      Hostname:<AnsibleServerIP>
      username: ansadmin
      password: *******
  5. Add docker server details to /etc/ansible/hosts (if you are using other hosts file update server info there)
    echo "<Docker_server_IP>" >> /etc/ansible/hosts
  6. Create Jenkins job, Fill the following details
    1. Login to your Jenkins Dashboard and click New Item
    2. Enter an Item name
      Select Maven project
    3. Under Source Code Management
      Select Git & input your jave program git repo URL
    4. Under Build
      Select Maven
      Goals and options: clean install package
    5. Add post-build steps
      • Send files or execute commands over SSH
        • SSH Server : ansible_server
        • Source fields: Dockerfile
        • Remote directory: //home//ansadmin
      • Click over Add Transfer set
        • SSH Server : ansible_server
        • Source fiels: webapp/target/*.war
        • Remove prefix: webapp/target
        • Remote directory: //home//ansadmin
        • Exec command
          docker build -t webmagic_demo .
          docker tag webmagic_demo dab8106/webmagic_demo
          docker push dab8106/webmagic_demo
          docker rmi webmagic_demo dab8106/webmagic_demo

    6. Apply & Save
  7. Now lets create a deploy job
    1. Login to your Jenkins Dashboard and click New Item
    2. Enter an Item name
      Select Free Style Project
    3. Under Source Code Management
      Select Git & input your jave program git repo URL
    4. Add post-build steps
      • Send files or execute commands over SSH
        • SSH Server : ansible_server
        • Source files: create_docker_container.yml
        • Remote directory: //home//ansadmin
        • Exec command
           ansible-playbook create_docker_container.yml

    5. Apply & Save
  8. As a last step, lets integrate our first pipeline to invoke other second pipeline
    1. Select your first pipeline
    2. Click on Configure
    3. Under Post-build Actions, select Build Other Projects
    4. Enter your project name and select the same from drop-down
    5. Apply & Save
  9. Now build from first pipeline, it will build and invoke second pipeline

Leave A Reply

Your email address will not be published.