CI/CD for Java Project using Git, Jenkins, Ansible & Tomcat

0 176

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. Tomcat Server up and running, here the reference link
  4. Install ansible on an EC2 instance, here the reference link
  5. Enable passwordless authentication between Ansible & Tomcat EC2 instance, here the reference link

Here we start:

  1. Install “publish Over SSH”
    Manage Jenkins > Manage Plugins > Available > Publish over SSH
  2. Enable connection between Ansible and Jenkins
    Manage Jenkins > Configure System > Publish Over SSH > SSH Servers

    • SSH Servers:
      Hostname:<AnsibleServerIP>
      username: ansadmin
      password: *******
    • Test the connection “Test Connection”
  3. In your ansible server, login with ansadmin & under /home/ansadmin

    #create a directory
    mkdir playbooks
    
    #Download ansible playbook which will copy java application to tomcat server
    wget https://s3.amazonaws.com/webmagicinformatica.com/DevOpsTrainingInMumbai/Ansible/copywarfile.yml
  4. Add tomcat server details to /etc/ansible/hosts (if you are using other hosts file update server info there)
    echo "<Tomcat_server_IP>" >> /etc/ansible/hosts
  5. 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 fiels: webapp/target/*.war
        • Remote directory: //home//ansadmin//playbooks
    6. Add post-build steps
      • Send files or execute commands over SSH
        • SSH Server : ansible_server
        • Exec command
          ansible-playbook /home/ansadmin/playbooks/copywarfile.yml

    7. Execute job and you should be able to seen build has been deployed on Tomcat server.

Leave A Reply

Your email address will not be published.