Jenkins pipline语法简介

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lb245557472/article/details/85550544

所有有效的声明pipline都必须被pipline括起来

    pipline{
    	/* ur code*/
    }
  • a.开头必须是块,例如:pipline{}
  • b.不必用分号分开,但是必须在同一行
  • c.块里面只能包括 Sections,Directives, Steps, 赋值语句
  • d.一些属性引用语句当做无参函数调用,例如input使用时为input()

Sections

1.agent(必须,可在pipline或stage)
agent必须定义在pipline块的最顶层(全局agent),在stage中时,可以随意的位置(局部agent)
参数
any可以在任意的agent上执行,agent any
none在最顶层不设置全局的agent,但是需要在stage设置各自运行的agent,agent none
pipeline {
    agent none 在这里插入代码片
    stages {
        stage('Example Build') {
            agent { docker 'maven:3-alpine' } 
            steps {
                echo 'Hello, Maven'
                sh 'mvn --version'
            }
        }
        stage('Example Test') {
            agent { docker 'openjdk:8-jre' } 
            steps {
                echo 'Hello, JDK'
                sh 'java -version'
    			}
    		}
    	}
    }
label标识运行那个agent,agent {label "master"}
node和label差不多但是node可以有额外的操作,agent {node {label "master"}}
docker运行docker文件后续研究下docker在补充
2.post(可选,可在pipline或stage)
post定义一些额外的步骤,在Pipeline’s 或者 stage’s 运行完之后运行post支持以下这些条件块
always, changed, fixed, regression, aborted, failure, success, unstable, cleanup
always 不管构建的状态,每次都会运行
changed 每当上一次构建的状态和当前不一致时运行
fixed 当上一次的构建失败或者不稳定,当前的成功时运行
aborted 构建被手动终止时运行
regression 当上一次的构建失败、手动终止或者不稳定,当前的构建成功时运行
failure 当前构建失败时运行,如果手动添加失败则不会运行( currentBuild.result = 'FAILURE')
unstable 当前构建处于不稳定状态时运行
cleanup 在其他的post condition(always,changed,etc)运行完之后运行,不管构建的状态

pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
    post { 
        always { 
            echo 'I will always say Hello again!'
        }
        success {
            echo 'test success condition'
        }
        changed {
            echo 'test changed condition'
        }
        aborted{
            echo 'test aborted condition'
			}
		cleanup{
            echo "test cleanup condition"
			}
		}
	}
3.stages(必须,只在pipline中,一次)
大模块的区分,例如build、test、deploy
至少包含一个stage
pipeline {
	agent any
	stages { 
		stage('Example') {
			steps {
				echo 'Hello World'
			}
		}
	}
}
4.steps(必须,在stage中)
详细的执行步骤
pipeline {
    agent any
    stages {
        stage('Example') {
            steps { 
                echo 'Hello World'
				}
			}
		}
	}

Directives

1.environment(可选,可在pipline或stage)
定义一些适用于所有步骤的键值对类似于全局变量、局部变量
pipeline {
    agent any
    environment { 
        CC = 'clang'
    }
    stages {
        stage('Example') {
            environment { 
                AN_ACCESS_KEY = credentials('my-prefined-secret-text') 
            }
            steps {
                sh 'printenv'
				}
			}
		}
	}  
2.options(可选,可在pipline或stage中,在stage中相对少一点配置选项)
配置pipline自己的配置
pipeline {
    agent any
    options {
        timeout(time: 1, unit: 'HOURS') 
    }
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
				}
			}
		}
	}
pipeline {
    agent any
    stages {
        stage('Example') {
            options {
                timeout(time: 1, unit: 'HOURS') 
            }
            steps {
                echo 'Hello World'
				}
			}
		}
	}
3.parameters(可选,在pipline中)
自定义一系列的参数,可以通过params对象来获取
pipeline {
    agent any
    parameters {
        string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')

        text(name: 'BIOGRAPHY', defaultValue: '', description: 'Enter some information about the person')

        booleanParam(name: 'TOGGLE', defaultValue: true, description: 'Toggle this value')

        choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')

        password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'Enter a password')

        file(name: "FILE", description: "Choose a file to upload")
    }
    stages {
        stage('Example') {
            steps {
                echo "Hello ${params.PERSON}"

                echo "Biography: ${params.BIOGRAPHY}"

                echo "Toggle: ${params.TOGGLE}"

                echo "Choice: ${params.CHOICE}"

                echo "Password: ${params.PASSWORD}"
				}
			}
		}
	}
4.triggers
5.stage
6.tools
运行之前需要在Manage Jenkins → Global Tool Configuration设置下
pipeline {
    agent any
    tools {
        ant 'antTest' 
    }
    stages {
        stage('Example') {
            steps {
                bat 'ant -version'
				}
			}
		}
	}
7.input
出现交互的页面
pipeline {
    agent any
    stages {
        stage('Example') {
            input {
                message "Should we continue?"
                ok "Yes, we should."
                submitter "alice,bob"
                parameters {
                    string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
                }
            }
         steps {
                echo "Hello, ${PERSON}, nice to meet you."
            }
        }
    }
}

样例代码

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                echo 'Building..'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing..'
            }
        }
        stage('Deploy') {
            when {
              expression {
                currentBuild.result == null || currentBuild.result == 'SUCCESS' 
              }
            }
            steps {
                echo currentBuild.result
                echo "try"
            }
        }
    }
}
pipeline {
    agent any 
    environment {
        // Using returnStdout
        CC = """${bat(
                returnStdout: true,
                script: 'echo "clang"'
            )}""" 
        // Using returnStatus
        EXIT_STATUS = """${bat(
                returnStatus: true,
                script: 'exit 1'
            )}"""
        TEST = "hello world!"
    }
    stages {
        stage('Example') {
            environment {
                DEBUG_FLAGS = '-g'
            }
            steps {
                // bat 'set'
                echo CC
                echo TEST
                echo "${TEST}"
                echo "${env.BUILD_ID}"
                echo "Running ${env.BUILD_ID} on ${env.JENKINS_URL}"
            }
        }
    }
}
pipeline {
    agent any
    parameters {
        string(name: 'Greeting', defaultValue: 'Hello', description: 'How should I greet the world?')
    }
    stages {
        stage('Example') {
            steps {
                echo "${params.Greeting} World!"
            }
        }
    }
}
stage('Test') {
    parallel linux: {
        //need the existed agent 
        node('master') {
            // checkout scm
            try {
                echo "hello linux"
            }
            finally {
                echo "good by, linux"
            }
        }
    },
    windows: {
        node('windows') {
            try {
                echo "hello windows"
            }
            finally {
                echo "good by, linux"
            }        }
    }
}

docker启动Jenkins命令

docker run -u root --rm -d -p 8080:8080 -p 50000:50000 
-v jenkins-data:/var/jenkins_home 
-v  /var/run/docker.sock:/var/run/docker.sock 
jenkinsci/blueocean	

猜你喜欢

转载自blog.csdn.net/lb245557472/article/details/85550544
今日推荐