Description:
- Preparing the environment with Jenkins==>Terraform==>Ansible
- CI/CD pipeline with Git==>GitHub==>Jenkins==>Maven==>Ansible==>Tomcat&Docker==>DockerHub==>Web Server
To prepare deployment environment, use Jenkins, Terraform, and Ansible, installing them and configuring Jenkins project accordingly. After committing changes to GitHub repository, Jenkins will build and test the application with Maven before transferring it to the Ansible server. There, Ansible will build a Docker image and send it to DockerHub before uploading it to the Docker server and launching it.
1.1 On your local computer (Ubuntu), download and run the script 1_install_packages_Ubuntu.sh
chmod +x 1_install_packages_Ubuntu.sh
source 1_install_packages_Ubuntu.sh
sudo mkdir /cicd
sudo ssh-keygen -f /cicd/aws
sudo chown jenkins:jenkins /cicd -R
1.3.1 Jenkins ==> Manage Jenkins ==> Global Tool Configuration add information where installed:
- Terraform. Find where it installed:
whereis terraform
- Ansible. Find where it installed
which ansible
1.3.2 Jenkins ==> Manage Jenkins ==> Manage Credentials ==> (global) ==> Add credentials ==> AWS Credentials Add credentials to connect to AWS (ID: cicd-credentials)
1.3.3 Create Jenkins pipeline job, paste code from Jenkinsfile and run. The process of launching three servers using the Terraform and their configuration using the Ansible will begin.
su *your_user_name* (ansiblecicd)
ssh-copy-id *private_ip_localhost* (private_2) #it's necessary to exchange the SSH-key with the local server on behalf of the created user
ssh-copy-id *private_ip_docker_server* (private_1)
docker login
4.1 Create trigger to automatically start a job in Jenkins, when some changes are made to the GitHub repository
- in settings GitHub repository with the project, specify the address of Jenkins-server and type of events to trigger this webhook (http://ip_jenkins_server:8080/github-webhook/)
-
Manage Jenkins ==> Configure System ==> Publish over SSH add private IP address of Ansible-server (private_2), username and created user password.
A better solution is to use an SSH key
-
Manage Jenkins ==> Global Tool Configuration ==> configure GIT, JDK and Maven
script to section "Exec command":
ansible-playbook -i/opt/docker/inventory.ini /opt/docker/3.3_playbook_push_image.yml;
sleep 10;
ansible-playbook -i/opt/docker/inventory.ini /opt/docker/3.4_playbook_run_container.yml;