Jenkins 2 & Bitbucket

Live together in perfect harmony

Jenkins

docker pull jenkins/jenkins:lts
docker run --name myjenkins -p 8080:8080 -p 50000:50000 \
-v /var/jenkins_home jenkins/jenkins:lts

Jenkins

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

d5567ba0c53b43709e2daae0bf8e6fa9

This may also be found at: /var/jenkins_home/secrets/initialAdminPassword

During installation, logs provide a key for initial setup

Jenkins

http://localhost:8080

Continue setup and install suggested plugins

Install Blue Ocean, Bitbucket, Jacoco, and Sonarqube scanner plugins

Restart Jenkins

Jenkins

Add a JDK

Manage Jenkins > Global Tools configuration

 

Jdk > Jdk installations

Name : jdk8

Install automatically : checked

Jenkins

Add a JDK

Run shell command

label : install open-jdk8

Command : `sudo apt-get --assume-yes install openjdk-8-jdk`

Tool Home : `/usr/bin`

 

Jenkins

Add a Maven Tool

 

Maven > Maven installations

Name : maven-3.5.3

Install automatically : checked

Install from Apache : 3.5.3

Bitbucket

docker volume create --name bitbucketVolume
docker run -v bitbucketVolume:/var/atlassian/application-data/bitbucket \
--name="bitbucket" -d -p 7990:7990 -p 7999:7999 atlassian/bitbucket-server:5.5

Bitbucket

http://localhost:7990

Acquire a Bitbucket evaluation license

Proceed to installation

Sonar

docker pull sonarqube:lts
docker run -d --name sonarqube -p 9000:9000 -p 9092:9092 sonarqube:lts

Sonar

http://localhost:9000

http://172.17.0.1:9000 (depending on your docker ip)

Sonar

Generate a token

Sonar

Generate a token

Sonar

Generate a token

Sonar

Generated maven command

mvn sonar:sonar \
  -Dsonar.host.url=http://localhost:9000 \
  -Dsonar.login=6470b64c0936e5e479b5c1504a8e547cf8a7351d
mvn sonar:sonar \
  -Dsonar.host.url=http://172.17.0.1:9000 \
  -Dsonar.login=6470b64c0936e5e479b5c1504a8e547cf8a7351d

Using docker IP

Sonar

Jenkins configuration, use the generated token

Docker commands

docker start myjenkins
docker start bitbucket
docker start sonarqube

The Bitbucket project

The Bitbucket project

The Bitbucket project

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>fr.bouvier.marc</groupId>
  <artifactId>pipeline-as-code</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>pipeline-as-code</name>
  <description>A simple pipeline-as-code.</description>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <skip.surefire.tests>${skipTests}</skip.surefire.tests>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.14</version>
        <configuration>
          <!-- skips surefire tests without skipping failsafe tests.
                 Property value seems to magically default to false -->
          <skipTests>${skip.surefire.tests}</skipTests>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.9</version>
        <executions>
          <execution>
            <id>default-prepare-agent</id>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
          <execution>
            <id>default-report</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

The Bitbucket project

public class App 
{
    public static void main( String[] args )
    {
        System.out.println( printHelloWorld());
    }

    public static String  printHelloWorld(){
        return "Hello World!";
    }
}

The Bitbucket project

public class AppTest {

    @Test
    public void testApp() {
        assertThat(
            App.printHelloWorld(),
            is(equalTo("Hello World!")));
    }
}

The Bitbucket project

public class AppIT {

    @Test
    public void testApp() {
        assertThat(
            App.printHelloWorld(),
            is(equalTo("Hello World!")));
    }
}

Pipeline as code : Jenkinsfile

pipeline {
  agent any
  stages {
    stage('Initialize') {
      steps {
        sh '''
                    echo "PATH = ${PATH}"
                    echo "M2_HOME = ${M2_HOME}"
                '''
      }
    }
    stage('Build') {
      steps {
        echo 'Building..'
        sh 'mvn clean compile'
      }
    }
    stage('Tests') {
      parallel {
        stage('Unit tests') {
          steps {
            echo 'Testing..'
            sh 'mvn test'
          }
          post {
            always {
              junit '**/surefire-reports/**/*.xml'

            }

          }
        }
        stage('Integration test') {
          steps {
            echo 'Testing..'
            sh 'mvn verify failsafe:integration-test -Dskip.surefire.tests=true'
          }
          post {
            always {
              junit '**/surefire-reports/**/*.xml'

            }

          }
        }
      }
    }
    stage('Jacoco') {
      steps {
        jacoco()
      }
    }
    stage("Package"){
      steps{
        sh 'mvn package'
        archiveArtifacts(artifacts: '**/target/*.jar', fingerprint: true)
      }
    }
    stage('Sonar') {
      steps {
        sh '''mvn sonar:sonar \\
  -Dsonar.host.url=http://172.17.0.1:9000 \\
  -Dsonar.login=0309b3dc4d0ca5a4fc35d0f18428499c818616c6'''
      }
    }
    stage('Deploy') {
      steps {
        echo 'Deploying...'
      }
    }
  }
  tools {
    maven 'maven-3.5.3'
    jdk 'jdk8'
  }
}

Blue Ocean

From Bitbucket

Build status on branches...

From Bitbucket

... and Pull Requests!

From Bitbucket

Build the merged result of your Pull Request before it is merged

From Bitbucket

Create a multibranch pipeline

Based on Jenkinsfile configuration as code

Builds on each branch

Builds merged result of Pull Requests

Create a multibranch pipeline

Create a multibranch pipeline

Create a multibranch pipeline

Create a multibranch pipeline

Create a multibranch pipeline

Create a multibranch pipeline

Create a multibranch pipeline

Create a multibranch pipeline

Pipeline editor

Pipeline editor

Pipeline editor

Generates or update Jenkinsfile

Pipeline editor

Pipeline editor

Check it out!

Read more

Jenkins 2 & Bitbucket

By Marc Bouvier

Jenkins 2 & Bitbucket

  • 663