Jenkinsfile管道:到达主机边车的ip

问题描述 投票:1回答:1

我正在使用Kubernetes插件在GKE上运行Jenkins。我添加了一个postgres容器作为带有jnlp代理容器的边车。它可以在代理商的localhost:5432上找到。我还在代理中安装了GKE节点的Docker套接字和bin,因此我可以启动“姐妹”容器。这也很好。

现在我想做以下事情:

pipeline {

    stages {

        stage('pytest') {

            agent {
                docker {
                    image "<image created in a previous stage>"
                    args '--add-host=database:\$(hostname\\ -i)'
                }
            }

            steps {
                // use postgres in sidecar of jnlp agent
                // e.g. on `database:5432`
            }
        }
    }
}

手动执行此操作很有效,但在Jenkins中,上述操作失败了:

Error: invalid argument "database:$(hostname -i)" for --add-host=database:$(hostname -i): invalid IP address in add-host: "$(hostname -i)"

有没有人知道如何逃避上述?或者也许是一种完全不同的方式来解决这个问题?

我没有在GKE上使用Docker 18.03+(卡在17.03.2-ce)所以我不能做host.docker.internal

docker run --network host也没有做任何事情。

docker jenkins kubernetes jenkins-pipeline
1个回答
0
投票

如果它是一辆边车你不需要$(hostname)你应该能够与localhost:5432连接。 pod中的容器共享相同的地址空间。

另一种选择是在pod规范中使用initContainers来设置所需的文件。你可以写一个像这样的bash脚本:

#!/bin/bash
cat <<EOF
pipeline {

    stages {

        stage('pytest') {

            agent {
                docker {
                    image "<image created in a previous stage>"
                    args "--add-host=database:${HOSTMAME}"
                }
            }

            steps {
                // use postgres in sidecar of jnlp agent
                // e.g. on `database:5432`
            }
        }
    }
}
EOF > /your-config-file

${HOSTNAME}是pod中的环境变量。

© www.soinside.com 2019 - 2024. All rights reserved.