将 kaniko 构建映像从 jenkins groovy 管道推送到 azure 容器注册表时出错

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

我有一个场景,我在 Minikube 的 K8s 集群中运行 Jenkins。我在 Jenkins Pipeline 中运行一个 groovy 脚本来构建 docker 镜像 使用 Kaniko (无需 docker 守护进程即可构建 docker 映像)并推送到 Azure 容器注册表。我已经创建了用于向 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
          '''
      }
    }
  }
}

您能帮忙解决这个错误吗?还有:

  • 我如何知道 kaiko 构建的镜像的名称?
  • 我只是像 -registry.acr.io/test 一样推送:最新,可能是图像名称不正确,这就是我收到 JSON 输出错误的原因?
jenkins kubernetes azure-devops jenkins-groovy azure-aks
1个回答
0
投票
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}
          '''
        }
      }
    }
 
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.