0%

Jenkins

Jenkins流水线的使用

Jenkins

流水线

创建流水线

1
2
3
4
5
6
7
8
9
10
pipeline {
agent { docker 'maven:3.3.3' }
stages {
stage('build') {
steps {
sh 'mvn --version'
}
}
}
}

重复、超时、部署后处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
pipeline {
agent any

// 环境变量
environment {
DISABLE_AUTH = 'true'
DB_ENGINE = 'sqlite'
}

stages {
stage('Deploy') {
steps {
// 重复3次
retry(3) {
sh './flakey-deploy.sh'
}
// 3分钟超时后,即部署失败
timeout(time: 3, unit: 'MINUTES') {
sh './health-check.sh'
}
}
}
stage('Sanity check') {
steps {
input "Does the staging environment look ok?" // 人工确认
}
}
}

// pipeline后处理
post {
always {
//通过 archiveArtifacts 步骤和文件匹配表达式可以很容易的完成构建结果记录和存储
archiveArtifacts artifacts: 'build/libs/**/*.jar', fingerprint: true
junit 'build/reports/**/*.xml' //记录测试
}
success {
echo 'This will run only if successful'
}
failure {
mail to: 'team@example.com',
subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
body: "Something is wrong with ${env.BUILD_URL}"
}
unstable {
echo 'This will run only if the run was marked as unstable'
}
changed {
echo 'This will run only if the state of the Pipeline has changed'
echo 'For example, if the Pipeline was previously failing but is now successful'
}
}
}

构建JAVA程序

1
docker run --rm -u root -p 8080:8080 -v /Users/xuexuan/jenkins-data:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -v "$HOME":/home jenkinsci/blueocean

https://www.jenkins.io/zh/doc/tutorials/build-a-java-app-with-maven/