Jenkins pipeline fails to login to docker hub - docker

I am trying to use pipeline to run a few things and when i started running my pipeline it failed to login to docker.
the weird thing is that i am able to login on the machine itself but when i run the pipeline is fails with this weird error:
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] stage
[Pipeline] { (Front-end)
[Pipeline] node
Running on test-env in /var/www/test-env/workspace/client-e2e
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withDockerRegistry
Using the existing docker config file.Removing blacklisted property: auths$ docker login -u ***** -p ******** https://hub.docker.com/?namespace=******
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Error response from daemon: login attempt to https://hub.docker.com/v2/ failed with status: 404 Not Found
[Pipeline] // withDockerRegistry
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
ERROR: docker login failed
Finished: FAILURE
I don't know why it performs a login when this image is publicly available for everyone.
can someone help me ?
this is the pipeline itself:
pipeline {
agent none
stages {
stage('Front-end') {
agent {
docker {
image 'node:8-alpine'
label "test-env"
}
}
steps {
sh 'node --version'
}
}
}
}

You can try with Credentials Binding Plugin in:
steps {
sh 'node --version'
}
You can do:
withCredentials([string(credentialsId: 'mytoken', variable: 'TOKEN')]) {
sh '''
docker login -u '<your_user>' -p '<$TOKEN>'
node --version
'''
}
There is an example here:
https://issues.jenkins-ci.org/browse/JENKINS-41051

Ok, so after a while i found out that it was as simple as doing this
pipeline {
agent none
stages {
stage('Front-end') {
agent {
docker {
image 'node:8-alpine'
registryUrl 'https://index.docker.io/v1/'
label "test-env"
}
}
steps {
sh 'node --version'
}
}
}
}
this was aded: registryUrl 'https://index.docker.io/v1/'

Related

Jenkins throws error when running pipeline

I got a little problem with Jenkins Pipeline... To be more specific it seems like it always fails, but without any reasons, (probably there is one, but cannot find it)
Snippet of the pipeline.... (Whole Pipeline)
pipeline {
environment {
DOCKERHUB_CREDENTIALS=credentials("Dockerhub")
APPLICATION_HOST="0.0.0.0:8000"
APPLICATION_PORT="8000"
}
stage("build"){
steps{
script {
def inspectDockerNetwork = sh script: "docker network create global_store_network", returnStatus: true
if (inspectDockerNetwork == 0) {
sh "echo 'Creating Docker Network...'"
sh "docker network create global_store_network"
sh "echo 'Network Has been Created..'"
}
}
dir("test_env"){
sh "docker-compose up -d"
sleep 10
sh "echo 'Docker Built Image Successfully! Running Container....'"
}
}
}
stage("test"){
steps{
load "./test_env/version_env.groovy"
sh "echo 'Running Test Pipeline'"
sh "echo 'Running Healtcheck Test...'"
sh "echo 'Sleeping until the Application will be fully ready...'"
sleep 10
script {
command = """curl -s -X GET -H 'accept: */*' 'http://${env.APPLICATION_HOST}:${env.APPLICATION_PORT}/healthcheck/'"""
responseStatus = sh(script: command, returnStdout: true).trim()
if (responseStatus != "200") {
sh "echo 'Application Responded with Failure, Not Ready for Production...'"
error "Health Check Stage Failure."
}
}
}
post {
always {
dir("test_env"){
sh "echo 'Removing Testing Environment'"
sh "docker-compose down"
}
}
}
}. /////////// It has not start an Execution of the Deployment, So the Error Is Somewhere above ////////////////////////////////////
stage("deployment"){
steps {
load "./test_env/version_env.groovy"
sh "echo 'Running Deployment Pipeline Stage...'"
sh "echo 'Tagging new Image Version'"
withCredentials([usernamePassword(
credentialsId: "DockerHub", // Credential Id that should be created at Jenkins Server...
usernameVariable: env.DOCKERHUB_CREDENTIALS_USR, // Credential Username that should be created at jenkins Server.
passwordVariable: env.DOCKERHUB_CREDENTIALS_PSW, // Credential Password that shoud be created at Jenkins Server..
)]){
sh "docker login -u ${env.DOCKERHUB_CREDENTIALS_USR} -p ${env.DOCKERHUB_CREDENTIALS_PSW}"
sh "echo 'Logged In.. Into Docker.'"
sh "echo 'Tagging An Image'"
sh "docker tag new_versioned_image ${env.DOCKERHUB_REPOSITORY_LINK}:latest"
sh "echo 'Tagged... Pushing onto docker repo.'"
sh "docker push ${env.DOCKERHUB_REPOSITORY_LINK}:latest"
sh "echo 'Tagged Successfully.. Pushing Image On Docker Hub..'"
sh "echo 'Image has been Pushed Successfully! Pipeline Finished.'"
}
}
}
}
So the Output of that snippet is following...
( I've Separated Stage Logs In order to make it easier to read )
/////////// Build Stage ///////////////
First time build. Skipping changelog.
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withCredentials
Masking supported pattern matches of $DOCKERHUB_CREDENTIALS or $DOCKERHUB_CREDENTIALS_PSW
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (build)
[Pipeline] dir
Running in /var/jenkins_home/workspace/Store Pipeline/test_env
[Pipeline] {
[Pipeline] sh
+ docker-compose up -d
Container test_postgres_store_database Creating
Container test_postgres_store_database Created
Container test_store_application_server Creating
Container test_store_application_server Created
Container test_postgres_store_database Starting
Container test_postgres_store_database Started
Container test_store_application_server Starting
Container test_store_application_server Started
[Pipeline] sleep
Sleeping for 10 sec
[Pipeline] sh
+ echo Docker Built Image Successfully! Running Container....
Docker Built Image Successfully! Running Container....
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] load
[Pipeline] { (./test_env/version_env.groovy)
[Pipeline] }
[Pipeline] // load
[Pipeline] sh
////////////// Testing Stage goes there /////////////////////
+ echo Running Test Pipeline
Running Test Pipeline
[Pipeline] sh
+ echo Running Healtcheck Test...
Running Healtcheck Test...
[Pipeline] sh
+ echo Sleeping until the Application will be fully ready...
Sleeping until the Application will be fully ready...
[Pipeline] sleep
Sleeping for 10 sec
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ curl -s -X GET -H accept: */* http://0.0.0.0:8000/healthcheck/
[Pipeline] }
[Pipeline] // script
Post stage
[Pipeline] dir
Running in /var/jenkins_home/workspace/Store Pipeline/test_env
[Pipeline] {
[Pipeline] sh
+ echo Removing Testing Environment
Removing Testing Environment
[Pipeline] sh
+ docker-compose down
Container test_store_application_server Stopping
Container test_store_application_server Stopping
Container test_store_application_server Stopped
Container test_store_application_server Removing
Container test_store_application_server Removed
Container test_postgres_store_database Stopping
Container test_postgres_store_database Stopping
Container test_postgres_store_database Stopped
Container test_postgres_store_database Removing
Container test_postgres_store_database Removed
///// Deployment Stage Goes there.... ////////////
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (deployment)
Stage "deployment" skipped due to earlier failure(s) ///// The Error Message Goes There....
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
//// ERROR /////
[Pipeline] End of Pipeline
ERROR: script returned exit code 7
Finished: FAILURE
The Problem is that it does not respond what's exactly the problem is, (just simply skipped due to earlier failure(s), above logs does not shows any of the errors.
So would really appreciate any help or any suggestions how to solve this Issue.
Thanks.

Dockerfile in Declarative pipeline job fails

Jenkins Version:-
Jenkins - 2.277.1 LTS.
My Dockerfile:-
FROM maven:3.6.0-jdk-13
RUN useradd -m -u 1000 -s /bin/bash jenkins
My Declarative Pipeline:-
pipeline {
agent {
label "VM-Linux-Agent"
}
environment {
DOCKERFILE = "Dockerfile"
}
stages {
stage("Checkout") {
steps {
git(
url: 'git#gitlab.company.com:maven-prj-group/mavenapp.git',
branch: "master"
)
}
}
stage("Build") {
agent {
dockerfile {
filename DOCKERFILE
args "-v $WORKSPACE:/var/maven"
}
}
steps {
sh "mvn clean install"
}
}
}
}
From Jenkins master i have configured Linux server as node VM-Linux-Agent and using this node pipeline job code checkout is happening and further using Dockerfile building a docker container then to run build and others steps on docker itself steps are not working. it shows below errors.
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/Dockerfile-Pipeline
[Pipeline] {
[Pipeline] isUnix
[Pipeline] readFile
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.nio.file.NoSuchFileException: /var/jenkins_home/workspace/Dockerfile-Pipeline/Dockerfile
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
at java.nio.file.Files.newByteChannel(Files.java:361)
at java.nio.file.Files.newByteChannel(Files.java:407)
at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
at java.nio.file.Files.newInputStream(Files.java:152)
at hudson.FilePath.newInputStreamDenyingSymlinkAsNeeded(FilePath.java:2112)
at hudson.FilePath.read(FilePath.java:2097)
at hudson.FilePath.read(FilePath.java:2089)
at org.jenkinsci.plugins.workflow.steps.ReadFileStep$Execution.run(ReadFileStep.java:104)
at org.jenkinsci.plugins.workflow.steps.ReadFileStep$Execution.run(ReadFileStep.java:94)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE
Build step instead of running on docker container it is running on master then it fails. Since i want to run only build + test steps on docker container from my node system(This is Docker Host). So how do i fix this in my declarative pipeline? please let me know the way to do this. Thanks in advance.
In the pipeline code section global agent replaced with none and to checkout the code on specific slave(In my case VM-Linux-Agent) added as agent label.
The below code working for me.
pipeline {
agent none
stages {
stage("Checkout") {
agent {
label "VM-Linux-Agent"
}
steps {
git(
url: 'git#gitlab.company.com:maven-prj-group/mavenapp.git',
branch: "master"
)
}
}
stage("Build") {
agent {
dockerfile {
filename 'Dockerfile'
label 'VM-Linux-Agent'
args "-v /home/user/maven:/var/maven"
}
}
steps {
sh "mvn clean install"
}
}
}
}

Jenkins is adding private regisrty URL tag while pulling Image

I am getting this issue in Jenkins pipeline where I want to pull 'node' image but jenkins is adding the private docker registry url tag to it so the image is not found (artifactory.x.com/node:7-alpine)
Here is the pipeline
pipeline {
agent {
docker
{
image 'node:7-alpine'
registryUrl 'https://artifactory.x.com/'
registryCredentialsId 'jenkins-artifactory'
}
}
stages {
stage('Test') {
steps {
sh 'node --version'
}
}
}
}
This is the error I am getting
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/jobs/enterprise-master/workspace
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withDockerRegistry
$ docker login -u jenkins -p ******** https://artifactory.x.com/
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /var/jenkins_home/jobs/enterprise-master/workspace#tmp/f54c8b21-837b-4652-b12c-d489fb7e4c4c/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[Pipeline] {
[Pipeline] isUnix
[Pipeline] sh
+ docker inspect -f . node:7-alpine
Error: No such object: node:7-alpine
[Pipeline] isUnix
[Pipeline] sh
+ docker inspect -f . artifactory.x.com/node:7-alpine
Error: No such object: artifactory.x.com/node:7-alpine
[Pipeline] isUnix
[Pipeline] sh
+ docker pull artifactory.x.com/node:7-alpine
Error response from daemon: unknown: Not Found
[Pipeline] }
[Pipeline] // withDockerRegistry
Now the problem is that there is no image artifactory.x.com/node:7-alpine so it cant be found.
How do I tell jenkins not to add the private repo URL while pulling.
Fixed this by removing Docker Registry URL and setting Registry credentials to none
Jenkins -> Manage Jenkins->Configure > Pipeline Model Definition
Also the pipeline definition is still same
pipeline {
agent {
docker
{
image 'node:7-alpine'
registryUrl 'https://artifactory.X.com/'
registryCredentialsId 'jenkins-artifactory'
}

Jenkinsfile: connect to a server and perform operations on the server

In Jenkinsfile I am trying to login to a server and from there run docker-compose. However, I am getting an AccessDeniedException error. Root user has permissions to all folders on the server.
I assume I have a thinking error: I suspect that after ssh to the server, the dir command doesn't not run in the server but rather where the jenkins is run.
Jenkinsfile:
pipeline {
agent any
environment {
PATH = "$PATH:/usr/local/bin/"
}
stages {
stage('Deploy to digital ocean') {
steps {
sshagent(['my-private-ssh-key']) {
sh """
ssh root#host
"""
dir("/var/www/car_prices"){
sh "docker-compose down"
}
}
}
}
}
}
Error stack:
[Pipeline] dir
Running in /var/www/car_prices
[Pipeline] {
[Pipeline] sh
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
$ ssh-agent -k
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 67144 killed;
[ssh-agent] Stopped.
[Pipeline] // sshagent
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.nio.file.AccessDeniedException: /var/www
Question:
1. Why does it throw an error?
You need to use ssh to do the job for you, which is rather straight forward:
...
sshagent(['my-private-ssh-key']) {
sh """
ssh root#host "cd /var/www/car_prices; docker-compose down"
"""
}
...
This is kind of ugly, but works. If you care, you can split commands into multiple lines for better readability. This approach becomes even trickier if you want to provide some more complex functionality, like error handling.

Jenkins Pipeline push the Docker Image

I'm trying to push my docker image after building source code, when jenkins push images to docker Hub Registry i am getting below error.
Pipeline Script
stage('Build Docker Image') {
container('docker') {
echo 'docker'
sh "docker build -t ${image_name} ."
sh "docker tag ${image_name} ${image_name}:${image_tag}"
}
}
stage('Push Docker Image') {
container('docker') {
withCredentials([string(credentialsId: 'DOCKER_HUB_CREDENTIALS', variable: 'DOCKER_HUB_CREDENTIALS')]) {
sh "docker login -u user-name -p ${DOCKER_HUB_CREDENTIALS}"
}
sh "docker push ${image_name}:${image_tag}"
}
}
Jenkins Logs
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Push Docker Image)
[Pipeline] container
[Pipeline] {
[Pipeline] withCredentials
Masking supported pattern matches of $DOCKER_HUB_CREDENTIALS
[Pipeline] {
[Pipeline] sh
+ docker login -u user-name -p ****
Login Succeeded
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] sh
+ docker push devopsimage.azure/frontend:bug-fix-2cbb925d
The push refers to repository [devopsimage.azure/frontend]
Get https://devopsimage.azure/v2/: dial tcp: lookup devopsimage.azure.io: Temporary failure in name resolution
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
Can you please any one help me on this ?
If you have dot(.) in your image name before the slash, docker treats it as the registry name. So docker push is trying to push to registry named devopsimage.azure. If you actually want to push to Docker Hub instead, remove the dot from the image name. If you want to push to registry named devopsimage.azure, then there is a DNS issue in resolving this registry from your build machine.

Resources