Jenkins (http://jenkins.io) is used for automated continuous integration and can be used for server management too.

Install Jenkins and Nginx on CentOS 7:
https://www.vultr.com/docs/how-to-install-jenkins-on-centos-7

Make Jenkins a sudoer:

#open /etc/sudoers to edit
sudo mcedit /etc/sudoers

#let jenkins run any command by adding this line
jenkins ALL=(ALL) ALL

#let jenkins run 'sudo' without asking for password
%jenkins ALL=NOPASSWD: ALL

Let Jenkins run bash script on remote Linux machine through SSH:

1. Generate an RSA key pair

2. Add the public key to the remote machine (to home directory of ‘someuser‘)

3. Copy the private key to directory ‘/var/lib/jenkins/.ssh/‘ with name ‘id_rsa

4. Add new build item in Jenkins with the following shell command in ‘Build’ section to test (note that the ‘someuser‘ is to match the one in step 2 above):

#add -v for verbose info
ssh -oStrictHostKeyChecking=no \
someuser@the.remote.machine.in.step2 <<SSHBLOCK
pwd
whoami
hostname
whatever-command-more
SSHBLOCK

Git command with GitHub using personal access token or password:

#example git pull, PAT=personal access token
git pull https://username:passwordOrPAT@github.com/org/repo

Run programme, .sh script, .js script as daemons (this is necessary as Jenkins will terminate all processed called in build process):

1. Run a programme as daemon

#install on centos|redhat
sudo yum install daemonize

#run
daemonize -E BUILD_ID=dontKillMe /path/to/programme

2. Run an .sh script as daemon

setsid somescript.sh >/dev/null 2>&1 </dev/null &

3. Run a .js script as daemon (especially in NodeJS project, PM2 can be used instead of the ‘forever’ module: http://keymetrics.io)

#install
npm install forever

#run
BUILD_ID=dontKillMe forever start somescript.js

#stop a daemon started by 'forever'
forever stop somescript.js

#get a log of daemon started by 'forever'
forever logs somescript.js

4. Find and kill a process

sudo pkill -f "something in command-line"
Advertisements