Jenkins Workflow is a game changing way to write automation jobs with Jenkins. Workflows can support simple, one-step hello-world type jobs to the most complex, parallel pipelines. Best of all, they support manual/automated intervention (eg: approvals) and also workflows survive Jenkins master restarts. Combining Jenkins Workflow with Docker can seriously reduce friction in your DevOps efforts. Come learn how.
5. The Automation Engine at the Heart of CD is Critical
5
Automation
Engine
Developer
Source
Code
Control
System
Compile
/BuildCode
Scan TestsCode
Commit
Results
Report
BuildCommit Test Stage Deploy
Deploy
Prod
Deploy
Stage
Complex Delivery Pipelines
Delivery of App and Config
Enterprise-grade
CD Automation
Platform
Continuous Delivery
ProdDev
6. Jenkins, with Workflow, is the Proven CD Platform
6
Developer
Source
Code
Control
System
Compile
/BuildCode
Scan TestsCode
Commit
Results
Report
BuildCommit Test Stage Deploy
Deploy
Prod
Deploy
Stage
Complex Delivery Pipelines
Delivery of App and Config
Jenkins Workflow
ProdDev
7. Jenkins is the Hub of the CD Ecosystem
7
Plug-ins for all
your tools
RunDeployStageTestBuildCommit
9. Jenkins Workflow CD Pipelines
9
Jenkins Workflow
ProdDev
Perf Test
BuildCommit
Selenium
Test Stage Deploy
Sonar
Test
Pipelines Need:
• Branching
• Looping
• Restarts
• Checkpoints
• Manual Input
??
10. Jenkins Workflow
10
• A first-class feature for managing complex, multi-step pipelines
• Brings the power of the Jenkins domain and plugin ecosystem into a Groovy
based scriptable Domain-Specific Language (DSL)
• Workflow is extensible by third-party developers, supporting custom
extensions to the Workflow DSL and various options for plugin integration
• Workflow scripts may be loaded directly from SCM
11. Key Workflow Features
11
• Entire flow is one concise Groovy script using Workflow DSL
– For loops,try-finally, fork-join,parallel …
• Workflow execution is resumable in the event the Jenkins master crashes or is
restarted
• Allocate slave nodes and workspaces
– As many as you want, when you want
• Stages allow throttling concurrency of builds
• Human input/approval integrated into flow
• Standard project concepts: SCM, artifacts,plugins
12. Your First Workflow
12
• Install the Workflow: Aggregator plugin*
• Click New Item, pick a name for your flow, select Workflow, and click OK
• The Script text area is where your flow script is defined
• Enter the following step into the Script text area:
echo 'Hello world'
• If you are not a Jenkins administrator, check the Use Groovy Sandbox option
• Save your workflow and click Build Now to run it
13. Workflow Snippet Generator
13
• The Workflow Snippet Generator
makes editing your workflows easier
• The Snippet Generator is dynamically
populated based on the Workflow
plugins you have installed
14. Workflow node Step
14
• The node step schedules a task to run by adding it to the Jenkins build
queue
• As soon as an executor slot is available on a node (the Jenkins master, or a
slave), the task is run on that node
• A node also allocates a workspace (file directory) on that node for the
duration of the task
• The body of the node step may contain many steps and some steps can only
run in the context of a node (git and sh for example)
• The node step takes a label expression to select a specific node type
15. Checking Out Code
15
• Simple syntax for retrieving source code, leveraging the many existing SCM
plugins for Jenkins:
checkout([$class: 'GitSCM', branches: [[name: '*/ master']],
userRemoteConfigs: [[url: 'http://github.com/ cloudbees/todo-
api.git']]])
• simplified syntax for Git based SCM:
git 'https://github.com/cloudbees/todo-api.git'
16. Workflow Stages
16
• Stages are usually the topmost element of Workflow syntax
• Stages allow you to group your build steps into its component parts
• Multiple builds of the same workflow can run concurrently,the stage step
allows you to control concurrency for a specific part of your flow:
stage 'build' node{ ... }
stage name: 'test', concurrency: 3 node{ ... }
stage name: 'deploy', concurrency: 1 node{ ... }
17. Workflow Parallelism
17
• The parallel construct allows you to run steps concurrenlty in the same
flow:
parallel (
"stream 1" : { ... things ... },
"stream 2" : { ... things in parallel ... }
)
• The parallel construct allows you to distribute a flow across multiple
nodes:
parallel (
"stream 1" : {
node { unstash "binary" sh "sleep 20s" sh "echo hstream1" }
}, "stream 2" : {
node { unstash "binary" sh "echo hello2" sh "hashtag fail" }
} )
18. Working with Files
18
• The stash and unstash steps share files between nodes
node{ stash includes: 'pom.xml', name: 'pom’}
node { unstash 'pom’}
• The archive step allows you to maintain binaries from your Workflow
build in Jenkins for easy access later:
archive includes: '*.jar, excludes: '*-sources.jar’
• Beyond stashing and archiving files, the following Workflow steps also work
with the file system:
pwd()
dir(''){}
writeFile file: 'target/results.txt', text: '' readFile
'target/results.txt'
fileExists 'target/results.txt'
19. Workflow Global Library
19
• Enables sharing ‘sub-scripts’ across multiple workflows
• Creates a ‘shared script library’ Git repository inside Jenkins
• Every workflow script in your Jenkins see these shared library scripts in their
classpath
• Add/modify scripts by cloning the repo and pushing to it
• You can define your own functions that looks and feels like built-in step
functions
• More info is available: https://github.com/jenkinsci/workflow-
plugin/blob/master/cps-global-lib/README.md
20. Mulitbranch Workflow
20
• A new Jenkins item type
• Configured as a Groovy CPS DSL from SCM
• Git, Subversion and Mercurial are currently supported
• Creates a sub-project (with separate build history, etc) for each branch in the
SCM project
• The Jenkins Multibranch Workflow job expects to find a script named
Jenkinsfile in branches it can build
22. How Does it Help You
22
• Customize slave allocation
• Run steps inside Docker containers
• Build and publish images
• Use custom registries and Docker hosts
• Running and testing images
24. Workflow Resources
24
• The GitHub project has documentations and tutorials:
https://github.com/jenkinsci/workflow-plugin
• Current plugin compatibility and notes for plugin developers:
https://github.com/jenkinsci/workflow-
plugin/blob/master/COMPATIBILITY.md
• There is a Dzone Refcard that lists all the core steps and some additional
steps: https://dzone.com/refcardz/continuous-delivery-with-jenkins-
workflow
• Numerous blog posts – do a Google search as there are too many to list!