jenkins pipelines - ghdrako/doc_snipets GitHub Wiki
For writing the Jenkins pipeline, we have to install two plugins in the Jenkins server:
Pipeline Concepts
- Pipeline: A pipeline is code that defines your entire build process, which includes different stages of building an application, testing, and delivering.
- Node: A node is a machine capable of executing a pipeline.
- Stage: There are different stages that software goes through as part of the build process, such as build, test, deploy, etc. A stage block in a pipeline defines different tasks to be performed as part of a particular stage. For example, the Build stage block includes tasks like compiling the source code, packaging a library, and so on.
- Step: A step is a single task that a pipeline performs as part of a particular stage. A stage block is nothing but a collection of multiple steps. For example, a step could execute a batch command, execute a particular Maven goal, and so on.
Pipeline Syntax Overview
Jenkins supports two types of pipelines
- declarative pipelines and
- scripted pipelines.
Scripted Pipeline
This is a traditional way of writing code for Jenkinsfile or Jenkins pipeline. It uses strictly groovy-based syntax. In this way of writing the pipeline, we have a lot of control over the pipeline script, and we can mutate it as per our use. This pipeline helps to develop complex features in pipeline code. Here’s an example:
node {
stage(“Hello World Stage”) {
echo “Hello World”
}
}
Declarative Pipelines
provides user-friendly syntax to develop Jenkins pipeline. Writing and reading pipeline code in this syntax is easier than in the scripted pipeline.
pipeline {
agent any
stages {
stage (“Hello World Stage”) {
steps {
echo “Hello World”
}
}
}
}
For writing pipelines, we can use the Pipeline Syntax Generator option provided by Jenkins. Once we click on this “Pipeline Syntax” button, it will redirect us to a page where we can generate the pipeline syntaxes.
Declarative pipelines contain a pipeline block that defines the entire build process.
pipeline
{
agent any 1
stages
{
stage('Build') 2
{
steps
{
// 3
}
}
stage('Test') 4
{
steps
{
// 5
}
}
stage('Deploy') 6
{
steps
{
// 7
}
}
}//Close of Stages
} //Close of Pipeline
1: This statement instructs Jenkins to execute this pipeline on any available agent, i.e. machine. 2: This block defines a build stage. 3: This defines a particular step (i.e., task) related to the build stage. 4: This defines the test stage. 5: This defines a particular step (i.e., task) related to the test stage. 6: This defines the deploy stage. 7: This defines a particular step (i.e., task) related to the deploy stage.
try
{
stage('Pulling Test automation code')
{
gitcredentialsId: 'MyGithubCredentials', url: '[email protected]:dpranoday/TestWebApplicationWithSeleniumPythonTests.git'
}
stage('Running tests')
{
if(params.TestType.equals("AllTests"))
{
bat "pytest --html ${params.TestType}Result.html"
}
else
{
bat "pytest -m ${params.TestType} --html ${params.TestType}Result.html"
}
}
}
finally
{
emailextattachmentsPattern: '**/'+params.TestType+'Result.html', body: 'Please find E-E Selenium scripts report file:'+params.TestType+'Result.html attached with this email', subject: 'Calculator Web Application details:$DEFAULT_SUBJECT', to: '[email protected]'
}
All the stages are enclosed between try
blocks so that even if a stage fails and you get an exception, the script code should send you an email notification.