Last active
June 2, 2021 19:12
-
-
Save mauriciosilva/0996b09a02ae0d8adbcc42191cf14cfb to your computer and use it in GitHub Desktop.
mulitstage-jenkinsfile -v1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This Pipeline builds the application, runs unit as well as integration tests and deploys the application to several environments. It uses a global variable "deploy" that is provided within a Shared Library. The deploy method copies the JAR-File to a remote server and starts the application. Through the handy REST endpoints of Spring Boot Actuator a previous version of the application is stopped beforehand. Afterwards the deployment is verified via the health status monitor of the application. | |
JenkinsFile | |
pipeline { | |
agent any | |
environment { | |
branch = 'master' | |
scmUrl = 'ssh://git@myScmServer.com/repos/myRepo.git' | |
serverPort = '8080' | |
developmentServer = 'dev-myproject.mycompany.com' | |
stagingServer = 'staging-myproject.mycompany.com' | |
productionServer = 'production-myproject.mycompany.com' | |
} | |
stages { | |
stage('checkout git') { | |
steps { | |
git branch: branch, credentialsId: 'GitCredentials', url: scmUrl | |
} | |
} | |
stage('build') { | |
steps { | |
sh 'mvn clean package -DskipTests=true' | |
} | |
} | |
stage ('test') { | |
steps { | |
parallel ( | |
"unit tests": { sh 'mvn test' }, | |
"integration tests": { sh 'mvn integration-test' } | |
) | |
} | |
} | |
stage('deploy development'){ | |
steps { | |
deploy(developmentServer, serverPort) | |
} | |
} | |
stage('deploy staging'){ | |
steps { | |
deploy(stagingServer, serverPort) | |
} | |
} | |
stage('deploy production'){ | |
steps { | |
deploy(productionServer, serverPort) | |
} | |
} | |
} | |
post { | |
failure { | |
mail to: 'team@example.com', subject: 'Pipeline failed', body: "${env.BUILD_URL}" | |
} | |
} | |
} | |
## vars/deploy.groovy | |
def call(def server, def port) { | |
httpRequest httpMode: 'POST', url: "http://${server}:${port}/shutdown", validResponseCodes: '200,408' | |
sshagent(['RemoteCredentials']) { | |
sh "scp target/*.jar root@${server}:/opt/jenkins-demo.jar" | |
sh "ssh root@${server} nohup java -Dserver.port=${port} -jar /opt/jenkins-demo.jar &" | |
} | |
retry (3) { | |
sleep 5 | |
httpRequest url:"http://${server}:${port}/health", validResponseCodes: '200', validResponseContent: '"status":"UP"' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment