Last active
May 31, 2020 23:15
-
-
Save Roaim/0e3df700e7db59b07b0d8c7765864179 to your computer and use it in GitHub Desktop.
Spring boot auto deploy script
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
#! /bin/bash | |
if [ $# -lt 3 ]; | |
then | |
echo "Please provide the project name, branch name and git user name" | |
else | |
projectName=$1 | |
projectParentDir=/opt/$projectName | |
branchName=$2 | |
gitUser=$3 | |
projectDir=$projectParentDir/project | |
gitDir=/home/$gitUser | |
echo "ProjectName: $projectName" | |
echo "ProjectDirectory: $projectDir" | |
echo "BranchName: $branchName" | |
echo "GitDirectory: $gitDir" | |
mkdir $projectParentDir | |
mkdir $projectDir | |
chown -R $gitUser:$gitUser $projectParentDir | |
cd $gitDir | |
git init --bare $projectName.git | |
chown -R $gitUser:$gitUser $projectName.git | |
cd $projectName.git/hooks | |
cat > post-receive << EOD | |
#!/bin/bash | |
TARGET=$projectDir | |
GIT_DIR=$gitDir/$projectName.git | |
BRANCH=$branchName | |
while read oldrev newrev ref | |
do | |
if [ "\$ref" = "refs/heads/\$BRANCH" ]; | |
then | |
echo "Ref \$ref received. Deploying \$BRANCH to production..." | |
git --work-tree=\$TARGET --git-dir=\$GIT_DIR checkout -f \$BRANCH | |
else | |
echo "Ref \$ref received. Doing nothing. Only the \$BRANCH branch is allowed to be deployed." | |
fi | |
done | |
msg=\$(git log -1 --pretty=format:%s) | |
echo "Last commit: \$msg" | |
if [[ "\$msg" == '#'* ]]; then | |
sn=\$(echo "\$msg" | cut -d '#' -f 2) | |
echo "Restarting \$sn..." | |
sudo $projectParentDir/restart/\$sn | |
else | |
echo "No service restarted. To auto restart, commit msg must start with #service-name#. Exp: #gateway-service#" | |
fi | |
EOD | |
chown $gitUser:$gitUser post-receive | |
chmod +x post-receive | |
cd $projectParentDir | |
mkdir restart | |
mkdir media | |
chown -R $gitUser:$gitUser restart | |
chown -R $gitUser:$gitUser media | |
git init --bare config.git | |
chown -R $gitUser:$gitUser config.git | |
cat > service-creator.sh <<- EOF | |
#!/bin/bash | |
if [ \$# -lt 1 ]; | |
then | |
echo "Please provide service name and optionally /subfolder name" | |
else | |
sn=\$1 | |
sf=\$2 | |
cat > $projectParentDir/restart/\$sn << EOS | |
cd $projectDir\$sf/\$sn | |
sh mvnw clean | |
sh mvnw package | |
systemctl restart \$sn | |
echo "\$sn is restarting..." | |
systemctl status \$sn | |
EOS | |
chown $gitUser:$gitUser $projectParentDir/restart/\$sn | |
chmod +x $projectParentDir/restart/\$sn | |
echo "%wheel ALL=NOPASSWD: $projectParentDir/restart/\$sn" >> /etc/sudoers.d/$gitUser | |
cat > /etc/systemd/system/\$sn.service << EOT | |
[Unit] | |
Description=\$sn | |
After=syslog.target | |
Type=simple | |
[Service] | |
User=$gitUser | |
ExecStart=/usr/bin/java -jar $projectDir\$sf/\$sn/target/\$sn-0.0.1-SNAPSHOT.jar | |
Restart=always | |
StandardOutput=syslog | |
StandardError=syslog | |
SyslogIdentifier=\$sn | |
SuccessExitStatus=143 | |
[Install] | |
WantedBy=multi-user.target | |
EOT | |
chmod 644 /etc/systemd/system/\$sn.service | |
systemctl enable \$sn | |
systemctl start \$sn | |
fi | |
EOF | |
chmod +x service-creator.sh | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment