Bootstrap

jenkins发布集成的镜像到私有harbor时,pipeline执行到docker镜像push环节,提示x509: certificate signed by unknown authority

问题:

jenkins发布集成的镜像到私有harbor时,pipeline执行到docker镜像push环节,提示Error response from daemon: Get “https://xxx.xx.xx.xxx/v2/”: x509: certificate signed by unknown authority。

查看pipeline内卡在执行命令为:
docker login ip

原因:

本地harbor默认是http协议,而docker login ip 命令走https://协议,需要将该ip加入jenkins临时容器的/etc/docker/daemon.json 的不安全表中。

方法:

进容器修改较麻烦,因此直接在pipeline语句中修改比较简单,因为pipeline语句中含有sh 脚本命令,和在容器中执行效果相同。新加第21-27行


node('testhan') {
    stage('Clone') {
        echo "1.Clone Stage"
        git url: "https://github.com/ashflasfh/jenkins-sample.git"
        script {
            build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
        }
    }
    stage('Test') {
      echo "2.Test Stage"

    }
    stage('Build') {
        echo "3.Build Docker Image Stage"
        sh "docker build -t 192.168.58.12/jenkins-demo/jenkins-demo:${build_tag} ."
    }
    stage('Push') {
        echo "4.Push Docker Image Stage"
        withCredentials([usernamePassword(credentialsId: 'dockerharbor', passwordVariable: 'dockerHubPassword', usernameVariable: 'dockerHubUser')]) {
            sh "cat >/etc/docker/daemon.json << EOF
{
 "registry-mirrors":["https://fsaf122.mirror.aliyuncs.com","https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn","https://dockerhub.azk8s.cn","http://hub-mirror.c.163.com","http://qewe42142.mirror.aliyuncs.com", "https://asfsf214.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "insecure-registries": ["https://192.168.40.181","harbor"]
}
EOF"
            sh "docker login 192.168.58.12 -u ${dockerHubUser} -p ${dockerHubPassword}"
            sh "docker push 192.168.58.12/jenkins-demo/jenkins-demo:${build_tag}"
        }
    }
    stage('Deploy to dev') {
        echo "5. Deploy DEV"
		sh "sed -i 's/<BUILD_TAG>/${build_tag}/' k8s-dev-harbor.yaml"
        sh "sed -i 's/<BRANCH_NAME>/${env.BRANCH_NAME}/' k8s-dev-harbor.yaml"
//        sh "bash running-devlopment.sh"
        sh "kubectl apply -f k8s-dev-harbor.yaml  --validate=false"
	}

如上: 在pipeline语句中间部分第21-27行加上一段sh语句,sh语句其中cat + EOF语句是输入内容到/etc/docker/daemon.json文件,
{}中前两行都是原来配置的,按自己系统情况修改即可;
{}中 “insecure-registries”: [“192.168.58.12”,“harbor”]属于新加内容,harbor服务器ip需要按自己实际harbor地址修改

          sh "cat >/etc/docker/daemon.json << EOF
{
 "registry-mirrors":["https://fsaf122.mirror.aliyuncs.com","https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn","https://dockerhub.azk8s.cn","http://hub-mirror.c.163.com","http://qewe42142.mirror.aliyuncs.com", "https://asfsf214.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "insecure-registries": ["192.168.58.12","harbor"]
}
EOF"

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;