1: Introduction
Details can be found in the w3c tutorial https://www.w3cschool.cn/jenkins/jenkins-jg9528pb.html
- 1:jenkins pipeline is divided into declarative and script
- 2: The advantage of declarative is simplicity, let's not say much else. See below
2: Overview of declarative Pipeline
pipeline { //stages are written inside the breakwater, and declarative forms consist of each distinct stage }
3:agent (this instruction is required)
-
The agent section specifies where the entire Pipeline or a specific phase will be executed in the Jenkins environment, depending on where the agent section is placed.This part must be defined at the top level within the pipeline block, but stage-level use is optional.Simply put, the main function of the agent part is to tell Jenkins to select the node machine to execute the Pipeline code.This instruction is required
-
Example:
pipeline { agent any //The parameter any indicates on which node the task is assigned by jenkins }
agent parameters
- Any parameter any indicates on which node the task is assigned by jenkins
pipeline { agent any //The parameter any indicates on which node the task is assigned by jenkins }
- none When applied at the top level of a pipeline block, global agents will not be assigned to the entire Pipeline run, and each stage section will need to contain its own agent section
pipeline { agent none stages { stage('getCode'){ agent { label 'master' //Specify that the current stage runs on the master node } steps{ echo "I am on master" } } stage('Scaner'){ agent { label 'node1' //Specify that the current stage runs on the node1 node } steps{ echo "I am on node1" } } } }
Note: The current stage can only be specified to run on a specific node if the agent is none
- label executes Pipeline or stage execution on a proxy machine available in the Jenkins environment using the provided Tags
pipeline { agent { label 'Node Label or Node Name' } stages { stage('structure'){ setps{ } } }
- Node functions like label above, but node runs other options, such as customWorkspace
pipeline { agent { node { label 'Node Label or Node Name' customWorkspace '${env.JOB_NAME}/${env.BUILD_NUMBER}' //Specify the workspace where jenkins will run } } }
4:post-post-built operation (not required)
- The post section defines the actions that will run at the end of a Pipeline run or phase.Some conditional blocks support post: partial always, changed, failure, success, unstable, and aborted.These blocks allow steps to be executed at the end of a Pipeline run or phase, depending on the state of the Pipeline
1:always executes this code regardless of the completion state of the Pipeline run
2:changed can trigger a run only if the current Pipeline is running in a different state than the previously completed Pipeline.
3:Failure executed after build failure
4:successExecute after successful build
Execute when unstable is unstable
6:aborted executes on termination/interruption of build
pipeline { agent none stages { stage('getCode'){ agent { label 'master' //Specify that the current stage runs on the master node } steps{ echo "I am on master" } } } post{ always{ echo "I am on always" } changed{ script{ echo "I am on changed" } } failure{ script{ echo "I am on failure" } } success{ script{ echo "I am on success" } } unstable{ script{ echo "I am on unstable" } } aborted{ script{ echo "I am on aborted" } } } }
Basic use of 5:stages, stages, and steps
1. stages: (necessary) The sum of all stages, all build actions must be performed under stages
2. stage: (necessary) a single stage
3. steps: (unnecessary) Execution steps under a single stage
pipeline { agent any stages { stage('Build') { steps { println "Build" } } stage('Test') { steps { println "Test" } } stage('Deploy') { steps { println "Deploy" } } } }
6:environment
-
The environment directive specifies a series of key-value pairs that are defined as environment variables for all steps or stage-specific steps
You can write down the top-level environment variables so that all stages share these variables, or you can define them separately under a stage that can only be invoked by this stage and not shared by other stages. -
Differences between global and local environment parameters
pipeline { agent any environment{ global = 'case1' //A global variable that can be used by all stage s below } stages { stage('Build') { steps { echo "Hello, ${global}, nice to meet you." } } stage('Testcase') { environment{ global2 = 'case2'//global2 is a local variable that can only be used for the "Testcase" stage, and the globalparameter is a global variable that he can also use } steps { echo "Hello, ${global2}, nice to meet you." } } } }
7: options
-
The options directive allows you to configure Pipeline-specific options within the Pipeline itself.Pipeline offers many of these options, such as buildDiscarder, but they may also be provided by plug-ins, such as timestamps
-
for example
pipeline { agent any options { retry(3) } stages { stage('demo') { steps { echo "Failed Retry Three Times" sh '1/0' //Executing here will fail the build with retry(3)Parameter, so retry three times } } } }