CI/CD For Java Project Using Git, Jenkins & Docker
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
- Make sure you have Docker is installed, here the reference link
- Make sure you have installed & configuredbelow plugin:
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:<DockerServerIP>
username: dockeradmin
- Now do test configuration. if you getting failed in test configuration then run following on Docker host
- Add below lines to visudo
visudo ansadmin ALL=(ALL) NOPASSWD: ALL
- Using key-based authentication is advised. If you are still at learning stage use password based authentication:
sed -ie 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
- Restart sshd service
service sshd restart
- Add below lines to visudo
- SSH Servers:
- In your docker server, login with dockeradmin & under /home/dockeradmin
#create a directory mkdir docker #Download ansible playbook which will copy java application to tomcat server wget https://s3.amazonaws.com/webmagicinformatica.com/DevOpsTrainingInMumbai/Docker/Dockerfile
- 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 : docker_server
- Source fiels: webapp/target/*.war
- Remove prefix: webapp/target
- Remote directory: //home//dockeradmin//docker
- Send files or execute commands over SSH
- Add post-build steps
- Send files or execute commands over SSH
- SSH Server : ansible_server
docker stop webmagic_demo; docker rm -f webmagic_demo; docker image rm -f webmagic_demo; cd /home/dockeradmin/docker; docker build -t webmagic_demo .; docker run -d --name webmagic_demo -p 8090:8080 webmagic_demo
- SSH Server : ansible_server
- Send files or execute commands over SSH
- Access web application from browser which is running on container
<docker_host_Public_IP>:8090/webapp
- Login to your Jenkins Dashboard and click New Item