Kir*_*ill 1 jenkins jenkins-docker jenkins-pipeline
假设我有一个包含多个步骤的 dockerized 管道。docker 容器定义在开头Jenkinsfile:
pipeline {
agent {
docker {
image 'gradle:latest'
}
}
stages {
// multiple steps, all executed in 'gradle' container
}
post {
always {
sh 'git whatever-command' // will not work in 'gradle' container
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想git在构建后操作中执行一些命令。问题是gradle图像没有git可执行文件。
script.sh:第 1 行:git:找不到命令
如何在 Docker 主机上执行它仍然使用gradle容器进行所有其他构建步骤?当然,我不想为每个步骤明确指定容器,而是要为特定的 post-post 操作指定容器。
好的,下面是我的工作解决方案,在单个 dockerized 阶段(Dockerized gradle)中对多个阶段(构建和测试)进行分组,并在 docker 主机和 docker 容器之间重用单个工作区(请参阅reuseNode文档):
pipeline {
agent {
// the code will be checked out on out of available docker hosts
label 'docker'
}
stages {
stage('Dockerized gradle') {
agent {
docker {
reuseNode true // < -- the most important part
image 'gradle:6.5.1-jdk11'
}
}
stages{
// Stages in this block will be executed inside of a gradle container
stage('Build') {
steps{
script {
sh "gradle build -x test"
}
}
}
stage('Test') {
steps{
script {
sh "gradle test"
}
}
}
}
}
stage('Cucumber Report') {
// this stage will be executed on docker host labeled 'docker'
steps {
cucumber 'build/cucumber.json'
}
}
}
post {
always {
sh 'git whatever-command' // this will also work outside of 'gradle' container and reuse original workspace
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
705 次 |
| 最近记录: |