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) -
It is the "Legacy UI"
-
Switch to BlueOcean, the new UI
-
Direct link: {jenkins-url}/blue
-
Or click on the top button "Open Blue Ocean"
-
-
Create your 1st Pipeline:
-
Stored in Git
-
Fetch URL from the Gitserver
-
Direct link: {gitserver-url}/butler/demoapp.git
-
-
Add a User/password credential (
butler
/butler
) -
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
-
Direct link: {gitserver-url}/butler/demoapp/settings/hooks
-
-
Add a Gogs webhook:
-
Payload URL: {jenkins-url}/job/demoapp/build?delay=0
-
When should this webhook be triggered?: I need everything
-
-
Pipeline-as-code: We need a
Jenkinsfile
-
Declarative or Scripted ?
-
-
Where to start ?
-
Documentation: https://jenkins.io/doc/pipeline
-
Getting Started: https://jenkins.io/doc/pipeline/tour/hello-world/
-
Syntax Reference: https://jenkins.io/doc/book/pipeline/syntax/
-
-
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 !
-
-
Let’s hack: open the BlueOcean Pipeline Editor
-
Direct (hidden) URL: {jenkins-url}/blue/organizations/jenkins/pipeline-editor/
-
Use
CTRL + S
(On Mac:CMD +S
) to switch to textual version
-
-
Also, use the "Legacy" Pipeline Syntax Snippet Generator:
-
Use the BO 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 demoapp
-
We want 4 stages, for the 4 Maven goals:
-
clean 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('Verify') {
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 in sucess
-
-
Clues: Keywords
post
+success
(not in Editor), andarchiveArtifacts
pipeline {
agent {
node {
label 'maven'
}
}
stages {
stage('Build') {
steps {
sh 'mvn clean compile test 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