jenkins Pipline入门

Pipeline

docker运行

pipeline {
    agent {docker 'python'}
    stages {
        stage('build') {
            step {
                sh 'python --version'
            }
        }
    }
}

Deploy阶段重试3次flask-deploy.sh直到成功,执行health-check.sh超时执行3分钟

pipeline {
    agent any
    stages {
        stage('Deploy') {
            steps {
                retry(3) {
                    sh './flask-deploy.sh'
                }
                timeout(time: 3,unit: 'MINUTES') {
                    sh './health-check.sh'
                }
            }
        }
    }
}

Deploy阶段重试执行5次flask-deploy.sh直到成功,如果这个时间过长,3分钟超时

pipeline {
    agent any
    stages {
        stage('Deploy') {
            steps {
                timeout(time: 3,unit: 'MINUTES') {
                    retry(5) {
                        sh './flask-deploy.sh'
                    }
                }
            }
        }
    }
}

当管道执行完成,可能需要根据管道的一些结果来进行处理后续清理工作,这里在post中完成。

pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                sh 'echo "Fail";exit 1'
            }
        }
    }
    post {
        always {
            echo "This will always run"
        }
        success {
            echo "This will run only if successful"
        }
        failure {
            echo "This will run only if failed"
        }
        unstable {
            echo "This will run only if run was marked as unstable"
        }
        changed {
            echo 'This will run only if the state of pipeline has changed'
        }
    }
}

使用环境变量

pipeline {
    agent any
     environment {
         DISABLE_AUTH='true'
         DB_ENGINE='mysql'
     }
     stages {
         stage('Build') {
             steps {
                 sh 'printenv'
             }
         }
     }

清理和通知

pipeline {
    agent any
    stages {
        stage('no-op') {
            steps {
                sh 'ls'
            }
        }
    }
    post {
        always {
            echo 'one way or another,i have finished'
            deleteDir() /* clean up our workspace */
        }
        failure {
            mail to: 'test@gmail.com',
                 subject: 'Failed Pipeline: ${currentBuild.fullDisplayName}',
                 body: 'Something is wrong with ${env.BUID_URL}'
        }
    }
}

部署

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying'
            }
        }
    }
}

交互部署

pipeline {
    agent any
    stages {
        stage('Deplay - dev') {
            steps {
                sh 'echo "deploy dev"'
            }
        }
        stage('Sanity check') {
            steps {
                input "R U OK?"
            }
        }
        stage('Deploy - production') {
            steps {
                sh 'echo "deploy production"'
            }
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容