Home > front end >  Pushing Docker image to Artifactory Docker registry fails
Pushing Docker image to Artifactory Docker registry fails

Time:09-13

I am trying to create a CI/CD pipeline in Jenkins. The pipeline utilizes a Jenkinsfile to pull sources from a private Bitbucket server, builds a Docker image und tries to push it to a private Artifactory which hosts a Docker registry.

I have successfully used this workflow with Amazon ECR as the last step, but I cannot get the push to the Artifactory repo to work.

I am using this Jenkinsfile:

pipeline {
    environment {
        jenkinsArtifactoryCredentialsID = '...'
    }
    agent any
    options {
        skipStagesAfterUnstable()
    }

    stages {
        stage('Clone repository') {
            steps {
                script {
                    checkout scm
                }
            }
        }

        stage('Build') {
            steps {
                script {
                    image = docker.build("test")
                }
            }
        }

        stage('Deploy to Artifactory') {
            steps {
                script {
                    docker.withRegistry(
                        'https://artifactory.my.company.com:8443/artifactory/docker_registry_repo',
                        jenkinsArtifactoryCredentialsID) {
                            image.push('latest')
                            image.push("${env.BUILD_NUMBER}")
                    }
                }
            }
        }
    }
}

As you can see from the file, I have created a repo in Artifactory that is supposedly - as I understand it - a fully fledged docker registry, e.g. I can push different images with different tags into it. I have installed the Artifactory plugin in Jenkins and configured credentials whose ID I use here as an enviroment variable.

When it comes to the build step, the build gets stuck:

...
Login Succeeded
  docker tag test artifactory.my.company.com:8443/test:latest
  docker push artifactory.my.company.com:8443/test:latest
The push refers to repository [artifactory.my.company.com:8443/test]
154c211638e0: Preparing
0997ee847b8f: Preparing
bca59febf004: Preparing
994393dc58e7: Preparing
0997ee847b8f: Retrying in 5 seconds
...
154c211638e0: Retrying in 1 second
unknown: Not Found
ERROR: script returned exit code 1
Finished: FAILURE

What irritates me is that during the tagging, the path to the repo (e.g. to docker_registry_repo) is omitted somehow and I suspect that this causes the build to fail. Any help is appreciated.

EDIT:

Using the Artifactory API inside then Jenkinsfile, the pushing is done via the command:

rtDockerPush(
    serverId: 'company-artifactory',
    image: 'artifactory.my.company.com:8443/test:latest',
    targetRepo: 'docker_registry_repo'
)

I also configured the Artifactory server in the system config of Jenkins, so that I don't have to put them into the file.

Following these changes, the push still fails, but takes significantly more time before it does. I am presented with the following output:

INFORMATION: Pushing image: artifactory.my.company.com:8443/test:latest
com.github.dockerjava.api.exception.DockerClientException: Could not push image: unknown: Not Found
    at com.github.dockerjava.core.command.PushImageResultCallback.throwFirstError(PushImageResultCallback.java:42)
    at com.github.dockerjava.api.async.ResultCallbackTemplate.awaitCompletion(ResultCallbackTemplate.java:93)
    at org.jfrog.build.extractor.docker.DockerJavaWrapper.pushImage(DockerJavaWrapper.java:42)
    at org.jfrog.build.extractor.docker.extractor.DockerPush.execute(DockerPush.java:84)
    at org.jfrog.build.extractor.packageManager.PackageManagerExtractor.executeAndSaveBuildInfo(PackageManagerExtractor.java:33)
    at org.jfrog.build.extractor.docker.extractor.DockerPush.main(DockerPush.java:69)
ERROR: Couldn't execute docker task. RuntimeException: docker build failed with exit code 1

java.lang.RuntimeException: docker build failed with exit code 1
    at org.jfrog.hudson.pipeline.common.Utils.launch(Utils.java:280)
Caused: java.lang.RuntimeException: docker build failed. Couldn't execute docker task. RuntimeException: docker build failed with exit code 1
    at org.jfrog.hudson.pipeline.common.Utils.launch(Utils.java:285)
    at org.jfrog.hudson.pipeline.common.executors.BuildInfoProcessRunner.execute(BuildInfoProcessRunner.java:59)
    at org.jfrog.hudson.pipeline.common.executors.DockerPushExecutor.execute(DockerPushExecutor.java:42)
    at org.jfrog.hudson.pipeline.declarative.steps.docker.DockerPushStep$Execution.runStep(DockerPushStep.java:98)
    at org.jfrog.hudson.pipeline.declarative.steps.docker.DockerPushStep$Execution.runStep(DockerPushStep.java:83)
    at org.jfrog.hudson.pipeline.ArtifactorySynchronousNonBlockingStepExecution.run(ArtifactorySynchronousNonBlockingStepExecution.java:55)
    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
Finished: FAILURE

I suspected that it might have to do with the image name, but changing the name inside the docker.build command to artifactory.my.company.com:8443/test:latest to match the image to push produced the same error.

CodePudding user response:

Since you have the Artifactory plugin installed, then you can do this with the Artifactory plugin global variable methods interfacing with that plugin instead of the Docker global variable methods. For declarative pipeline:

rtServer(
  id:            'myArtifactoryServer',
  url:           <artifactory docker registry url>,
  credentialsId: <artifactory docker registry credentaisl id>
)
rtDockerPush(
  serverId: 'Artifactory-1',
  image: <registry>/<repo>:<tag>,
  targetRepo: <target repo>,
  // Attach custom properties to the published artifacts:
  properties: 'project-name=docker1;status=stable',
  // Optional - Only if this build is associated with a project in Artifactory, set the project key as follows.
  project: <project key>,
)

For scripted pipeline (or within a script block within a declarative pipeline):

// if artifactory registry id is configured within jenkins
artServer = Artifactory.server(<registry id>)
// otherwise
artServer = Artifactory.newServer(url: <registry url>, credentialsId: <artifactory docker registry credentaisl id>)
artDocker = Artifactory.docker(server: artServer)
artDocker.push(<registry>/<repo>:<tag>, <target repo>)

Note the returned object from docker.build must be from an invocation with the full registry and repository name for the method argument:

image = docker.build(<registry>/<repository>)
  • Related