Skip to content

Mark1708/hello-world-spring-app-with-jenkins

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hello-world-spring-app-with-jenkins

Jenkins Spring Boot PostgreSQL Flyway Docker

Памятка по подключению Jenkins

Table of contents

General info

1. Установка

brew install jenkins-lts

2. Запуск jenkins

brew services start jenkins-lts

3. Вводим пароль админа из конфигурации

При первом старте проекта потребуется ключ initialAdmin

cat /Users/markguranov/.jenkins/secrets/initialAdminPassword

4. Устанавливаем утилиты

Выбираем "Install suggested plugins"

5. Настройка Глобальной конфигурации Jenkins

Настройки Jenkins -> Конфигурация глобальных инструментов

6. Создаём Pipeline для Jenkins

7. Проводим настройку Pipeline

7.1. General

7.2. Build Trigger

Включаем триггер

Стартуем ngrok

####(Т.к. Webhook нельзя делать на локальный хост)

./ngrok http 8080

Добавляем Webhook на Github

7.3. Pipeline

8. Создаём Dockerfile-packaged в директории нашего проекта

FROM alpine:3.10.3 as packager
RUN apk --no-cache add openjdk11-jdk openjdk11-jmods
ENV JAVA_MINIMAL="/opt/java-minimal"
# build minimal JRE
RUN /usr/lib/jvm/java-11-openjdk/bin/jlink \
    --verbose \
    --add-modules \
        java.base,java.sql,java.naming,java.desktop,java.management,java.security.jgss,java.instrument \
    --compress 2 --strip-debug --no-header-files --no-man-pages \
    --release-info="add:IMPLEMENTOR=radistao:IMPLEMENTOR_VERSION=radistao_JRE" \
    --output "$JAVA_MINIMAL"


FROM alpine:3.10.3
LABEL maintainer="Mark Gurianov [email protected]"
ENV JAVA_HOME=/opt/java-minimal
ENV PATH="$PATH:$JAVA_HOME/bin"
COPY --from=packager "$JAVA_HOME" "$JAVA_HOME"
COPY /target/*.jar app.jar
EXPOSE 8085
ENTRYPOINT ["java","-jar","/app.jar"]

9. Создаём Jenkinsfile(Pipeline script) в директории нашего проекта

pipeline {
    agent any
    options {
        skipStagesAfterUnstable()
    }
    environment {
        DOCKERHUB_CREDENTIALS = credentials('jenkins.dockerhub')
        IMAGE_BASE = 'markstav/jenkins-ci-cd-project'
        IMAGE_TAG = "v$BUILD_NUMBER"
      	IMAGE_NAME = "${env.IMAGE_BASE}:${env.IMAGE_TAG}"
      	IMAGE_NAME_LATEST = "${env.IMAGE_BASE}:latest"
      	DOCKERFILE_NAME = "Dockerfile-packaged"
    }
    stages {
        stage('CloneGitRepo') {
            agent any
            steps {
                git credentialsId: '32647e76-60a3-42e4-a5f1-a0d6c5d4fa53', url: 'https://github.com/Mark1708/hello-world-spring-app-with-jenkins.git'
            }
        }

        stage('MavenCompile') {
            agent any
            steps {
                sh './mvnw compile'
            }
        }

        stage('MavenTest') {
            agent any
            steps {
                sh './mvnw test'
            }
        }

        stage('MavenPackage') {
            agent any
            steps {
                sh './mvnw package'
            }
        }

        stage('BuildImage') {
            agent any
            steps {
                sh 'docker build -t $IMAGE_NAME -f Dockerfile-packaged .'
                sh 'docker tag $IMAGE_NAME $IMAGE_NAME_LATEST'
            }
        }

        stage('LoginDockerhub') {
            agent any
			steps {
				sh 'echo $DOCKERHUB_CREDENTIALS_PSW | docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin'
			}
		}

        stage('PushImage') {
            agent any
            steps {
                sh 'docker push $IMAGE_NAME'
                sh 'docker push $IMAGE_NAME_LATEST'
                sh "docker rmi ${env.IMAGE_NAME} ${env.IMAGE_NAME_LATEST}"
            }
        }

        stage('Deploy') {
            agent any
            steps {
                echo 'Deploy to cloud'
            }
        }

    }

    post {
		always {
			sh 'docker logout'
		}
	}
}

10. DockerHub && Github подготовка креденшелов

DockerHub

Github

Отправляемся сюда, чтобы сгенерить безопасный способ аутентификации

http://localhost:8080/job/JOB_NAME/pipeline-syntax/

11. Проверяем и наслаждаемся🤤

Technologies

  • Java - version 11
  • Spring Boot - version 2.7.2
  • PostgreSQL
  • Flyway
  • Docker
  • Jenkins

Status

Проект закончен

Inspiration

Проект сделан в образовательных целях

Contact

Created by Gurianov Mark - feel free to contact me!

+7(962)024-50-04 | [email protected] | github

Readme Card

Releases

No releases published

Packages

No packages published