Today, I will show to you how to create a new job to deploy source code in Git Server to Deployment Server by Jenkins. In previous article, we have installed successful Jenkins, so we will use it to create new job.
Adding public ssh key
The first step, we need to add ssh-key of Jenkins’s server to Git Server and Deployment Server. After that Jenkins can communicate with Git Server and Deployment Server without enter password. You log into Jenkins Server with jenkins user, after that gen-key by typing:
ssh-keygen -t rsa -C "[email protected]"
The next step, you open /var/lib/jenkins/.ssh/idrsa.pub and copy content. The next step, you go to Git Server and Deployment Server and adding jenkin’s key. You paste jenkin’s key to /home/username/.ssh/authorizedkeys of the both Git Server and Deployment Server.
Install Git client plugin for Jenkins
The next step, you go to Jenkins server http://yourserver:8080 and install “git client plugin” for jenkins. You go to Jenkins with administrator user, after that go to Manager Jenkins and choose Manager Plugin. Next, you click on “Available” tab and search “Git client plugin”, checking it and click “install without restart”. Now, You have Git client plugin installed.
The next, you need to install “rsync”. We will use it to copy source code to Deployment Server. To install rsync, you only typing in terminal:
sudo apt-get install rsync
Create a job to deploy
Now, we will create job to deploy source code in Git Server to Deployment Server by Jenkins. You go to Jenkins Server and login by admin user. After that, you click “New item” in the left menu and choose free-style project.
Press Ok and on the next page, You checking “This build is parameterized” to deploy with parameter for job.
The next step, In Source Code Management you choose Git and enter Repository URL of code and Branch to build.
The next step, In build tab, you add Execute shell.
After that fill in command textbox: install rsync, you only typing in terminal:
[email protected]_deployment_server DESTINATION_PATH=/home/sigma/cuongbn/folder_deploy RSYNC_PARAMS='-rl --del --exclude=.git --exclude rsync.log --log-file=rsync.log ' # run unit test if you have python3 unit_test/test.py # deploy sources eval rsync $RSYNC_PARAMS ./ $DESTINATION_SERVER:$DESTINATION_PATH
The final, You click Save button. Now, in the dashboard of Jenkins will show your job. You choose it and click “Build with Parameters” to Build and deploy your code to Deployment Server.