First of all, we made our server list file. Because this is very often used by Jenkins deploy and restart jobs.
Add your Jenkins user key to my_name/.ssh/authorized_key on each server.
-Jenkins Server List Configure Job-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CONFIG(){ | |
test -e $1 && cp $1 $1.old | |
cat > $1 <<EOF | |
192.28.255.111 | |
192.28.255.112 | |
EOF | |
test -e $1.old && diff $1 $1.old|cat | |
} | |
CONFIG /usr/local/repository/prd/my_product.list |
Next step, we build our project and make backup too.
-Jenkins Build Job-Build our project by running maven.
clean release:clean release:prepare package -P prd
And make backup directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This script is for jenkins build job | |
OLD_DIR=$(date +"%Y_%m_%d_%H_%M_%S") | |
REPO=/usr/local/repository/prd/my_product/ | |
BACKUP_DIR="/usr/local/repository/prd/my_product_backup" | |
if [ -d "$BACKUP_DIR" ]; then | |
mv $BACKUP_DIR/my_product $BACKUP_DIR/$OLD_DIR/ | |
fi | |
mv $REPO $BACKUP_DIR/my_product/ | |
/usr/bin/rsync -acvz ${WORKSPACE}/target/deploy/ $REPO --delete |
And run deploy job. Don't forget set post-build Actions. We set Build other projects(Jenkins Restart Job)
-Jenkins Deploy Job-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LIST=/usr/local/repository/prd/my_product.list | |
REPO=/usr/local/repository/prd/my_product | |
DEST=/usr/local/app/ | |
sed -e "s/#.*//" -e "s/^\s*$//" $LIST | while read i; do | |
echo "#### deploy -> $i ####" | |
rsync -e 'ssh -o StrictHostKeyChecking=no' -acvz --delete --exclude="logs" $REPO my_name@$i:$DEST | |
done | |
sed -e "s/#.*//" -e "s/^\s*$//" $LIST |
-Jenkins Restart Job-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LIST=/usr/local/repository/prd/my_product.list | |
COMMAND="sh /usr/local/app/my_product/bin/my_product_control.sh restart" | |
sed -e "s/#.*//" -e "s/^\s*$//" $LIST | while read i; do | |
echo "#### restart -> $i ####" | |
ssh -o 'StrictHostKeyChecking=no' -2 -n my_name@$i $COMMAND | |
done |
Sometimes we need to revert our service. We use this job. Don't forget to set Build other projects(Jenkins Deploy Job) that it will execute Jenkins Restart Job.
-Jenkins Roll Back Job-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/usr/bin/rsync -acvz /usr/local/repository/prd/my_product_backup/my_product/ /usr/local/repository/prd/my_product/ --delete |