Jenkins Docker 带容器的 Sidecar 运行 守护进程命令

Jenkins Docker Sidecar with Container Running a daemon command

我想 运行 ZAP 作为我管道中的代理,运行 我的 selenium 通过代理进行测试。我只是在容器中使用 curl 代替 selenium 进行测试,并且能够使用 docker.

在本地完成这项工作

在我的管道中,zap 启动了,但此后管道仅位于 zap 容器中,从未进入第二个容器。我明白为什么,我作为守护进程启动了一个进程,它永远不会完成,所以这个步骤永远不会完成。我只是不明白如何在 jenkins 中完成我需要的东西。

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' 中使用的命令启动 zap,并且仅在第二个容器阶段完成时才终止容器。

你可以直接在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-container 的 CMD。检查这个 API-documentation.