Skip to content

Latest commit

 

History

History

spring-boot-reactive-jenkinsfile

spring-boot-reactive-jenkinsfile

ตัวอย่างการเขียน Spring-boot Reactive Jenkinsfile

Prerequisites

1. เพิ่ม Dependencies และ Plugins

pom.xml

...
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.1</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>        
                <execution>            
                    <id>build-info</id>            
                    <goals>                
                        <goal>build-info</goal>            
                    </goals>        
                    <configuration>                
                        <additionalProperties>                    
                            <java.version>${java.version}</java.version>                                   
                        </additionalProperties>            
                    </configuration>        
                </execution>    
            </executions>
        </plugin>
    </plugins>
</build>
...

2. เขียน Main Class

@SpringBootApplication
public class AppStarter {

    public static void main(String[] args) {
        SpringApplication.run(AppStarter.class, args);
    }

}

3. เขียน Controller

@RestController
public class HomeController {

    @GetMapping({"", "/"})
    public Mono<String> hello() {
        return Mono.just("Hello world.");
    }
}

4. เขียน Dockerfile

ไว้ที่ root ของ project /Dockerfile

FROM openjdk:21-slim
EXPOSE 8080
ADD target/*.jar /app.jar
ENTRYPOINT java $JAVA_OPTS -jar /app.jar

ถ้าใครไม่แม่นเรื่อง Dockerfile สามารถอ่านได้จาก Slide นี้ พื้นฐาน Docker

5. เขียน Jenkinsfile

ไว้ที่ root ของ project /Jenkinsfile

pipeline {

    agent any

    environment {
        IMAGE_NAME = "registry.jittagornp.me/hello-world:${env.BUILD_NUMBER}"
        REGISTRY_URL = "https://registry.jittagornp.me"
        REGISTRY_USERNAME = "foo"
        REGISTRY_PASSWORD = "bar"
    }

    stages {

        stage("Init") {
            steps {
                echo "Init"
                echo "******************************"
                ...
            }
        }

        stage("Mvn Install") {
            steps {
                echo "Mvn Install"
                echo "******************************"
                sh 'docker run --rm -v $(pwd):/app -v ~/.m2:/root/.m2 maven:3.9.6-amazoncorretto-21 mvn -DskipTests=true clean package -f /app/pom.xml'
            }
        }

        stage("Mvn Test") {
            steps {
                echo "Mvn Test"
                echo "******************************"
                sh 'docker run --rm -v $(pwd):/app -v ~/.m2:/root/.m2 maven:3.9.6-amazoncorretto-21 mvn test -f /app/pom.xml'
            }
        }

        ...
    }
}

ถ้าใครไม่แม่นเรื่อง Jenkinsfile สามารถอ่านได้จากบทความนี้ พื้นฐานการเขียน Jenkins Pipeline

หมายเหตุ

  • Username/Password Registry จากตัวอย่างเป็นการ FIXED ค่าใส่ตัวแปร Environment ให้ดูง่าย ๆ แต่ตอนใช้งานจริงอาจจะเก็บค่าไว้ใน Jenkins Credentials แล้วค่อยอ่านค่ามาใช้งานแทนก็ได้

6. Push Code ไปไว้ที่ Git Remote Repository (Git Hosting)

เช่น GitHub, GitLab, etc.

7. สร้าง Jenkins Pipeline เพื่อทดสอบ Jenkinsfile

Output