kubernetes 将Kaniko构建映像从Jenkins Groovy管道推送到Azure容器注册表时出错

pkwftd7m  于 12个月前  发布在  Kubernetes
关注(0)|答案(1)|浏览(129)

我有一个场景,我在Minikube的K8s集群中运行我的Jenkins。我在Jenkins Pipeline中运行一个groovy脚本,它使用Kaniko构建Docker镜像(它构建一个没有Docker守护进程的Docker镜像)并推送到Azure容器注册表。我已经创建了Secret来向Azure进行身份验证。
但是当我输入一个图像时-我得到一个错误

" [36mINFO[0m[0004] Taking snapshot of files...                  
[36mINFO[0m[0004] ENTRYPOINT ["jenkins-slave"]                 
error pushing image: failed to push to destination Testimage.azurecr.io/test:latest: unexpected end of JSON input
[Pipeline] }"

字符串
我的剧本

My groovy script -- 

def label = "kaniko-${UUID.randomUUID().toString()}"

podTemplate(name: 'kaniko', label: label, yaml: """
kind: Pod
metadata:
  name: kaniko
spec:
  containers:
  - name: kaniko
    image: gcr.io/kaniko-project/executor:debug
    imagePullPolicy: Always
    command:
    - /busybox/cat
    tty: true
    volumeMounts:
      - name: jenkins-pv
        mountPath: /root
  volumes:
  - name: jenkins-pv
    projected:
      sources:
      - secret:
          name: pass
          items:
            - key: .dockerconfigjson
              path: .docker/config.json
"""
  ) {

  node(label) {
    stage('Build with Kaniko') {
      git 'https://github.com/jenkinsci/docker-jnlp-slave.git'
      container(name: 'kaniko', shell: '/busybox/sh') {
          sh '''#!/busybox/sh
          /kaniko/executor -f `pwd`/Dockerfile -c `pwd` --skip-tls-verify --destination=testimage.azurecr.io/test:latest
          '''
      }
    }
  }
}


您能帮助我们克服这个错误吗?还有:

  • 我怎么知道惠子建立的形象的名字?
  • 我只是推一样-registry.acr.io/test:最新的,可能是一个不正确的图像名称,这是我得到JSON输出错误的原因?
q35jwt9p

q35jwt9p1#

Here is whole procedure.

1. Create ACR Secret

ACR_NAME=youruniquename.azurecr.io
 
# Aenter code heressumes ACR Admin Account is enabled
 
ACR_UNAME=$(az acr credential show -n $ACR_NAME --query="username" -o tsv)
ACR_PASSWD=$(az acr credential show -n $ACR_NAME --query="passwords[0].value" -o tsv)
 
kubectl create secret docker-registry acr-secret \
  --docker-server=$ACR_NAME \
  --docker-username=$ACR_UNAME \
  --docker-password=$ACR_PASSWD \
  [email protected]
 
  2. Below is jenkins pipeline file for Kaniko
 
podTemplate(yaml: '''
    apiVersion: v1
    kind: Pod
    spec:
      containers:
      - name: maven
        image: maven:3.8.1-jdk-8
        command:
        - sleep
        args:
        - 99d
      - name: kaniko
        image: gcr.io/kaniko-project/executor:debug
        imagePullPolicy: Always
        command:
        - sleep
        args: 
        - 9999999
        volumeMounts:
        - name: kaniko-secret
          mountPath: /kaniko/.docker
      restartPolicy: Never
      volumes:
      - name: kaniko-secret
        secret: 
          secretName: acr-secret
          items:
            - key: .dockerconfigjson
              path: config.json
''') {
    environment {
        APP_NAME = "kaniko-build"
        RELEASE = "1.0.0"
        DOCKER_USER = "acruser"
        DOCKER_PASS = "acr-password"
        IMAGE_NAME = "${DOCKER_USER}" + "/" + "${APP_NAME}"
        IMAGE_TAG = "${RELEASE}-${BUILD_NUMBER}"
    }
  node(POD_LABEL) {
    stage('Get a Maven project') {
      git 'https://github.com/cyrille-leclerc/multi-module-maven-project'
      container('maven') {
        stage('Build a Maven project') {
          sh '''
          mvn clean verify
          '''
        }
      }
    }
 
    stage('Build Java Image') {
      git url: 'https://github.com/scriptcamp/kubernetes-kaniko.git', branch: 'main'    
      container('kaniko') {
        stage('Build an image') {
          sh '''
            /kaniko/executor --dockerfile `pwd`/Dockerfile --context `pwd` --destination=acrrepo.azurecr.io/kaniko-demo:${BUILD_NUMBER}
          '''
        }
      }
    }
 
  }
}

字符串

相关问题