25
[email protected] exadat.co.uk Chris Adkin chrisadkin.io Chris Adkin [email protected] CI With Jenkins, Docker and SQL Server

Continuous Integration With Jenkins Docker SQL Server

Embed Size (px)

Citation preview

Page 1: Continuous Integration With Jenkins Docker SQL Server

[email protected] Adkin chrisadkin.ioChris Adkin [email protected]

CI With Jenkins, Docker and SQL Server

Page 2: Continuous Integration With Jenkins Docker SQL Server

Build Pipeline 101

2. Build code into a deployable artefact

1. Check code out of source code control

3. Deploy code to test target

4. Run Tests

Apache Ant

Apache Maven

Gradle

Microsoft msbuild

Git

GitHub

Bitbucket

Team Foundation Server

Containers

Web servers

Databases

Public cloud PaaS

JUnit

Selenium

Mocha

Pester

Page 3: Continuous Integration With Jenkins Docker SQL Server

Build Pipeline As Code !!!

2. Build code into a deployable artefact

1. Check code out of source code control

3. Deploy code to test target

4. Run Tests

Jenkins Groovy DSL

(DSL = Domain specific language) Jenkins script Jenkins declarative pipeline

TFS / VSTS Pipeline as YAML in VSTS preview Coming to TFS sometime next year

Page 4: Continuous Integration With Jenkins Docker SQL Server

The ‘New’ Build Pipeline

2. Build code into a deployable artefact

1. Check code out of source code control

5. Deploy containerimage to a registry

4. Run Tests3. Deploy code to test target

Page 5: Continuous Integration With Jenkins Docker SQL Server

Builds Can Be Event Triggered

1. Checkout code 2. Build artefact 3. Deploy to target 4. Run Tests

Git/GitHub webhooks

Visual studio online project service hooks

Wouldn’t it be nice if we could have event driven builds and spin-up and then tare down the

deployment target ?

Page 6: Continuous Integration With Jenkins Docker SQL Server

Containers To The Rescue !!!

We can fully container-ise the build environment

Scale-out the build infrastructure elastically using containers

Spin-up “Side car” containers as deployment targets

The “Builder pattern”

Page 7: Continuous Integration With Jenkins Docker SQL Server

The ‘Builder’ Pattern

Spin-up a container to ‘Build’ the artefact here

Deploy artefact to ‘Run’ container here

Rationale: keep the size of the ‘Run’ container image as small as possible by not including the build tool chain

Page 8: Continuous Integration With Jenkins Docker SQL Server

Build Engines – There Are Lots To Choose From !!!

Jenkins: Great open-source plugin support

Free

Light weight

Can be fully container-ised and scaled out with containers

Strong build-pipeline-as-code support

Multi-branch build pipelines

Page 9: Continuous Integration With Jenkins Docker SQL Server

Jenkins Pipeline As Code: The Two Flavours

Scripted Pipeline Syntax

node {

stage('Example') {

try {

sh 'exit 1'

}

catch (exc) {

echo ‘Failure, sound the alarm!'

throw

}

}

}

Declarative Pipeline syntax

pipeline {

agent any

stages {

stage('Example') {

steps {

echo 'Hello World'

}

}

}

post {

always {

echo 'I will always say Hello again!'

}

}

}

https://jenkins.io/doc/book/pipeline/syntax/

Page 10: Continuous Integration With Jenkins Docker SQL Server

Store Your Pipeline Alongside Your Code

Your pipeline code goes in

here, this is formulti-branch

pipelines

Page 11: Continuous Integration With Jenkins Docker SQL Server

“Syntactic sugar” For Working With Containers

node('docker')

{

docker.withServer('tcp://docker.beedemo.net:2376', 'docker-beedemo-creds')

{

stage('Build Docker Image’)

{

def image = docker.build "cloudbees/mobile-deposit-api:${buildVersion}”

}

stage('Publish Docker Image’)

{

docker.withRegistry('https://registry.beedemo.net/', 'docker-registry-login')

{

image.push()

}

}

stage('Deploy Docker Image’)

{

def container = image.run('--name mobile-deposit-api -p 8080:8080')

}

}

}

Page 12: Continuous Integration With Jenkins Docker SQL Server

Demonstrations

May the demonstration gods smile upon me

Page 13: Continuous Integration With Jenkins Docker SQL Server

Demonstration: A Simple Web hook

sqlproj DACPAC

1. Open visual studio project, make a

change and perform a commit

2. Check code out of git

3. Build DACPAC from visual studio

project

4. Deploy code to SQL Server

GitHub repo: https://github.com/chrisadkin/SsdtDevOpsDemo, Blog Post

Page 14: Continuous Integration With Jenkins Docker SQL Server

Demonstration: A Multi-branch Build Pipeline

Master

HotFix

Feature

Jenkins creates a pipeline for each branch created from the masterGitHub repo: https://github.com/chrisadkin/SelfBuildPipeline, Blog Post

Page 15: Continuous Integration With Jenkins Docker SQL Server

Demonstration: Using A Jenkins Build Slave

Jenkins Build Master( orchestrate build here )

Jenkins Build Slave( deploy to container here )

GitHub repo: https://github.com/chrisadkin/SelfBuildPipelineDV, Blog Post

Page 16: Continuous Integration With Jenkins Docker SQL Server

Demonstration: Image Layering

Base Image

Intermediate images

Final Image

Page 17: Continuous Integration With Jenkins Docker SQL Server

Pro Tip #1 Constructing Dockerfiles

Put slow and timeconsuming operations as close to the top of

the file as possible

Put fast operations toward the bottom of

the Dockerfile

Page 18: Continuous Integration With Jenkins Docker SQL Server

Pro Tip #2 Use of FROM Clauses In Dockerfiles

The Dockerfile should only contain 1 FROM

clause

Page 19: Continuous Integration With Jenkins Docker SQL Server

Pro Tip #3 Use Timeout Wrappers

timeout(time:2, unit:'MINUTES') {

bat "docker run -d -e ACCEPT_EULA=Y -e SA_PASSWORD=P@ssword1 --name

SQLLinuxmaster -d -i -p 15565:1433 microsoft/mssql-server-linux"

}

My samples do not use this feature, but you may wish to consider this for pipelines used in actual production

Page 20: Continuous Integration With Jenkins Docker SQL Server

Demonstration: A Fully Containerised Build Environment

Jenkins Master SQL Server “Side car”Deployment target

GitHub repo: https://github.com/chrisadkin/SsdtJenkinsCiInDocker, Blog Post

Page 21: Continuous Integration With Jenkins Docker SQL Server

Demonstration: Adding tSQLt Unit Testing To The Pipeline

sqlproj DACPAC

1. Open visual studio project, make a

change and perform a commit

2. Check code out of git

3. Build DACPAC from visual studio

project

4. Deploy code to SQL Server

GitHub repo: https://github.com/chrisadkin/SelfBuildPipelineDV_tSQLt, Blog post coming soon . . .

5. Run tSQLttests

Page 22: Continuous Integration With Jenkins Docker SQL Server

Want To Get More Adventurous ?

Scale out your build platform with build slaves as containers

Deploy Jenkins to Kubernetes

Build shared groovy script libraries to share code across pipelines

Invoke builds from the REST API

Page 23: Continuous Integration With Jenkins Docker SQL Server

Some Final Words

Linux images tend to be smaller, faster and more stable than their windows counterparts: windowsservercore 10.4 GB ubuntu 123 MB Alpine 3.97 MB ( .Net core 2.1 will run on this)

Docker community edition can be temperamental

People including myself have had difficulty getting the docker SQL Server lab to work

Jenkins is open source, things can break from one release to the next !!!

Page 24: Continuous Integration With Jenkins Docker SQL Server

Further Reading

Top 10 Jenkins best practices from CloudBees

Jenkins Pipeline as code from Jenkins.io, includes: Demos

Downloadable examples

Articles

Blogposts

Recordings

Page 25: Continuous Integration With Jenkins Docker SQL Server

. . . any questions