diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..657927c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM openjdk:11-jre-slim + +WORKDIR /app + +COPY ./target/tdl-1.0.0.war . + +EXPOSE 8080 + +ENTRYPOINT ["java","-jar","tdl-1.0.0.war"] \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile index 55a50c4..abb55f1 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,9 +1,9 @@ pipeline { agent any environment { - appIP=""; - gitRepo=""; - repoName=""; + appIP="35.226.220.179"; + containerName="java"; + imageName="javakb3"; } stages{ stage('Test Application'){ @@ -17,64 +17,46 @@ pipeline { sh 'mv ./target/surefire-reports/*.txt /home/jenkins/Tests/${BUILD_NUMBER}_tests/' } } - stage('SSH Build Deploy'){ + stage('Build Application'){ steps{ - sh '''ssh -i "~/.ssh/jenkins_key" jenkins@$appIP << EOF - rm -rf $repoName - git clone $gitRepo - cd $repoName - rm -f ./src/main/resources/application-dev.properties - mvn clean package + sh 'mvn clean package' + } + } + stage('Docker Build'){ + steps{ + sh ''' + docker build -t ksbhull/$imageName:latest . ''' } } - stage('Moving War'){ + stage('Push Images'){ steps{ - sh '''ssh -i "~/.ssh/jenkins_key" jenkins@$appIP << EOF - cd $repoName - mkdir -p /home/jenkins/Wars - mv ./target/*.war /home/jenkins/Wars/project_war.war + sh ''' + docker push ksbhull/$imageName:latest ''' } } - stage('Stopping Service'){ + + stage('Stopping Container'){ steps{ - sh '''ssh -i "~/.ssh/jenkins_key" jenkins@$appIP << EOF - cd $repoName - bash stopservice.sh + sh '''ssh -i "~/.ssh/id_rsa" jenkins@$appIP << EOF + docker rm -f $containerName ''' } } - stage('Create new service file'){ + stage('Restart App'){ steps{ - sh '''ssh -i "~/.ssh/jenkins_key" jenkins@$appIP << EOF - mkdir -p /home/jenkins/appservice - echo '#!/bin/bash -sudo java -jar /home/jenkins/Wars/project_war.war' > /home/jenkins/appservice/start.sh -sudo chmod +x /home/jenkins/appservice/start.sh -echo '[Unit] -Description=My SpringBoot App - -[Service] -User=ubuntu -Type=simple - -ExecStart=/home/jenkins/appservice/start.sh - -[Install] -WantedBy=multi-user.target' > /home/jenkins/myApp.service -sudo mv /home/jenkins/myApp.service /etc/systemd/system/myApp.service + sh '''ssh -i "~/.ssh/id_rsa" jenkins@$appIP << EOF + docker run -d -p 8080:8080 --name $containerName ksbhull/$imageName ''' } } - stage('Reload and restart service'){ + stage('Clean Up'){ steps{ - sh '''ssh -i "~/.ssh/jenkins_key" jenkins@$appIP << EOF - sudo systemctl daemon-reload - sudo systemctl restart myApp + sh ''' + docker system prune -f ''' } } - } } diff --git a/README.md b/README.md index e1a7f92..8384aed 100644 --- a/README.md +++ b/README.md @@ -236,3 +236,5 @@ You can click on the boxes to see the output of the stages When it finishes the app will be on port 8080 on the app instances public IP You can make changes to the Jenkinsfile to change the pipeline + +New line for webhook