![Pipeline Jenkins: Construa o docker dentro do contêiner do docker](https://rvso.com/image/756767/Pipeline%20Jenkins%3A%20Construa%20o%20docker%20dentro%20do%20cont%C3%AAiner%20do%20docker.png)
Estou tentando fazer o seguinte
- Confira o código
- Faça algumas pré-verificações usando outras imagens do Docker (não quero instalá-las no nó Jenkins)
- Construir jar usando imagem docker
maven:3.6-jdk-8
- Em seguida, execute
Dockerfile
para criar a imagem do aplicativo - Envie a imagem para o repositório
Agora, não quero instalar nada além do Docker no nó Jenkins. Quero executar o pipeline completo no contêiner Docker para conseguir isso. O que estou lutando é como construir a quarta etapa de dentro do contêiner.
Eu escrevi o Jenkinsfile como abaixo
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?
}
}
}
Mas não tenho certeza de como construir uma imagem docker dentro do contêiner. A pesquisa não ajudou muito. Tentei usar o nó Jenkins para a construção da imagem do docker, mas parece que não consigo misturar e combinar. Entendo perfeitamente que esta é uma questão bastante aberta, mas acho que seria útil saber a(s) resposta(s) direta(s).
Responder1
Eu tentaria algo como:
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}
'''
}
}
}
}
A reuseNode
opção deve permitir que você execute o maven build no contêiner, mesmo que o checkout e o Docker build sejam executados no próprio nó. Veja a documentaçãoaqui.