Build, test and deploy with Jenkins

To build and deploy this website we use Jenkins (available here: javaee.cloud:8081/job/java-demo-pipeline/)

Automatically build after GitHub commits

Install the GitHub plugin in Jenkins.

In your the Jenkins pipeline configuration of your project select the option ‘GitHub hook trigger for GITScm polling’.

You have to add a WebHook to your GitHub project that points to your Jenkins installation.

Jenkins Pipeline Script

In the pipeline we use the following script, the credentials for Docker Hub / Docker Cloud are stored in Jenkins.

def dockerImage

node {
    stage ('Build from GitHub') {
    git branch: 'master', url: 'https://github.com/marco76/java-demo.git';
    sh 'mvn clean install';
    archiveArtifacts 'server/target/*.war';      
    }
     
stage('SonarQube analysis') {
    withSonarQubeEnv('SonarQube Local') {
    sh 'mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar'
 }
}

stage('Build Docker image') {
    sh 'docker system prune -a -f';
     dockerImage = docker.build('javaee/java-demo', '.');
}
stage('Push Docker Image to Docker Cloud')
  docker.withRegistry('https://registry.hub.docker.com', 'docker-hub')
    {
        dockerImage.push("${env.BUILD_NUMBER}");
        dockerImage.push("latest");
    } 
}