'Cannot run program "docker"' when using withDockerRegistry in Declarative Jenkins Pipeline - docker

I'm in the middle of refactoring our old scripted jenkins pipeline and am trying to take advantage of the nice jenkins declarative syntax.
However I'm having issue trying to authenticate with our private docker registry.
The resources online tell me this should work:
steps {
sh 'docker -v'
withDockerRegistry([url: DOCKER_REGISTRY_URL, credentialsId: DOCKER_REGISTRY_CREDENTIALS]) {
pushDockerImage()
}
}
The first line, "docker -v", executes correctly and the current docker version is printed out.
However when it tries to execute "withDockerRegistry" it fails with:
Cannot run program "docker": error=2, No such file or directory
Do I have the syntax wrong or am I missing some sort of global config?
Cheers,

Add a toolName to the withDockerRegistry:
withDockerRegistry(registry: [url: DOCKER_REGISTRY_URL, credentialsId: DOCKER_REGISTRY_CREDENTIALS], toolName: 'docker')
Also setup docker in Global Tool Configuration.

Related

Jenkins Groovy pipeline #tmp dir() AccessDenied exception for createDirectory

I'm trying to set-up a simple (single) Groovy pipeline script that would deploy my PHP project using webhooks. Sounds like a simple script and I've had it working in an older Jenkins version (which used bash script) but when I use the dir(String) {} method I get the following exception:
java.nio.file.AccessDeniedException: /srv/www/staging#tmp
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:384)
at java.nio.file.Files.createDirectory(Files.java:674)
at java.nio.file.Files.createAndCheckIsDirectory(Files.java:781)
at java.nio.file.Files.createDirectories(Files.java:767)
at hudson.FilePath.mkdirs(FilePath.java:3563)
The server itself runs on CentOS 7 and Jenkins is using version 2.303.3. I've added the jenkins user to the apache group, which is the owner+group of the relative webroot, located in /srv/www/staging. The permissions for the /srv/www and /srv/www/staging directories are set to 0773 apache:apache.
When I simulate a bash session with user jenkins using su -s /bin/bash jenkins, I am able to create directories and files within the /srv/www/staging directory - without any issue. So I'm wondering how the pipeline itself doesn't have permissions to do so!..
The pipeline script is as follows:
pipeline {
agent any
stages {
stage('Git Pull') {
steps {
sh "whoami"
dir ('/srv/www/staging') {
sh "pwd"
}
}
}
}
}
The sh "whoami" returns jenkins, so I'm sure the proper user is executing the commands.
I can't really find a descriptive similar issue, thus why I'm posting this.
Hopefully someone is able to guide me in the right direction.

"Docker: command not found" from Jenkins on MacOS

When running jobs from Jenkinsfile with Pipeline syntax and a Docker agent, the pipeline fails with "Docker: command not found." I understand this to mean that either (1) Docker is not installed; or (2) Jenkins is not pointing to the correct Docker installation path. My situation is very similar to this issue: Docker command not found in local Jenkins multi branch pipeline . Jenkins is installed on MacOS and running off of localhost:8080. Docker is also installed (v18.06.0-ce-mac70)./
That user's solution included a switch from pipeline declarative syntax to node scripted syntax. However I want to resolve the issue while retaining the declarative syntax.
Jenkinsfile
#!groovy
pipeline {
agent {
docker {
image 'node:7-alpine'
}
}
stages {
stage('Unit') {
steps {
sh 'node -v'
sh 'npm -v'
}
}
}
}
Error message
docker inspect -f . node:7-alpine
docker: command not found
docker pull node:7-alpine
docker: command not found
In Jenkins Global Tool Configuration, for Docker installations I tried both (1) install automatically (from docker.com); and (2) local installation with installation root /usr/local/.
All of the relevant plugins appears to be installed as well.
I solved this problem here: https://stackoverflow.com/a/58688536/8160903
(Add Docker's path to Homebrew Jenkins plist /usr/local/Cellar/jenkins-lts/2.176.3/homebrew.mxcl.jenkins-lts.plist)
I would check the user who is running the jenkins process and make sure they are part of the docker group.
You can try adding the full path of docker executable on your machine to Jenkins at Manage Jenkins > Global Tool Configuration.
I've seen it happen sometimes that the user which has started Jenkins doesn't have the executable's location on $PATH.

jenkins pipeline. Ssh to a server get stuck on job

I need to ssh to a server from a simple jenkin pipeline and make a deploy which is simply moving to a directory and do a git fetch and some other comands (nmp install among others). Thing is that when jenkin job ssh to the remote server it connects ok but then It gets stucked, I have to stop it. I just now modify the script to simply do a "ssh to server " and a "pwd command" to go to the easiest but it connects to it and it get stuck untill I abort. What Am I missing? here is the simpe pipeline script and the output on an screenshot
pipeline {
agent any
stages {
stage('Connect to server') {
steps {
sh "ssh -t -t jenkins#10.x.x.xx"
sh "pwd"
}
}
stage('branch status') {
steps {
sh "git status"
}
}
}
}
Jenkins executes each "sh" step as a separate shell script. Content is written to a temporary file on Jenkins node and only then executed. Each command is executed in separate session and is not aware of previous one. So neither ssh session or changes in environment variable will persist between the two.
More importantly though, you are forcing pseudo-terminal allocation with -t flag. This is pretty much opposite to what you want to achieve, i.e. run shell commands non-interactively. Simply
sh "ssh jenkins#10.x.x.xx pwd"
is enough for your example to work. Placing the commands on separate lines would not work with regular shell script, regardless of Jenkins. However you still need to have private key available on node, otherwise the job will hang, waiting for you to provide password interactively. Normally, you will want to use SSH Agent Plugin to provide private key at runtime.
script {
sshagent(["your-ssh-credentals"]) {
sh "..."
}
}
For execution on longer commands see What is the cleanest way to ssh and run multiple commands in Bash?

Jenkins error "No such property: docker for class: groovy.lang.Binding"

I'm trying to follow this tutorial to create a simple docker environment in as part of my Jenkins pipeline build.
I'm trying to build a couple of Docker images just as a test before I do my maven build. At the moment I have the following Groovy for my Jenkinsfile:
#!groovy
node {
stage 'Building docker env'
def dbImage = docker.build('oracle', 'docker/oracle')
def wlpImage = docker.build('liberty', 'docker/liberty')
stage 'Running maven build'
git url: 'https://mysite/myproject.git', branch: 'docker'
def mvnHome = tool 'maven 3.3.9'
sh "${mvnHome}/bin/mvn -B clean install"
}
I'm trying to have the Docker build look in the directory "docker/oracle" and call the Dockerfile in that directory, and build the Docker images named oracle, and liberty. At the moment though it's giving me this error:
Running on master in /root/.jenkins/workspace/pipeline_test
[Pipeline] {
[Pipeline] stage (Building docker env)
Using the ‘stage’ step without a block argument is deprecated
Entering stage Building docker env
Proceeding
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: docker for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224)
at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:221)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:28)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
...
Any ideas what could be the problem with the docker build command I'm using? (or could it be something I forgot to install in Jenkins?)
The issue was I needed to install the Docker Pipeline plugin in Jenkins.
To install the plugin using the GUI:
Dashboard > Manage Jenkins > Manage Plugins > Available (tab) > docker-workflow.
As Pete says you will have to install the Docker Pipeline plugin. You can do that though the Jenkins UI.
Maybe I'm missing some part of your code, but where do you define the docker? If that's the complete Groovy script, you're trying to bind a variable which isn't declared anything, so it's not to weird that it fails, right?
Just define a docker it that's what you want, like:
def docker = "my docker" // something similar like this
And it will at-least resolve your missing property exception.
Whenever we see the error like below :
groovy.lang.MissingPropertyException: No such property:
It means , groovey script did not able to find the property mentioned post the colon sign : , so we need to either define the user defined variable/property or use the correct one from API.
If you have this issue:
groovy.lang.MissingPropertyException: No such property: docker for class: groovy.lang.Binding.
We most likely encountered the same issue, in order to fix it I only had to install the Docker Pipeline plugin in Jenkins, so all you have to do is go to:
Jenkins Homepage > Manage Jenkins > Manage Plugins > Available
Search for Docker Pipeline install it restart jenkins and you are ready to go.
For more info about Docker Pipeline Plugin Scripts click here.
I had the same issue but after I installed the Docker Pipeline plugin as #Affes Salem suggested it is working now.

Execute a script from jenkins pipeline

I have a jenkins pipeline that builds a java artifact,
copies it to a directory and then attempts to execute a external script.
I am using this syntax within the pipeline script to execute the external script
dir('/opt/script-directory') {
sh './run.sh'
}
The script is just a simple docker build script, but the build will fail
with this exception:
java.io.IOException: Failed to mkdirs: /opt/script-directory#tmp/durable-ae56483c
The error is confusing because the script does not create any directories. It is just building a docker image and placing the freshly built java artifact in that image.
If I create a different job in jenkins that executes the external script as
its only build step and then call that job from my pipeline script using this syntax:
build 'docker test build'
everything works fine, the script executes within the other job and the pipeline
continues as expected.
Is this the only way to execute a script that is external to the workspace?
What am I doing wrong with my attempt at executing the script from within
the pipeline script?
The issue is that the jenkins user (or whatever the user is that runs the Jenkins slave process) does not have write permission on /opt and the sh step wants to create the script-directory#tmp/durable-ae56483c sub-directory there.
Either remove the dir block and use the absolute path to the script:
sh '/opt/script-directory/run.sh'
or give write permission to jenkins user to folder /opt (not preferred for security reasons)
Looks like a bug in Jenkins, durable directories are meant to store recovery information e.g. before executing an external script using sh.
For now all you can do is make sure that /opt/script-directory has +r +w and +x set for jenkins user.
Another workaround would be not to change the current directory, just execute sh with it:
sh '/opt/script-directory/run.sh'
I had a similar concern when trying to execute a script in a Jenkins pipeline using a Jenkinsfile.
I was trying to run a script restart_rb.sh with sudo.
To run it I specified the present working directory ($PWD):
sh 'sudo sh $PWD/restart_rb.sh'

Resources