我正在尝试做以下事情
- 查看代码
- 使用其他 docker 镜像进行一些预检查(不想在 Jenkins 节点上安装这些镜像)
- 使用docker镜像构建jar
maven:3.6-jdk-8
- 然后运行
Dockerfile
构建应用程序映像 - 将镜像推送到存储库
现在,除了在 Jenkins 节点上安装 Docker 之外,我不想安装任何东西。我想在 Docker 容器中运行完整的管道来实现这一点。我所苦恼的是如何从容器内部构建第 4 步。
我写了如下的 Jenkinsfile
pipeline {
agent none
stages {
stage('Maven build') {
agent {
docker {
image 'maven:3.6-jdk-8'
args '-u root:root'
}
}
steps {
checkout(
[
$class: 'GitSCM',
branches: [
[name: '*/master']
],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [
[
credentialsId: '<cred-id>',
url: '<github-url>']
]
])
sh '''
set -eux pipefail
mvn -e clean install
'''
}
}
stage('Build docker image') {
// Which docker image to use?
}
}
}
但我不确定如何在容器内构建docker镜像。搜索没有太大帮助。我尝试使用Jenkins节点构建docker镜像,但似乎无法混合搭配。我完全理解这是一个相当开放的问题,但我认为知道直接的答案会很有帮助。
答案1
我会尝试类似的事情:
pipeline {
/*
* Run everything on an existing agent configured with a label 'docker'.
* This agent will need docker, git and a jdk installed at a minimum.
*/
agent {
node {
label 'docker'
}
}
// using the Timestamper plugin we can add timestamps to the console log
options {
timestamps()
}
environment {
//Use Pipeline Utility Steps plugin to read information from pom.xml into env variables
IMAGE = readMavenPom().getArtifactId()
VERSION = readMavenPom().getVersion()
}
stages {
stage('Clone repository') {
/*
* Let's make sure we have the repository cloned to our workspace
*/
checkout(
[
$class: 'GitSCM',
branches: [
[name: '*/master']
],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [
[
credentialsId: '<cred-id>',
url: '<github-url>']
]
])
}
stage('Maven build') {
agent {
docker {
/*
* Reuse the workspace on the agent defined at top-level of
* Pipeline but run inside a container.
*/
image 'maven:3.6-jdk-8'
reuseNode true
}
}
steps {
sh '''
set -eux pipefail
mvn -e clean install
'''
}
post {
success {
/*
* Only worry about archiving the jar file
* if the build steps are successful (this part may be not necessary)
*/
archiveArtifacts(artifacts: '**/target/*.jar', allowEmptyArchive: true)
}
}
}
stage('Build docker image') {
steps {
sh '''
docker build -t ${IMAGE} .
docker tag ${IMAGE} ${IMAGE}:${VERSION}
docker push ${IMAGE}:${VERSION}
'''
}
}
}
}
reuseNode
即使签出和 Docker 构建在节点本身上运行,该选项也应该允许您在容器中运行 maven 构建。请参阅文档这里。