Jenkins2.0 Pipeline import
Pipeline as Code is the essence of Jenkins 2.0, which is the key tool to help Jenkins realize the magnificent transformation from CI to CD.
The so-called Pipeline, simply speaking, is a set of workflow framework running on Jenkins, which connects tasks that originally run independently on a single or multiple nodes to achieve a complex release process that is difficult to be completed by a single task
Pipeline is implemented as a Groovy DSL(similar to Gradle). Any publishing process can be expressed as a Groovy script, and Jenkins supports reading scripts directly from the Code base, thus implementing the concept of Pipeline as Code.
Note: This article was published on My public account CodeSheep. You can subscribe by holding down or scanning the heart below ↓ ↓ ↓
Why Jenkins2.0 Pipeline
This is mainly based on my own pain points used in traditional Jenkins Job:
-
Traditional Jenkins jobs are difficult to parallelize flexibly and efficiently (parallelism among jobs, nodes, tasks, and even four dimensions within tasks)
-
The trend of the traditional Jenkins Job getting out of control caught us off guard. There were too many jobs, CI scripts were too discrete, maintenance costs were too high, and it was very dangerous. When a single Jenkins Server failed, everything was Game Over
-
The branch code CI deployment of the new pull branch is too cumbersome
-
The traditional Jenkins Job display is not very intuitive
I think these reasons should be enough to turn our attention to Jenkins2.0 Pipeline!
Features and advantages of Pipeline:
- Persistence: Pipeline’s job still works after Jenkins’ master is restarted as planned and unplanned, unaffected. Jenkins’ master and agent are connected by SSH. If you know nohup or disown, you can understand why the restart of the master does not affect the job running on agent.
- Pausability: Groovy-based pipelines can suspend a job and wait for user input or approval before continuing execution. 3. More flexible parallel execution and stronger dependency control. Groovy scripts can realize parallel execution between steps and stages and more complex interdependence.
- Extensibility: Extensions made easier by Groovy programming.
- Design Pipeline = Design code, very elegant
- As Code: Centrally manage CI scripts, manage scripts with the Code base, read scripts directly from the Code base, and pull up project CI quickly!
Pipeline principle and process
Pipeline designs three basic concepts for users:
- Stages: A Pipeline can be divided into stages, each representing a set of operations. Note that Stage is a logical grouping concept that can span multiple nodes.
- Node: A Node is a Jenkins Node, either Master or Agent, which is the specific runtime environment for executing Step.
- Step: Step is the most basic operation unit, ranging from creating a directory to building a Docker image, which is provided by various Jenkins plugins.
A typical Stage View is shown below:
From the diagram, you can easily see which stages passed, which failed, and when they were built.
Groovy script is used to build Jenkins2.0 Pipeline. The steps of Groovy script are as follows:
- Go to Jenkins main interface to set up Pipeline task
In fact, the more common Pipeline is MultiBranch, which is not included in the screenshot above, but is basically similar to a normal Pipeline.
- Customize workflow using Groovy scripts
The example script above is as follows:
node {
stage('Checkout Code'{/ /for display purposes
// Get some code from a GitHub repository
git 'https://github.com/jglick/simple-maven-project-with-tests.git'
}
stage('Build') {
// Run the maven build
if (isUnix()) {
sh "'${MAVEN_HOME}/bin/mvn' -Dmaven.test.failure.ignore clean package"
} else {
bat(/"${MAVEN_HOME}\bin\mvn" -Dmaven.test.failure.ignore clean package/)
}
}
stage('Unit test') {
junit '**/target/surefire-reports/TEST-UT.xml'
archive 'target/*.jar'}}Copy the code
- Start Pipeline execution
The stage View of the construction process is as follows:
It is obvious that the code shown here is consistent with the formatted code in the Groovy script, showing the progress and results of each workflow in real time, which is straightforward to understand. Move the mouse pointer to view the thumbnail of the log information. Click to move it to the console corresponding to the stage.
All in all, everything is so elegant!
Jenkins2.0 Pipeline key DSL syntax and examples
Here’s a summary of the key DSL syntax in Pipeline, which can be combined with Groovy to complete any complex CI/CD process, and it’s helpful to be familiar with them.
- archiveArtifacts
Archiving documents, for example:
archiveArtifacts 'target/*.jar'
Copy the code
- bat
Execute batch files under Windows platform, such as
bat "call example.bat"
Copy the code
- build
Trigger to build a Jenkins job, as in
build 'TEST_JOB'
Copy the code
- checkout
Checkout repo from an SCM system, such as:
checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: ' ', excludedRegions: ' ', excludedRevprop: ' ', excludedUsers: ' ', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: ' ', locations: [[credentialsId: '30e6c1e5-1035-4bdd-8a44-05ba8f885158', depthOption: 'infinity', ignoreExternalsOption: true.local: '. ', remote: 'svn://xxxxxx']], workspaceUpdater: [$class: 'UpdateUpdater']])
Copy the code
- deleteDir()
Delete the current directory from the workspace
- dir
Switch the directory, for example
dir('/home/jenkins') {// Switch to the /home/Jenkins directory to do something // some block}Copy the code
- echo
Print information, such as echo ‘Hello world’
- emailtext
Jenkins can be used to send emails, content and subject can be customized, such as
emailext body: 'Subject_test', subject: 'Subject_test', to: '[email protected]'// The body, subject, and recipient to of an email can be customizedCopy the code
- error
Throw an error signal, which you can throw in your own code, such as error’ read_error’
- fileExists
Check whether a file exists in a path of the workspace, for example:
fileExists '/home/test.txt'// Check whether test.txt existsCopy the code
- input
Waiting for interactive input from external users, for example:
input message: ' ', parameters: [string(defaultValue: 'Default value', description: 'Version number', name: 'version')] // At a certain step, wait for the user to enter the version parameter before proceedingCopy the code
- isUnix
The command is used to check whether the current task is running on a UNIX-like node. For example:
def flag = isUnix()
if( flag == false) {// You can judge from thisecho "not run on a unix node !"
}
Copy the code
- load
Call an external Groovy script, for example:
load 'D:\\jenkins\\workspace\\test.groovy'
Copy the code
- node
Assign a node to a task. For example:
node('Node label') {// Run a Task on the node of the corresponding label Task()}Copy the code
- parallel
Parallel execution of tasks is arguably the most practical and efficient tool, for example:
Parallel (// Run android Unit Tests and Android E2E tests in parallel'android unit tests': {
runCmdOnDockerImage(androidImageName, 'bash /app/ContainerShip/scripts/run-android-docker-unit-tests.sh'.'--privileged --rm')},'android e2e tests': {
runCmdOnDockerImage(androidImageName, 'bash /app/ContainerShip/scripts/run-ci-e2e-tests.sh --android --js'.'--rm')})Copy the code
- Properties:
To set the attributes of the Job, run the following command:
properties([parameters([string(defaultValue: '1.0.0', description: 'Version number', name: 'VERSION')]), pipelineTriggers([])]) // Set a VERSION parameter for the jobCopy the code
-
PWD Displays the current directory
-
readFile
To read a file from a workspace, for example:
def editionName = readFile '/home/Test/exam.txt'
Copy the code
- retry
Repeat the body code N times, for example:
retry(10) {
// some block
}
Copy the code
- sh
Run the shell script, for example: sh” sh test.sh”
- sleep
Sleep time: 2, unit: ‘HOURS’
- stage
Create a stage for a task, for example:
stage('stage name') {
// some block
}
Copy the code
- stash
Save files for subsequent construction. For example:
dir('target') {
stash name: 'war', includes: 'x.war'
}
Copy the code
- unstash
Recreate the files stored in the Stash step in the current workspace, for example:
def deploy(id) {
unstash 'war'
sh "cp x.war /tmp/${id}.war"
}
Copy the code
- timeout
Time limits, for example
timeout(time: 4, unit: 'SECONDS') {
// some block
}
Copy the code
- timestamps
Used to timestamp the console, for example:
timestamps {
// some block
}
Copy the code
- touch
Create a file. For example:
touch file: 'TEST.txt', timestamp: 0
Copy the code
- unzip
Decompress the file, for example:
unzip dir: '/home/workspace', glob: ' ', zipFile: 'TEST.zip'
Copy the code
- validateDeclarativePipeline
Check whether the given file contains a valid Declarative Pipeline that returns either T or F
validateDeclarativePipeline '/home/wospace'
Copy the code
- waitUntil
Wait until the condition is met
waitUntil {
// some block
}
Copy the code
- withCredentials
Using the credentials
withCredentials([usernameColonPassword(credentialsId: 'mylogin', variable: 'USERPASS')]) {
sh ' '' set +x curl -u $USERPASS https://private.server/ > output '' '
}
Copy the code
- withEnv
Set environment variables, pay attention to the recent run effective!
withEnv(['MYTOOL_HOME=/usr/local/mytool']) {
sh '$MYTOOL_HOME/bin/start'
}
Copy the code
- writeFile
Write a file to a path
writeFile file: '/home/workspace', text: 'hello world'
Copy the code
- writeJSON
Write JSON file, the usage is basically the same as above
- zip
Creating a ZIP file
zip dir: '/home/workspace', glob: ' ', zipFile: 'TEST.zip'
Copy the code
- ws
A custom workspace in which to do some work is similar to the Dir command. For example:
ws('/home/jenkins_workspace') {
// some block
}
Copy the code
Afterword.
-
The author’s more original articles are here, welcome to watch
-
My Personal Blog
The author has more SpringBt practice articles here:
- Spring Boot application monitoring actual combat
- The SpringBoot application is deployed in an external Tomcat container
- ElasticSearch in SpringBt practice
- A preliminary study on Kotlin+SpringBoot joint programming
- Spring Boot Logging framework practices
- SpringBoot elegant coding: Lombok plus
If you are interested, take some time to read some of the author’s articles on containerization and microservitization:
- Use K8S technology stack to create personal private cloud serial articles
- Nginx server configuration from a detailed configuration list
- Docker container visual monitoring center was built
- Use ELK to build Docker containerized application log center
- RPC framework practice: Apache Thrift
- RPC framework practice: Google gRPC
- Establishment of microservice call chain tracking center
- Docker containers communicate across hosts
- Preliminary study on Docker Swarm cluster
- Several guidelines for writing dockerFiles efficiently