Omar Lopesino

Drupal Developer

drupal.org/u/mistermoper
Contributed modules: json_form, slickquiz_field...

In this session we will see:

  1. Why multibranch testing?
  2. Jenkins multibranch testing example
  3. How to Jenkins multibranch
  4. Use case
  5. What more can we do?
First

Why multibranch testing

Objective

Branch model

Source: Branching workflows

What to test

But should I have to run all these tests manually?

Jenkins to the rescue!

Parallel testing

Second

Jenkins multibranch testing example

Jenkins multibranch testing example

Automated tests for each git repository branch.

Jenkins multibranch testing example

Automated tests for each git repository branch.

Jenkins multibranch testing example

Automated tests for each git repository branch.

Jenkins multibranch testing example

Automated tests for each git repository branch.

Jenkins multibranch testing example

Notification

Jenkins multibranch testing example

Code static analysis

Jenkins multibranch testing example

Test results

Third

How to Jenkins multibranch

How to Jenkins multibranch

Which tools do we need?

  • Docker
  • Docker compose
  • Jenkins job

How to Jenkins multibranch

  1. Develop a docker infraestructure for drupal

  2. Create Jenkins pipeline

  3. Create Jenkins multibranch pipeline job

How to Jenkins multibranch

Develop a docker infraestructure for drupal

Example: Docker4Drupal

How to Jenkins multibranch

Create Jenkins multibranch pipeline job

  • Pipeline job code-written
  • Committed in repository
  • Jenkins will create a job for each branch that contains this file.
Source
Talk is cheap. Show me the code
Talk is cheap. Show me the code

How to Jenkins multibranch

Create Jenkins multibranch pipeline job: Skeleton

pipeline {
    agent any
    stages {
        stage('Docker start') { ... }
        stage('Composer') { ... }
        stage('Static Analysis') { ... }
        stage('Unit tests') { ... }
        stage('Database sync') { ... }
        stage('Functional tests') { ... }
    }
    post { ... }
}

How to Jenkins multibranch

Create Jenkins multibranch pipeline job: Docker start

pipeline {
    agent any
    stages {
        stage('Docker start') {
            sh "docker-compose up -d"	
        }
    }
}

How to Jenkins multibranch

Create Jenkins multibranch pipeline job: Composer

pipeline {
    agent any
    stages {
        stage('Docker start') { ... }
        stage('Composer') {
            sh "docker-compose exec php -T composer install"
        }
    }
}

How to Jenkins multibranch

Creating Jenkinsfile: Static analysis

pipeline {
    agent any
    stages {
        stage('Docker start') { ... }
        stage('Composer') { ... }
        stage('Static Analysis') {
          sh "docker-compose exec php -T 
            ./vendor/bin/phpcs --standard=Drupal,DrupalPractice"
        }
    }
}

How to Jenkins multibranch

Creating Jenkinsfile: Unit tests

pipeline {
    agent any
    stages {
        stage('Docker start') { ... }
        stage('Composer') { ... }
        stage('Static Analysis') { ... }
        stage('Unit tests') {
          sh "docker-compose exec php -T 
            ./vendor/bin/phpunit -c phpunit.xml"
        }
    }
}

How to Jenkins multibranch

Creating Jenkinsfile: Database sync

pipeline {
    agent any
    stages {
        stage('Docker start') { ... }
        stage('Composer') { ... }
        stage('Static Analysis') { ... }
        stage('Unit tests') { ... }
        stage('Database sync') {
          sh "drush @my-environment sql:sync"
          // sanitize, updb, cim, cr, file sync..
        }
    }
}

How to Jenkins multibranch

Creating Jenkinsfile: Functional tests

pipeline {
    agent any
    stages {
        stage('Docker start') { ... }
        stage('Composer') { ... }
        stage('Static Analysis') { ... }
        stage('Unit tests') { ... }
        stage('Database sync') { ... }
        stage('Functional tests') {
          sh "docker-compose exec php -T 
            vendor/bin/behat --config tests/behat/behat.yml"
        }
    }
}

How to Jenkins multibranch

Creating Jenkinsfile: Post steps

pipeline {
    agent any
    stages { ... }
    post {
      always {
        sh "docker-compose down"
      }
      success {
        slackSend color: "#00FF00", 
          channel: "#myproject",
          message: "Tests passes, hurray!"
      }
      failure { ... }
      changed { ... }
    }
}

How to Jenkins multibranch

Create multibranch pipeline job

Fourth

Use case

Use case

  • Allow editors edit any article
  • Accidentally enable delete any article permission.

Use case

Use case

Fifth

What more can we do?

What more can we do?

  • Rest services: Postman tests automated with newman

What more can we do?

  • Rest services: Postman tests automated with newman
  • Check updates: modules, security updates.

What more can we do?

Patches applied in our site

Conclusions

  • To deploy new features multibranch testing can be used as quality assurance method.
  • It helps to detect regressions that allows fail early.
  • Automated and integrated.

Any questions?

Thanks!!!

@omarlopesino