# Jenkins的Pipeline流水线作业

之前我们都是通过界面的层面下进行配置发布与构建的过程。

为什么要使用Pipeline流水线作业?

答:为了管理构建流程的被一个步骤清晰可见。而不是手动的去配置每一个步骤的执行命令。

有了Pipeline流水线作业我们可以单独的管理每一个步骤执行的情况。

# Jenkins新建任务

image-20250330153020771

image-20250330153211162

image-20250330153311750

# 回顾之前构建的过程总结

  • 第一步:通过git的客户端插件先从gitlab服务器上拉取指定分支的代码。
  • 第二步:然后生成对应的jar包。
  • 第三步:进行文件传输。传送jar包、dockerfile文件。再进行构建docker的镜像。并将镜像推送到harbor的仓库中去。
  • 第四步:通知目标服务器创建相应的docker容器进行运行。

image-20250330154324049

# 第一步)拉取gitLab代码

image-20250330154456982

image-20250330154633955

image-20250330154704539

checkout scmGit(branches: [[name: '$tag']], extensions: [], userRemoteConfigs: [[url: 'http://192.168.5.240/root/myproject_wutunan_ci']])
1

第一步完成的脚本:

pipeline {
    agent any

    stages {
        stage('Pull SourceCode') {
            steps {
                checkout scmGit(branches: [[name: '$tag']], extensions: [], userRemoteConfigs: [[url: 'http://192.168.5.240/root/myproject_wutunan_ci']])
            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11

# 第二步)代码的编译

在我们进行拉取代码以后。接着就要去执行构建jar包的命令。

image-20250330162134354

/usr/local/maven/bin/mvn package
1

image-20250330162237995

生成的脚本。

sh '/usr/local/maven/bin/mvn package'
1
pipeline {
    agent any

    stages {
        stage('Pull SourceCode') {
            steps {
                checkout scmGit(branches: [[name: '$tag']], extensions: [], userRemoteConfigs: [[url: 'http://192.168.5.240/root/myproject_wutunan_ci']])
            }
        }
    stage('Maven Build') {
            steps {
                sh '/usr/local/maven/bin/mvn package'
            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

image-20250330162526872

验证是否构建jar包成功。我们要去jenkins服务器上。进入/var/jenkins/workspace目录下。

[root@cicd workspace]# pwd
/var/jenkins/workspace
1
2

image-20250330163501196

# 第三步)构建与发布镜像

image-20250330163746749

这一步我们要将文件发布到远程的服务器上进行构建镜像。

image-20250330164252368

注意

这里还要在添加一个传输项。目的就是为了传输dockerfile的构建文件。

image-20250330164335952

最终:

pipeline {
    agent any

    stages {
        stage('Pull SourceCode') {
            steps {
                checkout scmGit(branches: [[name: '$tag']], extensions: [], userRemoteConfigs: [[url: 'http://192.168.5.240/root/myproject_wutunan_ci']])
            }
        }
    stage('Maven Build') {
            steps {
                sh '/usr/local/maven/bin/mvn package'
            }
        }
    stage('Publing Harbor Image') {
            steps {
                sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.5.203', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/*.jar'), sshTransfer(cleanRemote: false, excludes: '', execCommand: '''docker build -t 192.168.5.203:80/public/myproject:$tag /usr/local/
docker login -u admin -p Harbor12345 192.168.5.203:80
docker push 192.168.5.203:80/public/myproject:$tag''', execTimeout: 3000000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'docker', sourceFiles: 'docker/*', usePty: true)], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

image-20250330164839109

# 第四步)目标服务器运行容器

image-20250330165408595

image-20250330165455201

生成最终的流水线脚本:

pipeline {
    agent any

    stages {
        stage('Pull SourceCode') {
            steps {
                checkout scmGit(branches: [[name: '$tag']], extensions: [], userRemoteConfigs: [[url: 'http://192.168.5.240/root/myproject_wutunan_ci']])
            }
        }
    stage('Maven Build') {
            steps {
                sh '/usr/local/maven/bin/mvn package'
            }
        }
    stage('Publing Harbor Image') {
            steps {
                sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.5.203', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/*.jar'), sshTransfer(cleanRemote: false, excludes: '', execCommand: '''docker build -t 192.168.5.203:80/public/myproject:$tag /usr/local/
docker login -u admin -p Harbor12345 192.168.5.203:80
docker push 192.168.5.203:80/public/myproject:$tag''', execTimeout: 3000000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'docker', sourceFiles: 'docker/*', usePty: true)], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }
        }
    stage('Run Container') {
            steps {
                sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.5.11', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''docker rm -f myproject
docker run -d -p 80:80 --name=myproject 192.168.5.203:80/public/myproject:$tag''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '', usePty: true)], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }
        }
    }
}

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

验证:是否能成功构建镜像、发布镜像、在目标服务器上创建镜像对应的容器。

image-20250330165810228

image-20250330170116311

harbor仓库的验证:

image-20250330170308248

目标服务器的验证:

image-20250330170144109

浏览器验证服务是否正常运行:

image-20250330170406095

# 问题1)Pipeline没有阶段视图

问题描述:在触发构建后没有显示构建的阶段视图。

image-20250330161517894

解决:安装插件。

Pipeline Stage View
1

image-20250330161503583

点击安装Pipeline Stage View。

image-20250330161629251

插件安装完成的效果:

image-20250330161732186

这里我们点击 “返回首页”。结果会看到阶段构建视图。

image-20250330161854265

Last Updated: 4/4/2025, 12:26:44 PM