I have a problem with a jenkins pipeline on a maven spring boot project using spring profiles.
This is the pom.xml:
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
</profile>
<profile>
<id>svil</id>
<properties>
<spring.profiles.active>svil</spring.profiles.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
</profile>
</profiles>
application.properties:
#---
spring.profiles.active=@spring.profiles.active@
my.prop=used-always-in-all-profiles
#---
spring.config.activate.on-profile=dev
spring.datasource.url=.....
spring.datasource.username=....
spring.datasource.password=....
#---
spring.config.activate.on-profile=svil
spring.datasource.url=.....
spring.datasource.username=....
spring.datasource.password=....
#---
spring.config.activate.on-profile=production
spring.datasource.url=....
spring.datasource.username=....
spring.datasource.password=....
Jenkinsfile:
pipeline {
agent any
tools {
maven 'jenkins-maven'
jdk 'java-17'
}
stages {
stage('Build'){
steps {
sh "mvn -Psvil clean install -DskipTests"
}
}
stage('Test'){
steps{
sh "mvn test"
}
}
stage('Deploy') {
steps {
.....
}
}
}}
Jenkins is configured with my github repository and runs the steps defined in the Jenkinsfile. The maven installed on the server is used:
/opt/apache-maven-3.6.3
When running the command:
sh "mvn -Psvil clean install -DskipTests"
The indicated profile (svil) is not loaded correctly but takes the default one (dev)
2024-04-17T14:25:04.167+02:00 INFO 825213 --- [ main] i.r.p.ApplicationTests : Starting ApplicationTests using Java 17.0.10
with PID 825213 (started by jenkins in /var/lib/jenkins/workspace/app)
2024-04-17T14:25:04.168+02:00 INFO 825213 --- [ main] i.r.p.ApplicationTests : The following 1 profile is active: "dev"
From the command line I didn't encounter any problems and the project compiled into jar without any problems.
How can I configure the jenkins pipeline to take the correct spring profiles?
Thanks
The solution is modify the Jenkinsfile and add the environment with the profile and use them in mvn clean installan during the build:
environment {
SPRING_PROFILES_ACTIVE = 'svil'
}
stages {
stage('Build'){
steps {
sh "mvn clean install -Dspring.profiles.active=${SPRING_PROFILES_ACTIVE} -DskipTests"
}
}