CI/CD for Java Project using Git, Jenkins, Ansible & Tomcat
Before you start:
- Make sure you have Jenkins installed, here the reference link
- Make sure you have pushed java code to your git repo, here the java code
- Tomcat Server up and running, here the reference link
- Install ansible on an EC2 instance, here the reference link
- Enable passwordless authentication between Ansible & Tomcat EC2 instance, here the reference link
Here we start:
- Install “publish Over SSH”
Manage Jenkins > Manage Plugins > Available > Publish over SSH - 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”
- SSH Servers:
- 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
- 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
- Create Jenkins job, Fill the following details
- Login to your Jenkins Dashboard and click New Item
- Enter an Item name
Select Maven project
- Under Source Code Management
Select Git & input your jave program git repo URL
- Under Build
Select Maven
Goals and options: clean install package
- 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
- Send files or execute commands over SSH
- Add post-build steps
- Send files or execute commands over SSH
- SSH Server : ansible_server
- Exec command
ansible-playbook /home/ansadmin/playbooks/copywarfile.yml
- Send files or execute commands over SSH
- Execute job and you should be able to seen build has been deployed on Tomcat server.
- Login to your Jenkins Dashboard and click New Item