Jenkins管道:docker.withRegistry(...)失败

5tmbdcev  于 2023-08-03  发布在  Jenkins
关注(0)|答案(1)|浏览(97)

bounty还有6天到期。回答此问题可获得+50声望奖励。Aniket Singla正在寻找一个规范答案

OS: MacOS Ventura 13.0.1
Jenkins LTS 2.361.3 installed from homebrew, running in my local MacOS host.
Docker 20.10.20 from Docker Desktop

字符串

我正在努力用上面解释的环境构建我的Jenkins CI管道。这是我的Jenkinsfile,描述了声明式管道。
Jenkinsfile:

pipeline {
    agent any

    // polling by schedular
    // triggers {
    //     pollSCM('*/3 * * * *')
    // }

    environment {
        registry = 'my-docker-repository-name'
        registryCredential = 'my-jenkins-credential-for-dockerhub'
        dockerImage = ''
    }

    stages {
        stage('Echo') {
            agent any
            steps {
                echo 'from github repository'
                echo 'build number is ' + "${env.BUILD_NUMBER}"
            }
        }
        // stage for cloning your github repository
        stage('Prepare') {
            agent any
            steps {
                echo 'Cloning Repository'
                git url: 'my-github-repository-url',
                branch: 'master',
                credentialsId: 'jenkins-credential-for-github' 
            }
            post {
                failure {
                    error 'This pipeline stops here...'
                }
            }
        }

        // gradle build
        stage('Bulid Gradle') {
            agent any
            steps {
                echo 'Bulid Gradle'
                dir ('.'){
                  sh './gradlew clean build'
                }
            }
            post {
                failure {
                    error 'This pipeline stops here...'
                }
            }
        }

        // docker build
        stage('Bulid Docker') {
            agent any
            steps {
                echo 'Bulid Docker'
                script {
                    dockerImage = docker.build registry
                }
            }
            post {
                failure {
                  error 'This pipeline stops here...'
                }
            }
        }

        // docker push
        stage('Push Docker') {
            agent any
            steps {
                echo 'Push Docker'
                script {
                    docker.withRegistry('https://registry.hub.docker.com', registryCredential) {
                        dockerImage.push("${env.BUILD_NUMBER}") 
                        dockerImage.push('latest')
                    }
                }
            }
            post {
                failure {
                    error 'This pipeline stops here...'
                }
            }
        }
    }
}


此管道在“Push Docker”阶段失败,消息如下:

Using the existing docker config file.Removing blacklisted property: authsRemoving blacklisted property: credsStore$ docker login -u me -p me https://registry.hub.docker.com
Current context "desktop-linux" is not found on the file system, please check your config file at /Users/me/.jenkins/workspace/ci-pipeline@2@tmp/6e9fd8bf-aa3e-4654-b7b6-54b6138478df/config.json


当我在Push Docker阶段使用docker.withRegistry('')尝试此脚本时(即没有url和dockerhub凭据),我可以成功推送图像。
你能告诉我我错过了什么吗?

  • 谢谢-谢谢
    =其他配置文件如下。
    Dockerfile:
FROM openjdk:11-jdk
LABEL maintainer="email"
ARG JAR_FILE=build/libs/me-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} me.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/docker-springboot.jar"]


.docker/config.json

{
        "auths": {
                "https://index.docker.io/v1/": {},
                "registry.hub.docker.com": {}
        },
        "credsStore": "desktop",
        "currentContext": "desktop-linux"
}


/opt/homebrew/Cellar/jenkins-lts/2.361.3

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
        <key>Label</key>
                <string>Aqua</string>
                <string>Background</string>
                <string>LoginWindow</string>
                <string>StandardIO</string>
                <string>System</string>
        </array>
        <key>ProgramArguments</key>
        <array>
                <string>/opt/homebrew/opt/openjdk@17/bin/java</string>
                <string>-Dmail.smtp.starttls.enable=true</string>
                <string>-jar</string>
                <string>/opt/homebrew/opt/jenkins-lts/libexec/jenkins.war</string>
                <string>--httpListenAddress=127.0.0.1</string>
                <string>--httpPort=8888</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>EnvironmentVariables</key>
        <dict>
                <key>PATH</key>
                <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Docker.app/Contents/Resources/bin/:/Users/me/Library/Group\ Containers/group.com.docker</string>
        </dict>
</dict>
</plist>

yb3bgrhw

yb3bgrhw1#

我最近也遇到了同样的问题。Jenkins从.docker/config.json复制配置
如果将currentContext更新为默认值,那么它似乎可以正常工作。

{
    "auths": {
            "https://index.docker.io/v1/": {},
            "registry.hub.docker.com": {}
    },
    "credsStore": "desktop",
    "currentContext": "default"
}

字符串
应该能解决你的问题。它似乎解决了我的问题。对于我的情况,问题在最近的docker更新后开始发生。

相关问题