在多个阶段之间重用Jenkins中的代理(docker容器)

nwwlzxa7  于 2023-04-29  发布在  Jenkins
关注(0)|答案(4)|浏览(171)

我有一个包含多个阶段的流水线,我想只在“n”个阶段之间重用docker容器,而不是所有阶段:

pipeline {
   agent none

   stages {
       stage('Install deps') {
            agent {
                docker { image 'node:10-alpine' }
            }

            steps {
                sh 'npm install'
            }
        }

       stage('Build, test, lint, etc') {
            agent {
                docker { image 'node:10-alpine' }
            }

            parallel {
                stage('Build') {
                    agent {
                        docker { image 'node:10-alpine' }
                    }

                    // This fails because it runs in a new container, and the node_modules created during the first installation are gone at this point
                    // How do I reuse the same container created in the install dep step?
                    steps {
                        sh 'npm run build'
                    }
                }

                stage('Test') {
                    agent {
                        docker { image 'node:10-alpine' }
                    }

                    steps {
                        sh 'npm run test'
                    }
                }
            }
        }

        // Later on, there is a deployment stage which MUST deploy using a specific node,
        // which is why "agent: none" is used in the first place

   }
}
qpgpyjmq

qpgpyjmq1#

参见Jenkins Pipeline docker agent的reuseNode选项:
https://jenkins.io/doc/book/pipeline/syntax/#agent

pipeline {
  agent any

  stages {
    stage('NPM install') {
      agent {
        docker {
          /*
           * Reuse the workspace on the agent defined at top-level of
           * Pipeline, but run inside a container.
           */
          reuseNode true
          image 'node:12.16.1'
        }
      }

      environment {
        /*
         * Change HOME, because default is usually root dir, and
         * Jenkins user may not have write permissions in that dir.
         */
        HOME = "${WORKSPACE}"
      }

      steps {
        sh 'env | sort'
        sh 'npm install'
      }
    }
  } 
}
rt4zxlrg

rt4zxlrg2#

您可以使用脚本管道,在其中您可以将多个stage步骤放入docker步骤中,例如。g的。

node {
  checkout scm
  docker.image('node:10-alpine').inside {
    stage('Build') {
       sh 'npm run build'
     }
     stage('Test') {
       sh 'npm run test'
     }
  }
}

(code未经测试)

neekobn8

neekobn83#

对于Declarative pipeline,一种解决方案可以是在项目的根目录中使用Dockerfile。例如:

Dockerfile

FROM node:10-alpine
// Further Instructions

Jenkinsfile

pipeline{

    agent {
        dockerfile true
    }
    stage('Build') {
        steps{
            sh 'npm run build'
        }
    }
     stage('Test') {
        steps{
            sh 'npm run test'
        }
    }
}
yjghlzjz

yjghlzjz4#

对于声明性管道,您可以使用这里描述的嵌套阶段
在上面的链接中有一个更好的例子,我只是不想逐字复制粘贴。

pipeline {
    agent none

    stages {
        stage("Reuse Docker Image Across Sub-stages") {
            agent { docker "image1" }
            stages {
               stage("sub stage 1") {
                   steps { sh "./one.sh" }
               }
               stage("sub stage 2") {
                   steps { sh "./two.sh" }
               }
            }
        }

        stage("Use new docker image for something else") 
            agent { docker "image2" }
            steps { sh "./three.sh" }
        }
    }
}

相关问题