CI/CD For Java Project Using Git, Jenkins, Ansible, Docker Hub & 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 Ansible and Docker installed on same host
- Make sure you have Docker is installed on one more host, here the reference link
- Enable passwordless authentication between Ansible & Docker EC2 instance, here the reference link
- Make sure you have installed & configuredbelow plugin:
Here we start:
- On Ansible server, login with ansadmin and then docker to docker hub:
docker login
- Add ansadmin username to Docker (default) group
usermod -aG docker ansadmin
- On Jenkins, 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: ansadmin
password: *******
- SSH Servers:
Hostname:<AnsibleServerIP>
username: ansadmin
password: *******
- SSH Servers:
- 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
- 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 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
- Send files or execute commands over SSH
- Apply & Save
- Login to your Jenkins Dashboard and click New Item
- Now lets create a deploy job
- Login to your Jenkins Dashboard and click New Item
- Enter an Item name
Select Free Style Project - Under Source Code Management
Select Git & input your jave program git repo URL
- 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
- Send files or execute commands over SSH
- Apply & Save
- As a last step, lets integrate our first pipeline to invoke other second pipeline
- Select your first pipeline
- Click on Configure
- Under Post-build Actions, select Build Other Projects
- Enter your project name and select the same from drop-down
- Apply & Save
- Select your first pipeline
- Now build from first pipeline, it will build and invoke second pipeline