代码之家  ›  专栏  ›  技术社区  ›  Avindra Goolcharan

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

  •  4
  • Avindra Goolcharan  · 技术社区  · 6 年前

    我有一个具有多个阶段的管道,我希望在“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
    
       }
    }
    
    2 回复  |  直到 6 年前
        1
  •  5
  •   StephenKing    6 年前

    你可以用 scripted pipelines ,可以放置多个 stage A内台阶 docker 步骤,例如

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

    (代码未测试)

        2
  •  1
  •   Ankit Pandoh    5 年前

    对于声明性管道,一种解决方案是在项目的根目录中使用dockerfile。例如:

    文档文件

    FROM node:10-alpine
    // Further Instructions
    

    詹金斯档案

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