Friday, 30 June 2017

Jenkins + Gitlab + AWS EC2 Deployment


Jenkins Gitlab configuration :

Go  to Gitlab --> User --> Settings --> Look for Access Tokens

Create a new Access Tokens for Jenkins


 Jenkins Configuration :

Add Gitlab token in Credentials



Add Publish over SSH plugin in Jenkins

Configure Hostname as a Remote Server IP Address
Configure Username as a Remote Server's Username
Key : Add Jenkins users private key




How  to Generate SSH key for Jenkins user on Jenkins System 

sudo su -s /bin/bash jenkins
ssh-keygen
cat /var/lib/jenkins/.ssh/id_rsa.pub
Copy key from id_rsa.pub
Paste the key in remote server's authorized key file

Jenkins Build Job Configuration :

Select Execute Shell :

#!/bin/bash
cd /home/g1/
. ./copy2ec2.sh


Copy2ec2  Script :

TIME=`date +%b-%d-%y`            
FILENAME=backup-$TIME.tar.gz

SRCDIR=/var/lib/jenkins/workspace/xxx/xxx
DESTDIR=/home/g1/joom_coode

tar -zcvf $DESTDIR/$FILENAME $SRCDIR --exclude .git --exclude /administrator/cache --exclude configuration.php  --exclude .gitignore --exclude .gitlab-ci.yml --exclude .htaccess  --exclude htaccess.txt --exclude robots.txt --exclude robots.txt.dist --exclude web.config.txt

echo "Uploading Artifacts on EC2...."
cd /home/g1/joom_coode
scp -r * xxx@x.210.x.x:/home/xxx/jen_deploy
rm -Rf *


Send Files or Execute commands over SSH

echo "Unpacking TAR...."
cd /home/xxx/jen_deploy

tar -zxvf backup-*.tar.gz

1 comment:

  1. Thanks for sharing for this informative blog post and i really appreciate your work keep it up.
    DevOps Online Training

    ReplyDelete