“未知标志:--在Jenkins管道中使用“Docker buildx build”时的平台

kjthegm6  于 2022-11-02  发布在  Jenkins
关注(0)|答案(1)|浏览(325)

我正在使用RedHat-7系统。我想用Jenkins管道来实现Devops。
但是当我使用docker buildx build特性时,Jenkins说“未知标志:--平台”。
我运行我的Jenkins与Docker图像:

docker run -d \
    --name jenkins \
    --restart=unless-stopped \
    -u 0 \
    --network jenkins \
    -p 8082:8080 \
    -p 50000:50000 \
    -v /home/ngtl/jenkins-data:/var/jenkins_home \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v $(which docker):/usr/bin/docker \
    -e TZ=Asia/Shanghai \
    -e JAVA_OPTS=-Duser.timezone=Asia/Shanghai \
    jenkins/jenkins:lts-jdk11

这是我的管道:

pipeline {
  agent any

  tools {
      maven 'mvn'
  }

  environment {
    DOCKER_CREDENTIALS = credentials('clouds3n-ldap')
  }

  stages {
    stage('Unit Test') {
      steps {
        withMaven(maven: 'mvn') {
          sh 'mvn clean test -Dmaven.test.failure.ignore=false'
        }
      }
    }

    stage('Maven Build') {
      steps {
        withMaven(maven: 'mvn') {
          sh 'mvn package -Dmaven.test.skip -DskipTests'
        }
      }
    }

    stage('Sonar Scan') {
      steps {
        withSonarQubeEnv('sonarqube') {
          withMaven(maven: 'mvn') {
            script {
              def allJob = env.JOB_NAME.tokenize('/') as String[]
              def projectName = allJob[0]
              sh "mvn sonar:sonar -Dsonar.branch.name=${env.GIT_BRANCH} -Dsonar.projectKey=${projectName} -Dsonar.projectName=${projectName} -Dmaven.test.skip -DskipTests"
            }
          }
        }
      }
    }

    stage('Sonar Gate') {
      steps {
        timeout(time: 30, unit: 'MINUTES') {
          waitForQualityGate abortPipeline: true
        }
      }
    }

    stage('Docker Build') {
      steps {
        script {
          def allJob = env.JOB_NAME.tokenize('/') as String[]
          def projectName = allJob[0]
          final noSuffixProjectName = projectName.substring(0, projectName.lastIndexOf('-'))
          sh "echo ${DOCKER_CREDENTIALS_PSW} | docker login -u ${DOCKER_CREDENTIALS_USR} 192.168.2.157:8881 --password-stdin"
          sh "docker buildx build --platform linux/amd64 -t 192.168.2.157:8881/uni/${noSuffixProjectName}:dev-${BUILD_NUMBER} -f ${env.JENKINS_HOME}/k8s-config/docker/BackendDockerfile . --push"
        }
      }
    }

    stage('Maven Deploy') {
      steps {
        withMaven(maven: 'mvn') {
          sh 'mvn deploy -Dmaven.test.skip -DskipTests'
        }
      }
    }

    stage('K8s Apply') {
      steps {
        echo 'not support now, comming soon'
      }
    }
  }

    post {

      always {
        sh 'docker logout 192.168.2.157:8881'
      }

      cleanup {
        cleanWs()
      }

      success {
        echo 'Finished!'
      }
    }
}

当达到“Docker Build”阶段时,Jenkins将抛出错误:

Warning: A secret was passed to "sh" using Groovy String interpolation, which is insecure.
         Affected argument(s) used the following variable(s): [DOCKER_CREDENTIALS_PSW]
         See https://jenkins.io/redirect/groovy-string-interpolation for details.
+ echo****
+ docker login -u****192.168.2.157:8881 --password-stdin
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[Pipeline] sh
+ docker buildx build --platform linux/amd64 -t 192.168.2.157:8881/uni/cqu:dev-11 -f /var/jenkins_home/k8s-config/docker/BackendDockerfile . --push
unknown flag: --platform
See 'docker --help'.

为什么Jenkins管道不能使用“--platform”选项?如何解决这个问题?

zqdjd7g9

zqdjd7g91#

确保您的Jenkins代理(从代理)具有最新版本的docker. BuildKit has been integrated to docker build since Docker 18.06
在我的情况下,版本18.09.6没有工作。20.10是好的。

相关问题