You are on page 1of 33

Discovering Jenkins Pipelines!

Empower your Continuous Delivery possibilities

Medallia © Copyright 2017. 1


Who am I?
TL;DR: Braian Iván Monnier
• Front-end Software Engineer @ Medallia
• We work with ES7, Angular, Karma, React, Jest, Node.js, Docker

• Passionate about solving problems :)

• Interested on Continuous Delivery and Security

• Working with Jenkins for more than a year

Medallia © Copyright 2017. 2


AGENDA
1 Why Continuous Delivery Pipelines?

2 Pipelines on Jenkins

3 Groovy???

4 Let's dig into syntax!

5 Steps

6 Thank you :)

Medallia © Copyright 2017. 3


AGENDA
1 Why Continuous Delivery Pipelines?

2 Pipelines on Jenkins

3 Groovy???

4 Let's dig into syntax!

5 Steps

6 Thank you :)

Medallia © Copyright 2017. 4


Why Continuous Delivery?
• As an organization/business, we believe we must:
• Deliver high quality products to customers
• Do it as fast as possible, with an automated process
• Avoid developers from thinking on different environments
(production, staging, etc) while coding

Medallia © Copyright 2017. 5


Continuous Delivery?
• Natural extension of Continuous Integration
• Checks for additional "acceptance tests" against our codebase
• Must ensure code is release-ready, generating deployable units
• No manual overhead
• Continuous Delivery != Continuous Deployment

Medallia © Copyright 2017. 6


Pipeline?

A Continuous Delivery Pipeline is the


complete suite of steps to generate
the deployable units; it is the actual PLACE IMAGE OVER THIS SPACE

execution and "wiring" of the abstract


idea behind Continuous Delivery.

Medallia © Copyright 2017. 7


Continuous Delivery
Best practices for Pipelines

• Pipeline definition must be independent from runtime configuration


• Create a repeatable and reliable process, to incrementally improve it
• Always use a clean/pristine environment
• Reuse same builds during the whole process
• Servers must have same (or very similar) configuration to the one used
in Production

Medallia © Copyright 2017. 8


Continuous Delivery
vs Continuous Deployment

Medallia © Copyright 2017. 9


AGENDA
1 Why Continuous Delivery Pipelines?

2 Pipelines on Jenkins

3 Groovy???

4 Let's dig into syntax!

5 Steps

6 Thank you :)

Medallia © Copyright 2017. 10


Pipelines on Jenkins
Common terms

• Node/Slave: It's an executor, where a new workspace is allocated and


assigned to a specific build.
• Stage: The complete definition is divided into stages, each one with a
single responsibility, e.g. Build Automation, Health Checks, etc.
• Steps: All the different actions performed inside a stage (these are the
Pipeline DSL methods).

Medallia © Copyright 2017. 11


Pipelines on Jenkins
Job types

• Pipeline: It's the smallest unit for defining Continuous Delivery


Pipelines. An inline definition can be set, or taken from a CSM.
• Multibranch Pipeline: Creates a set of Pipelines for both branches and
Pull Requests for a given repo, customizing rules on how to create
them.
• GitHub Organization: Extends the previous one, to also create one
Multibranch Pipeline for each repo hitting specified rules.

Medallia © Copyright 2017. 12


Medallia © Copyright 2017. 13
Pipelines on Jenkins
Key advantages

• Ability to suspend/resume jobs, by implementing CPS


(Continuation-passing Style)
• Supports a set of Steps or pipeline-specific methods (Pipeline DSL)
• Steps are fully extensible, both through plugins or shared libraries
• Automatically sandboxed
• Turing-complete

Medallia © Copyright 2017. 14


Pipelines on Jenkins
Key advantages

• Pipeline definition can be versioned (as Jenkinsfile by default), having


a Single Source of Truth
• Pipeline definition can be executed per branch and Pull Request
• Separates Pipeline definition from Job itself
• Written in Groovy

Medallia © Copyright 2017. 15


AGENDA
1 Why Continuous Delivery Pipelines?

2 Pipelines on Jenkins

3 Groovy???

4 Let's dig into syntax!

5 Steps

6 Thank you :)

Medallia © Copyright 2017. 16


Groovy???
Don't panic!

• Superset of Java, meaning all Java code is valid


• Not knowing it won't stop you from writing simple Pipeline definitions
• Has lots of flexibility:
• Allows loosely-type programming (def keyword)
• Implicit casting
• It's not required, but recommended to learn about Groovy

Medallia © Copyright 2017. 17


AGENDA
1 Why Continuous Delivery Pipelines?

2 Pipelines on Jenkins

3 Groovy???

4 Let's dig into syntax!

5 Steps

6 Thank you :)

Medallia © Copyright 2017. 18


Jenkins Pipeline Syntax
Declarative

• It's an extension to the Pipeline DSL, which specifies a special syntax


• Definition can only be specified via Sections and Directives
• Easy to recognize: All code is enclosed by a pipeline { } block
• Very handy for newcomers
• Adds simple and powerful post-action hooks
• Recommended by Jenkins nowadays!

Medallia © Copyright 2017. 19


Medallia © Copyright 2017. 20
Jenkins Pipeline Syntax
Scripted

• Pure Groovy-based
• Huge amount of flexibility and extensibility, like loops, flow control,
exceptions handling, etc.
• Complete freedom on how to define the pipeline
• More compact (no Sections required)
• Groovy learning-curve may be not desired

Medallia © Copyright 2017. 21


Medallia © Copyright 2017. 22
Medallia © Copyright 2017. 23
Medallia © Copyright 2017. 24
AGENDA
1 Why Continuous Delivery Pipelines?

2 Pipelines on Jenkins

3 Groovy???

4 Let's dig into syntax!

5 Steps

6 Thank you :)

Medallia © Copyright 2017. 25


Steps or Pipeline DSL
Why a custom solution?

Jenkins need to "communicate" all steps to the corresponding allocated


node for the given build. This is done by the Pipeline DSL methods.
Build execution state is constantly serialized and saved so that it can
survive both planned and unplanned restarts on master.

Medallia © Copyright 2017. 26


Pipeline DSL
Some steps

• node: Allocates an executor on a node, and a workspace


• ws: Allocates an alternative workspace
• stage: Defines a stage
• sh: Runs a shell command
• pwd: Returns current workspace directory
• dir: Changes working directory on workspace

Medallia © Copyright 2017. 27


Pipeline DSL
More steps!!

• echo: Prints a message.


• readFile/writeFile: Reads and writes a file from/into the workspace.
• stash/unstash: Saves and retrieves a set of files for use later in the
same build, generally on another node/workspace.
• checkout: Special step that allows to checkout a repo given Job
config. A special scm variable gets available with related info.

Medallia © Copyright 2017. 28


Pipeline DSL
Much more steps!

• tool: Installs a tool on the workspace.


• build: Triggers a new build for the given job.
• retry: Retries the contained steps up to N times.
• parallel: Executes sub-workflow in parallel.
• checkpoint: Saves the state of build execution to be restarted later.
• input: Pauses build execution and waits for user input.

Medallia © Copyright 2017. 29


Pipeline DSL
Wait! IT'S MORE!!!!!

• archive: Archives an artifact.


• unarchive: Copies an archived artifact into the build.

Check the entire list here!

Medallia © Copyright 2017. 30


Recap
• Continuous Delivery enables you to securely deliver your product as
fast as possible!
• Jenkins v2 allows to write CD Pipeline definitions "as Code"
• Supports two different syntaxes, Declarative and Scripted
• Groovy knowledge is desired, but not a blocker
• Jenkins serializes the state of a build, so it can be paused/resumed
• Steps (or Pipeline DSL methods) are fully extensible!

Medallia © Copyright 2017. 31


Q&A
Medallia © Copyright 2017. 32
Thank you!!!
See you next time :)

Braian Iván Monnier


braian@medallia.com

Medallia © Copyright 2017. 33

You might also like