/jenkinsfiles

Examples for jenkins pipelines, comparing scripted and declarative syntax

MIT LicenseMIT

cloudogu/jenkinsfiles

This project contains examples for the Jenkins pipeline plugin, comparing both declarative and scripted syntax.

The examples were developed while working on an article series called Coding Continuous Delivery published in Java aktuell. Both English translation and German original can be found on the Cloudogu Blog.

01/2018 (covering examples on branches 1 to 5)
πŸ‡¬πŸ‡§ Jenkins pipeline plugin basics
πŸ‡©πŸ‡ͺ Grundlagen des Jenkins-Pipeline-Plug-ins
02/2018 (covering examples on branches 6 and 7)
πŸ‡¬πŸ‡§ Performance optimization for the Jenkins Pipeline
πŸ‡©πŸ‡ͺ Performance Optimierung fΓΌr die Jenkins Pipeline
03/2018 (covering examples on branches 8 and 9)
πŸ‡¬πŸ‡§ Helpful Tools for the Jenkins Pipeline
πŸ‡©πŸ‡ͺ Hilfreiche Werkzeuge fΓΌr die Jenkins Pipeline
04/2018 (covering examples on branches 10 and 11)
πŸ‡¬πŸ‡§ Statical Code Analysis with SonarQube and deployment to Kubernetes et al. with Jenkins Pipelines
πŸ‡©πŸ‡ͺ Statische Code Analyse mit SonarQube und Deployment auf Kubernetes et al. mit Jenkins Pipelines

The project being built by the pipeline examples is wildfly/quickstart/kitchensink, a typical JEE web app basing on CDI, JSF, JPA, EJB, JAX-RS and integration tests with arquillian. It was extended slightly to allow for running integration tests using WildFly Swarm and (in branch 11-x) to provide its version name via REST.

The pipeline examples are built on top of each other, each in declarative and scripted syntax, respectively. Each example is put on a separate branches for convenient access.

Please see our Jenkins Instance for build results.

The following aspects are covered by the examples:

  1. A simple pipeline (declarative | scripted)
  2. Improving maintainability by introducing custom steps (declarative | scripted)
  3. Division into smaller stages (declarative | scripted)
  4. End of pipeline a Handling failures (declarative | scripted)
    b Simplified Mailing (declarative | scripted)
  5. Archive and Properties/Options (declarative | scripted)
  6. Parallel (declarative | scripted)
  7. Time Triggered Builds (e.g. nightly) (declarative | scripted)
  8. Shared libraries (declarative | scripted | shared library)
  9. Docker
    a Run whole pipeline inside a Docker container (declarative | scripted)
    b Using Docker inside a custom step (implemented in shared library) (declarative | scripted | shared library)
  10. Statical code analysis with SonarQube
    a check quality gate outside of node (as shown within the docs) (declarative | scripted)
    b analysis and quality gate within one stage (pragmatic, easier to maintain) (declarative | scripted)
  11. Deployment to Kubernetes (declarative | scripted)

Jenkins Setup

All examples (obviously) need the Jenkins Pipeline plugin installed. It has been part of the Jenkins default plugins for quite some time.

8. Shared libraries

Starting with branch 8, the Pipeline: GitHub Groovy Libraries plugin is needed so the libraries are loaded out of the box. It is also one of the Jenkins default plugins.

9. Docker

Branch 9 and up require the Docker Pipeline plugin (installed by default) and only run on workers that have a working docker client (docker binary on the PATH). In order to distinguish this workers from others, they provide a docker label.

10. SonarQube

From branch 10 the SonarQube Scanner plugin and a SonarQube instance sonarcloud.io set up in Jenkins. In addition, in SonarQube, a webhook to https://JENKINS/sonarqube-webhook must be configured.

Before Jenkins is able to analyse the branches a first analysis has to be done (e.g. from your developer machine) analysis, e.g. like so

mvn clean install -Pjenkins -DskipITs
mvn sonar:sonar -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=<SECURITY TOKEN> -Dsonar.organization=<YOUR-ORG-KEY>

If you want the quality gate to fail (for showcases), one option is to analyse one of the 10a branches but remove the class Untested before.

11. Kubernetes

In order to deploy to Kubernetes the examples in the 11-x-branches require the Kubernetes Continuous Deploy plugin. The following must be executed (from one of the 11-x-branches) before the build can succeed.

kubectl apply -f k8s/namespace.yaml
kubectl apply --namespace jenkins-ns -f k8s/service.yaml,k8s/serviceaccount.yaml
k8s/create-kubeconfig jenkins-sa --namespace=jenkins-ns > jenkins.kubeconfig

Then, add the jenkins.kubeconfig as Jenkins file credential, called kubeconfig-oss-deployer.
Finally, add a Username and Password credential called hub.docker.com-cesmarvin (e.g. on GCR, the user is _json_key and the password is the a JSON in single quotes with line breaks removed - cat account.json). and change the image name to match your registry.

Jenkins Build status

Branch Declarative Scripted Library/SQ
1. Simple pipeline Build Status Build Status
2. Custom steps Build Status Build Status
3. Smaller Stages Build Status Build Status
4a Handling failures Build Status Build Status
4b Simplified Mailing Build Status Build Status
5. Archive and Properties/Options Build Status Build Status
6. Parallel Build Status Build Status
7. Time Triggered Builds Build Status Build Status
8. Shared libraries Build Status Build Status Build Status
9a Docker (whole pipeline in container) Build Status Build Status
9b Docker (inside custom step) Build Status Build Status Build Status
10a SonarQube (as shown in docs) Build Status
See SQ!
Build Status
See SQ!
SonarQube Badge
10b SonarQube (pragmatic) Build Status
See SQ!
Build Status
See SQ!
SonarQube Badge
11. Kubernetes Build Status Build Status

Further resources

Jenkinsfiles

Shared libraries

A collection of shared libraries to be used as reference.

Others