【问题标题】:Jenkinsfile dockerJenkinsfile 泊坞窗
【发布时间】:2017-10-12 23:21:13
【问题描述】:

我在 docker 容器内的 GCE 上运行 jenkins 实例,并希望从此 Jenkinsfile 和 Github 执行多分支管道。我正在为此使用GCE jenkins 教程。这是我的Jenkinsfile

node {
  def project = 'xxxxxx'
  def appName = 'gceme'
  def feSvcName = "${appName}-frontend"
  def imageTag = "eu.gcr.io/${project}/${appName}:${env.BRANCH_NAME}.${env.BUILD_NUMBER}"

  checkout scm

  sh("echo Build image")
  stage 'Build image'
  sh("docker build -t ${imageTag} .")

  sh("echo Run Go tests")
  stage 'Run Go tests'
  sh("docker run ${imageTag} go test")

  sh("echo Push image to registry")
  stage 'Push image to registry'
  sh("gcloud docker push ${imageTag}")

  sh("echo Deploy Application")
  stage "Deploy Application"
  switch (env.BRANCH_NAME) {
    // Roll out to canary environment
    case "canary":
        // Change deployed image in canary to the one we just built
        sh("sed -i.bak 's#eu.gcr.io/cloud-solutions-images/gceme:1.0.0#${imageTag}#' ./k8s/canary/*.yaml")
        sh("kubectl --namespace=production apply -f k8s/services/")
        sh("kubectl --namespace=production apply -f k8s/canary/")
        sh("echo http://`kubectl --namespace=production get service/${feSvcName} --output=json | jq -r '.status.loadBalancer.ingress[0].ip'` > ${feSvcName}")
        break

    // Roll out to production
    case "master":
        // Change deployed image in canary to the one we just built
        sh("sed -i.bak 's#eu.gcr.io/cloud-solutions-images/gceme:1.0.0#${imageTag}#' ./k8s/production/*.yaml")
        sh("kubectl --namespace=production apply -f k8s/services/")
        sh("kubectl --namespace=production apply -f k8s/production/")
        sh("echo http://`kubectl --namespace=production get service/${feSvcName} --output=json | jq -r '.status.loadBalancer.ingress[0].ip'` > ${feSvcName}")
        break

    // Roll out a dev environment
    default:
        // Create namespace if it doesn't exist
        sh("kubectl get ns ${env.BRANCH_NAME} || kubectl create ns ${env.BRANCH_NAME}")
        // Don't use public load balancing for development branches
        sh("sed -i.bak 's#LoadBalancer#ClusterIP#' ./k8s/services/frontend.yaml")
        sh("sed -i.bak 's#eu.gcr.io/cloud-solutions-images/gceme:1.0.0#${imageTag}#' ./k8s/dev/*.yaml")
        sh("kubectl --namespace=${env.BRANCH_NAME} apply -f k8s/services/")
        sh("kubectl --namespace=${env.BRANCH_NAME} apply -f k8s/dev/")
        echo 'To access your environment run `kubectl proxy`'
        echo "Then access your service via http://localhost:8001/api/v1/proxy/namespaces/${env.BRANCH_NAME}/services/${feSvcName}:80/"
  }
}

我总是收到错误docker not found:

[apiservice_master-GJCRJX6ZJPDVVSEUHIS6VBX7OYMFS5WKRVRKCSF4PSO76ZGZPKFQ] Running shell script
+ docker build -t eu.gcr.io/xxxxx/apiservice:master.1 .
/var/jenkins_home/workspace/apiservice_master-GJCRJX6ZJPDVVSEUHIS6VBX7OYMFS5WKRVRKCSF4PSO76ZGZPKFQ@tmp/durable-b4503ecc/script.sh: 2: /var/jenkins_home/workspace/apiservice_master-GJCRJX6ZJPDVVSEUHIS6VBX7OYMFS5WKRVRKCSF4PSO76ZGZPKFQ@tmp/durable-b4503ecc/script.sh: docker: not found

要让 docker 在 jenkins 中工作,我需要进行哪些更改?

【问题讨论】:

  • 您在 GCP 上看到 Container builder 了吗?使用非常简单,只需gcloud container builds submit . -t ${imageTag}而不是docker build,也会自动推送。但是,这并不能解决您的测试(也许是一些小型测试 GKE env?)。

标签: docker jenkins google-cloud-platform kubernetes jenkins-pipeline


【解决方案1】:

这看起来像 DiD(Docker 中的 Docker),recent issue 指出这是有问题的。
见“Using Docker-in-Docker for your CI or testing environment? Think twice.

同样的问题建议在特权模式下运行。
并确保您正在执行的 docker 容器确实安装了 docker。

【讨论】:

【解决方案2】:

您需要在用于该节点的 Jenkins 代理映像中安装 docker 客户端,即。 cloudbees/java-with-docker-client 以及安装在代理中的 docker 套接字

【讨论】:

    猜你喜欢
    • 2021-11-05
    • 2018-12-20
    • 2019-06-28
    • 2016-10-20
    • 1970-01-01
    • 2021-09-08
    • 2015-10-08
    • 2017-02-02
    • 2019-01-15
    相关资源
    最近更新 更多