Friday, June 15, 2018

Micro Continuous Deployment System

Hi Everyone,

Hope you have a great day, Today we going to build the micro deployment system.

What we need:
- ubuntu server (root access)
- git repository with your code

1. SSH to your favorite Ubuntu server.
ssh -p 22 shmalex@ubutunserver

2. Clone project
git clone https://github.com/shmalex/wifispymap.git

3. Check if your project is working and configure everything you need.

3.1 The main trick in the successful deployment of the system - all configurations should be in a file that does not see LIVE.

This can be done in many ways:
  1. place the configuration in the directory above
  2. keep all the plates in the same environment
  3. keep you configuration in DB.... (but you need to store the connection string to the db...)

3.2 Double check that you projects folder is 'clean' by running command:
git status
3.4 It should show you that there are No/0/ZERO changes

3.4 By knowing that not all projects are perfect, I might need to keep making changes in your project and repeat step 2 and 3 until you achieve the resuls in 3.4.


All good?

ok, now the Continues Deployment part

When you have done all steps in 1,2,3 - the CD is very easy, let's make scheduler that run the some script, that script will contain the commands to pull the project, and other commands that makes our project apply thous changes.

4 - Create 'Deployment' script:

# open home folder
$ cd
# craete file
$ echo '#!/bin/bash' > deployment.sh
# edit the file
$ nano deployment.bash

Ok, now the tricky part.
we need to specify full path to the folders and files in bash script. we can not use the '~/wifispymap' path.
this is my script: 
#!/bin/bash
cd /home/shmalex/wifispymap/
git pull

4.2 make that file executable
chmod +x deployment.sh 

Now let's test it!
1 Test permissions - 
run the command 
$ ls -ahl | grep 'deployment.sh'
It will show the file's permission, we are interested in the x
-rwxrwxr-x  1 shmalex shmalex   50 Jun 15 14:22 deployment.sh

#2 test from home directory
$ cd$ deployment.sh
Already up to date.

#3 test from the root
$ cd /
$ /home/shmalex/deployment.sh
Already up to date.

100% passed.


5 - Convert "Deployment" into ''Continuous Deployment"
We need to use the scheduler,
$ sudo crontab -e

Add new line that will run our script every 5 minutes
*/5 * * * * /home/shmalex/deployment.sh

5.1. Test, following line will show you your changes
$ sudo crontab -l 

5.1 Test
Scheduler adds lines to the syslog file, where we can find our deployment script
$ grep CRON /var/log/syslog
We almost done!!! :)

6. And last test - let's make changes and find them on our server :)

6.1 - make any changes in project, i will update the readme.md file
6.2 - monitor the cron job with command
$ grep CRON /var/log/syslog
...
Jun 15 14:40:01 shmalex-dellpc CRON[14218]: (root) CMD (/home/shmalex/deployment.sh)
Jun 15 14:40:03 shmalex-dellpc CRON[14217]: (CRON) info (No MTA installed, discarding output)
... 

6.3. check the file with text editor, or by running the ls -hls command to check the time of changes file.


Have fun! and feel free to ask any questions :)


Reading materials

No comments:

Post a Comment