Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save gokman/3f332fe04bff83b81e687111de49cd3e to your computer and use it in GitHub Desktop.

Select an option

Save gokman/3f332fe04bff83b81e687111de49cd3e to your computer and use it in GitHub Desktop.
spring boot auto deployment with git hooks and docker
1. create git bare repository on your server machine (for example a virtual machine rented from digital ocean)
git init --bare
2. go hooks folder in git repo that created above and create post-receive.sh file
3. in post-receive file write below:
#!/usr/bin/env bash
TARGET="/opt/my_app"
REPO="/opt/my_app_git"
TEMP="/tmp/my_app"
# Extracting source to temp directory.
rm -rf $TEMP
mkdir -p $TEMP
git --work-tree=$TEMP --git-dir=$REPO checkout -f
# copy project file from temp directory to the target directory
cd $TEMP
rm -rf $TARGET/*
mv $TEMP/* $TARGET
cd $TARGET
# remove existing container and image
docker stop <container name>
docker rm <container name>
docker rmi <image name>
# execute gradle build to create docker image
./gradlew build -x test -PactiveProfiles=integration buildDocker
# create docker container based on above image
docker run -d --network <docker network> -p 8081:8081 --name <container name> --ip 172.10.0.4 <image name>
# create connection between our docker network and above container
docker network connect --ip 172.10.0.4 <docker network> <container name>
4. prepare your spring boot project to integrate with docker
4.1. inside build.gradle put docker dependency in buildscript -> dependencies
buildscript {
.
.
.
dependencies {
.
.
.
classpath('se.transmode.gradle:gradle-docker:1.2')
}
}
4.2. add below code at the bottom of the build.gradle file
task buildDocker(type: Docker, dependsOn: build) {
push = true
applicationName = jar.baseName
dockerfile = file('src/main/docker/Dockerfile')
doFirst {
copy {
from jar
into stageDir
}
}
}
4.3. create Dockerfile in src/main/docker
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD <your jar file name> app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
5. thats it. finally push your project to the bare repository in server machine
git push <my private repo> master
Good luck!!!
@suxg001

suxg001 commented Sep 12, 2018

Copy link
Copy Markdown

why not add git url ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment