我正在嘗試執行以下操作
- 檢查程式碼
- 使用其他一些 docker 映像進行一些預檢查(不想在 Jenkins 節點上安裝這些映像)
- 使用 docker 映像建置 jar
maven:3.6-jdk-8
- 然後運行
Dockerfile
建置應用程式映像 - 將鏡像推送到倉庫
現在,我不想在 Jenkins 節點上安裝 Docker 以外的任何東西。我想在 Docker 容器中運行完整的管道來實現這一點。我正在努力解決的是如何從容器內建立第四步。
我寫的 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
選項應該允許您在容器中運行 Maven 構建,即使結帳和 Docker 構建是在節點本身上運行的。查看文件這裡。