一千萬個為什麽

搜索

Jenkins管道+ Docker集成



我有一個CI服務器,它自己在主機上安裝了Jenkins,我想在Docker容器中運行構建和測試。直到這裏它的工作方式應該如此,但我也想使用docker-compose部署容器,這應該在主機中完成,但是當我在Jenkinsfile中運行命令時,它們會在用於構建和測試的Docker容器內運行。

pipeline {
agent {
    dockerfile {
        filename "Dockerfile"
    }
}

stages {
    stage("Run unit tests") {
        steps {
            # This is ran inside the Docker container, as expected
            sh "cp environments/.env.build .env"
            sh "vendor/bin/phpunit"
        }
    }

    stage('Deploy to test') {
        when {
            branch "develop"
        }

        steps {
            # This is still ran inside the Docker container, not the host
            sh 'docker-compose up -d'
        }
    }

    stage("Archive") {
        steps {
            # This is ran inside the Docker container, as expected
            sh "zip -r dist.zip ."
        }
    }
}

post {
    always {
        archiveArtifacts artifacts: "dist.zip"
        junit "reports/phpunit.report.xml"
    }
}
}

轉載註明原文: Jenkins管道+ Docker集成

一共有 1 個回答:

解決此問題的一種方法是使用多個管道。第一個管道在docker中運行測試。完成後,可以打電話給另一份工作

https://jenkins.io/doc/pipeline/steps/pipeline-build - 工序/

https://support.cloudbees.com/hc/en-us/articles/221400287-How-to-pass-parameter-to-downstream-job-in-Pipeline-job-

  node {
    paramAValue =“paramAValue”
    paramBValue =“paramBValue”
    build job:'downstream-freestyle',參數:[[$ class:'StringParameterValue',name:'ParamA',value:paramAValue],[$ class:
       'StringParameterValue',名稱:'ParamB',值:paramBValue]]
}
</代碼> 

在Jenkins本身上運行docker-compose。