带有容器的Jenkins Docker Sidecar运行守护程序

时间:2018-11-20 13:53:17

标签: docker jenkins zap

我想在管道中将ZAP作为代理运行,并通过代理运行硒测试。我只是在容器中使用curl代替硒来进行测试,因此能够使用docker在本地进行这项工作。

在我的管道中,zap启动,但是之后管道仅位于zap容器中,再也没有进入第二个容器。我知道为什么,我已经作为守护进程启动了一个进程,该进程永远不会完成,所以该步骤永远不会完成。我只是不明白如何完成詹金斯所需要的。

stage('Run Zap Proxy'){
        docker.image('owasp/zap2docker-weekly').withRun('-p 8090:8090') { c ->
            docker.image('owasp/zap2docker-weekly').inside("-v $WORKSPACE:/zap/wrk:rw") {
                /* Wait until mysql service is up */
                sh """
                   zap.sh -daemon -port 8090 -host 0.0.0.0 -newsession testing -config api.addrs.addr.name=.* -config api.addrs.addr.regex=true -config api.disablekey=true
               """
            }
            docker.image('cfmanteiga/alpine-bash-curl-jq').inside("--link ${c.id}:proxy") {
                sh 'curl -k -x http://proxy:8090 https://my.fqdn.net'
                sh """
                   curl -k -x http://proxy:8090 \
                       -X POST https://my.fqdn.net/api/rest/sessions \
                       -H 'Content-Type: application/json' \
                       -H 'Accept: application/json' \
                       -d '{"username":"username","password":"password"}'
               """
                sh 'sleep 2m'
                sh 'curl -o report.html http://zap/UI/core/other/htmlreport'
                stash includes: 'report.html', name: 'report'
            }
        }
}

我本质上需要在inside中使用im命令启动zap,并且仅在第二个容器阶段完成后才杀死该容器。

1 个答案:

答案 0 :(得分:1)

您可以在withRun部分中直接传递zap命令:

stage('Run Zap Proxy'){
    docker.image('owasp/zap2docker-weekly').withRun('-p 8090:8090 -v $WORKSPACE:/zap/wrk:rw', 'zap.sh -daemon -port 8090 -host 0.0.0.0 -newsession testing -config api.addrs.addr.name=.* -config api.addrs.addr.regex=true -config api.disablekey=true') { c ->
        docker.image('cfmanteiga/alpine-bash-curl-jq').inside("--link ${c.id}:proxy") {
            sh 'curl -k -x http://proxy:8090 https://my.fqdn.net'
            sh """
               curl -k -x http://proxy:8090 \
                   -X POST https://my.fqdn.net/api/rest/sessions \
                   -H 'Content-Type: application/json' \
                   -H 'Accept: application/json' \
                   -d '{"username":"username","password":"password"}'
           """
            sh 'sleep 2m'
            sh 'curl -o report.html http://zap/UI/core/other/htmlreport'
            stash includes: 'report.html', name: 'report'
        }
    }
}

withRun允许您覆盖zap容器的CMD。选中此API-documentation

相关问题