Jenkinsfile码头工人 [英] Jenkinsfile docker

查看:524
本文介绍了Jenkinsfile码头工人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 节点{
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)
stageDeploy Application
switch(env.BRANCH_NAME){
//推广到金丝雀环境
案例金丝雀:
//将金丝雀中的部署图像更改为我们刚建立的图像
sh(sed -i.bak's#eu.gcr。 IO /云的解决方案,图像/ gceme:1.0.0#$ {} imageTag #'./k8s/canary/*.yaml)
sh(kubectl --namespace = production apply -f k8s / services /)
sh(kubectl --namespace = production apply - (echo http://`kubectl --namespace = production get service / $ {feSvcName} --output = json | fs8s / canary /)
sh jq -r'.status.loadBalancer.ingress [0] .ip'`> $ {feSvcName})
break

//转入制作
casemaster:
//将金丝雀中部署的图像更改为我们只需要的图像建立
sh(sed -i.bak'#eu.gcr.io / cloud-solutions-images / gceme:1.0.0#$ {imageTag}#'./k8s/production/*.yaml )
sh(kubectl --namespace = production apply -f k8s / production /)
sh(kubectl --namespace = production apply -f k8s / services /)
sh (echo http://`kubectl --namespace = production get service / $ {feSvcName} --output = json | jq -r'.status.loadBalancer.ingress [0] .ip'`> $ {feSvcName} )
break

//推出开发环境
默认值:
//创建名称空间(如果不存在)
sh(kubectl获取ns $ {env.BRANCH_NAME} || kubectl创建ns $ {env.BRANCH_NAME})
//不要对开发分支使用公共负载平衡
sh(sed -i.bak s#LoadBalancer#ClusterIP#'./k8s/services/frontend.y aml)
sh(sed -i.bak's#eu.gcr.io / cloud-solutions-images / gceme:1.0.0#$ {imageTag}#'./k8s/dev/*。 (kubectl --namespace = $ {env.BRANCH_NAME} apply -f k8s / services /)
sh(kubectl --namespace = $ {env.BRANCH_NAME} apply - f k8s / dev /)
echo'要访问您的环境,运行`kubectl proxy`'
echo'然后通过http:// localhost:8001 / api / v1 / proxy / namespaces / $ {env.BRANCH_NAME} / services / $ {feSvcName}:80 /
}
}

我总是得到一个错误 docker not found

  [apiservice_master-GJCRJX6ZJPDVVSEUHIS6VBX7OYMFS5WKRVRKCSF4PSO76ZGZPKFQ]运行shell脚本
+ 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在jenkins里面工作,我需要做些什么?

解决方案

这看起来像DiD(Docker中的Docker),它近期问题指出有问题。

请参阅在CI或测试环境中使用Docker-in-Docker?思考两次。



同样的问题建议以特权模式运行。

并确保您正在执行的Docker容器具有码头工talled。

I'm running a jenkins instance on GCE inside a docker container and would like to execute a multibranch pipeline from this Jenkinsfile and Github. I'm using the GCE jenkins tutorial for this. Here is my 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/"
  }
}

I always get an error 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

What do I have to change to make docker work inside jenkins?

解决方案

That looks like DiD (Docker in Docker), which this recent issue points out as problematic.
See "Using Docker-in-Docker for your CI or testing environment? Think twice."

That same issue recommends to run in privilege mode.
And make sure your docker container in which you are executing does have docker installed.

这篇关于Jenkinsfile码头工人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆