Jusene's Blog

Jenkins Pipline入门

字数统计: 564阅读时长: 3 min
2018/01/28 Share

first Pipeline

docker运行

1
pipeline {
2
    agent {docker 'python'}
3
    stages {
4
        stage('build') {
5
            step {
6
                sh 'python --version'
7
            }
8
        }
9
    }
10
}

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

1
pipeline {
2
    agent any
3
    stages {
4
        stage('Deploy') {
5
            steps {
6
                retry(3) {
7
                    sh './flask-deploy.sh'
8
                }
9
                timeout(time: 3,unit: 'MINUTES') {
10
                    sh './health-check.sh'
11
                }
12
            }
13
        }
14
    }
15
}

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

1
pipeline {
2
    agent any
3
    stages {
4
        stage('Deploy') {
5
            steps {
6
                timeout(time: 3,unit: 'MINUTES') {
7
                    retry(5) {
8
                        sh './flask-deploy.sh'
9
                    }
10
                }
11
            }
12
        }
13
    }
14
}

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

1
pipeline {
2
    agent any
3
    stages {
4
        stage('Test') {
5
            steps {
6
                sh 'echo "Fail";exit 1'
7
            }
8
        }
9
    }
10
    post {
11
        always {
12
            echo "This will always run"
13
        }
14
        success {
15
            echo "This will run only if successful"
16
        }
17
        failure {
18
            echo "This will run only if failed"
19
        }
20
        unstable {
21
            echo "This will run only if run was marked as unstable"
22
        }
23
        changed {
24
            echo 'This will run only if the state of pipeline has changed'
25
        }
26
    }
27
}

使用环境变量

1
pipeline {
2
    agent any
3
     environment {
4
         DISABLE_AUTH='true'
5
         DB_ENGINE='mysql'
6
     }
7
     stages {
8
         stage('Build') {
9
             steps {
10
                 sh 'printenv'
11
             }
12
         }
13
     }
14
}

清理和通知

1
pipeline {
2
    agent any
3
    stages {
4
        stage('no-op') {
5
            steps {
6
                sh 'ls'
7
            }
8
        }
9
    }
10
    post {
11
        always {
12
            echo 'one way or another,i have finished'
13
            deleteDir() /* clean up our workspace */
14
        }
15
        failure {
16
            mail to: 'jusene@1234.com',
17
                 subject: 'Failed Pipeline: ${currentBuild.fullDisplayName}',
18
                 body: 'Something is wrong with ${env.BUID_URL}'
19
        }
20
    }
21
}

部署

1
pipeline {
2
    agent any
3
    stages {
4
        stage('Build') {
5
            steps {
6
                echo 'Building'
7
            }
8
        }
9
        stage('Test') {
10
            steps {
11
                echo 'Testing'
12
            }
13
        }
14
        stage('Deploy') {
15
            steps {
16
                echo 'Deploying'
17
            }
18
        }
19
    }
20
}

交互部署

1
pipeline {
2
    agent any
3
    stages {
4
        stage('Deplay - dev') {
5
            steps {
6
                sh 'echo "deploy dev"'
7
            }
8
        }
9
10
        stage('Sanity check') {
11
            steps {
12
                input "R U OK?"
13
            }
14
        }
15
16
        stage('Deploy - production') {
17
            steps {
18
                sh 'echo "deploy production"'
19
            }
20
        }
21
    }
22
}
CATALOG
  1. 1. first Pipeline
  2. 2. 使用环境变量
  3. 3. 清理和通知
  4. 4. 部署