Devops Project 2 Develop Jenkins Pipeline Project

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

DevOps Project 2 Develop Jenkins Pipeline Project

What is a Jenkins Pipeline?

Jenkins Pipeline (or simply "Pipeline") is a suite of plugins which supports implementing and
integrating continuous delivery pipelines into Jenkins.

A continuous delivery pipeline is an automated expression of your process for getting software
from version control right through to your users and customers.

Jenkins Pipeline provides an extensible set of tools for modeling simple-to-complex delivery
pipelines "as code". The definition of a Jenkins Pipeline is typically written into a text file (called
a Jenkinsfile) which in turn is checked into a project’s source control repository. [1]

For more information about Pipeline and what a Jenkinsfile is, refer to the
respective Pipeline and Using a Jenkinsfile sections of the User Handbook.

To get started quickly with Pipeline:

1. Copy one of the examples below into your repository and name it Jenkinsfile

2. Click the New Item menu within Jenkins 


3. Provide a name for your new item (e.g. My-Pipeline) and select Multibranch Pipeline
DevOps Project 2 Develop Jenkins Pipeline Project
4. Click the Add Source button, choose the type of repository you want to use and fill in the
details.
5. Click the Save button and watch your first Pipeline run!
You may need to modify one of the example Jenkinsfile's to make it run with your project. Try
modifying the sh command to run the same command you would run on your local machine.

After you have setup your Pipeline, Jenkins will automatically detect any new Branches or Pull
Requests that are created in your repository and start running Pipelines for them.

Continue to "Run multiple steps"

Quick Start Examples


Below are some easily copied and pasted examples of a simple Pipeline with various languages.

Java

Jenkinsfile (Declarative Pipeline)


pipeline {
agent { docker { image 'maven:3.8.4-openjdk-11-slim' } }
stages {
stage('build') {
steps {
sh 'mvn --version'
}
}
}
}

Toggle Scripted Pipeline (Advanced)


Node.js / JavaScript

Jenkinsfile (Declarative Pipeline)


pipeline {
agent { docker { image 'node:16.13.1-alpine' } }
stages {
stage('build') {
steps {
sh 'node --version'
}
}
}
}

Toggle Scripted Pipeline (Advanced)


Ruby

Jenkinsfile (Declarative Pipeline)


pipeline {
DevOps Project 2 Develop Jenkins Pipeline Project
agent { docker { image 'ruby:3.0.3-alpine' } }
stages {
stage('build') {
steps {
sh 'ruby --version'
}
}
}
}

Toggle Scripted Pipeline (Advanced)


Python

Jenkinsfile (Declarative Pipeline)


pipeline {
agent { docker { image 'python:3.10.1-alpine' } }
stages {
stage('build') {
steps {
sh 'python --version'
}
}
}
}

Toggle Scripted Pipeline (Advanced)


PHP

Jenkinsfile (Declarative Pipeline)


pipeline {
agent { docker { image 'php:8.1.0-alpine' } }
stages {
stage('build') {
steps {
sh 'php --version'
}
}
}
}

Toggle Scripted Pipeline (Advanced)


Go

Jenkinsfile (Declarative Pipeline)


pipeline {
agent { docker { image 'golang:1.17.5-alpine' } }
stages {
stage('build') {
steps {
sh 'go version'
}
}
}
DevOps Project 2 Develop Jenkins Pipeline Project
}

pipelin
e {
agent any
environment {
//be sure to replace "bhavukm" with your own Docker Hub username
DOCKER_IMAGE_NAME = "bhavukm/train-schedule"
}
stages {
stage('Build') {
steps {
echo 'Running build automation'
sh './gradlew build --no-daemon'
archiveArtifacts artifacts: 'dist/trainSchedule.zip'
}
}
stage('Build Docker Image') {
when {
branch 'master'
}
steps {
script {
app = docker.build(DOCKER_IMAGE_NAME)
app.inside {
sh 'echo Hello, World!'
}
}
}
}
stage('Push Docker Image') {
when {
branch 'master'
}
DevOps Project 2 Develop Jenkins Pipeline Project
steps {
script {
docker.withRegistry('https://registry.hub.docker.com',
'docker_hub_login') {
app.push("${env.BUILD_NUMBER}")
app.push("latest")
}
}
}
}
stage('CanaryDeploy') {
when {
branch 'master'
}
environment {
CANARY_REPLICAS = 1
}
steps {
kubernetesDeploy(
kubeconfigId: 'kubeconfig',
configs: 'train-schedule-kube-canary.yml',
enableConfigSubstitution: true
)
}
}
stage('DeployToProduction') {
when {
branch 'master'
}
environment {
CANARY_REPLICAS = 0
}
steps {
input 'Deploy to Production?'
milestone(1)
kubernetesDeploy(
kubeconfigId: 'kubeconfig',
configs: 'train-schedule-kube-canary.yml',
enableConfigSubstitution: true
)
kubernetesDeploy(
kubeconfigId: 'kubeconfig',
configs: 'train-schedule-kube.yml',
DevOps Project 2 Develop Jenkins Pipeline Project
enableConfigSubstitution: true
)
}
}
}
}

You need a Java JDK 7 or later to run the build. You can run the build like this:

./gradlew build
You can run the app with:

./gradlew npm_start

Thank You

You might also like