aka "CI"
Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily, leading to multiple integrations per day.
-
Access your Jenkins instance:
-
Log in as the user
butler
(password is the same) -
This is the "Jenkins Classic GUI"
-
Switch to BlueOcean, the new UI
-
Or click on the top button "Open Blue Ocean"
-
Create your 1st Pipeline:
-
Stored in Git
-
Fetch URL from the Gitserver
-
Add a User/password credential (
{admin-user}
/{admin-user}
) -
Pipeline is empty (for now): no
Jenkinsfile
-
-
We want Fast feedback !
-
Pushed code to repository ? Tell Jenkins to build it now
-
-
Let’s use Webhook to the repository
-
HTTP request Gitserver → Jenkins
-
-
From repo. in Gitserver → Settings → Webhooks
-
Add a new webhook:
-
Type: Gogs (not Slack)
-
Payload URL: {demoapp-mb-job-build-url}
-
When should this webhook be triggered?: I need everything
-
-
Pipeline-as-code: We need a
Jenkinsfile
-
Where to start ?
-
Declarative
-
Easy syntax
-
Default syntax
-
Start with this one
-
-
Scripted
-
Original syntax (~3 years)
-
"Great Power == Great Responsibility"
-
Use it when Declarative starts to be weird
-
-
Provides the full round trip with SCM
-
No Pipeline ? Follow the wizard (not Gandalf, fool !)
-
Already have a Pipeline ? Edit, commit, run it
-
Needs a compliant SCM
-
Only Github with BO 1.0.1
-
Interested ? Open-Source: Contribute !
-
-
Git is not supported (yet): let’s hack
-
Open the hidden BlueOcean Pipeline Editor: Direct URL
-
Use
CTRL + S
(On Mac:CMD +S
) to switch to/from textual version
-
-
The Pipeline Syntax Snippet Generator is useful:
-
Dynamic generation based on the installed plugins
-
A pipeline job is required: check the left menu icon on {demoapp-mb-job-url}
-
-
Use the BlueOcean Pipeline Editor and Gitserver
-
Create a Pipeline that have a single stage "Hello"
-
This stage have 1 step that prints the message "Hello World"
-
Copy/Paste this Pipeline in a new file
Jenkinsfile
on the repository root -
A build will kick off immediately:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Hello World !'
}
}
}
}
-
Exercise: Implement a simple build pipeline for demoapp
-
We want 4 stages, for the 4 Maven goals:
-
compile
,test
,package
,verify
-
-
We need to build on the
maven
agent
pipeline {
agent {
node {
label 'maven'
}
}
stages {
stage('Compile') {
steps {
sh 'mvn compile'
}
}
stage('Unit Tests') {
steps {
sh 'mvn test'
}
}
stage('Build') {
steps {
sh 'mvn package'
}
}
stage('Integration Tests') {
steps {
sh 'mvn verify'
}
}
}
}
-
We want to simplify to 2 stages, based on Unit Tests definition:
-
Build
: compile, unit test and package the application -
Verify
: Run Integration Tests
-
-
We also want to archive the generated
jar
file-
Only if the build is successful
-
-
Clues: Keywords
post
+success
(not in Editor), andarchiveArtifacts
pipeline {
agent {
node {
label 'maven'
}
}
stages {
stage('Build') {
steps {
sh 'mvn package'
}
}
stage('Verify') {
steps {
sh 'mvn verify'
}
}
}
post {
success {
archiveArtifacts 'target/demoapp.jar'
}
}
}
-
We want the integration test reports to be published to Jenkins
-
Better feedback loop
-
-
If Integration Tests are failing, do NOT fail the build
-
Make it UNSTABLE instead
-
-
Clues:
-
Maven flag
-fn
("Fails Never") -
keyword
junit
(Pipeline keyword)
-
pipeline {
agent {
node {
label 'maven'
}
}
stages {
stage('Build') {
steps {
sh 'mvn clean compile test package'
}
}
stage('Verify') {
steps {
sh 'mvn verify -fn'
junit '**/target/failsafe-reports/*.xml'
}
}
}
post {
success {
archiveArtifacts 'target/demoapp.jar'
}
}
}
-
We now want all test reports published
-
Problem: how to handle Unit test failure ?
-
-
We also want to archive artifacts if build is unstable only due to the
Verify
stage -
Clues:
post
can be used per stage
pipeline {
agent {
node {
label 'maven'
}
}
stages {
stage('Build') {
steps {
sh 'mvn clean compile test package'
}
post {
always {
junit '**/target/surefire-reports/*.xml'
}
}
}
stage('Verify') {
steps {
sh 'mvn verify -fn'
junit '**/target/failsafe-reports/*.xml'
}
post {
unstable {
archiveArtifacts 'target/demoapp.jar'
}
}
}
}
post {
success {
archiveArtifacts 'target/demoapp.jar'
}
}
}
-
Validate your changes by making your tests fails.
-
Edit each one and uncomment the failing block:
-
Integration:
src/master/src/test/java/hello/ApplicationIT.java
-
Unit Tests:
src/master/src/test/java/hello/ApplicationTest.java
-
-
Browse the top-level items "Changes", "Tests" and "Artifacts"
-
Do NOT forget to correct your tests at the end