我使用的是RedHat-7系统。我想要 Jenkins Pipeline 来实施 DevOps。
但是当我使用
docker buildx build
功能时,Jenkins 说“未知标志:--platform”。
我使用 docker 镜像运行 Jenkins:
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 pipeline 不能使用“--platform”选项?如何解决这个问题?