user6410013
user6410013

Reputation: 31

How can load the Docker section of my Jenkins pipeline (Jenkinsfile) from a file?

I have multiple pipelines using Jenkinsfiles that retrieve a docker image from a private registry. I would like to be able to load the docker specific information into the pipelines from a file, so that I don’t have to modify all of my Jenkinsfiles when the docker label or credentials change. I attempted to do this using the example jenkinsfile below:

def common

pipeline {
   agent none
   options {
     timestamps()
   }
   stages {
    stage('Image fetch') {
     steps{
        script {
              common = load('/home/jenkins/workspace/common/docker_image')
              common.fetchImage()
        }
     }
  }
}

With docker_image containing:

def fetchImage() {
  agent {
   docker {
    label “target_node ”
    image 'registry-url/image:latest'
    alwaysPull true
    registryUrl 'https://registry-url’
    registryCredentialsId ‘xxxxxxx-xxxxxx-xxxx’
   }
  }
}

I got the following error when I executed the pipeline:

Required context class hudson.FilePath is missing Perhaps you forgot to surround the code with a step that provides this, such as: node,dockerNode

How can I do this using a declarative pipeline?

Upvotes: 3

Views: 1595

Answers (1)

tftd
tftd

Reputation: 17062

There are a few issues with this:

  1. You can allocate a node only at top level

    pipeline {
      agent ...
    }
    

    Or you can use a per-stage node allocation like so:

    pipeline {
      agent none
      ....
      stages {
        stage("My stage") {
          agent ...
          steps {
            // run my steps on this agent
          }
        }
      }
    }
    

    You can check the docs here

  2. The steps are supposed to be executed on the allocated node (or in some cases they can be executed without allocating a node at all).

  3. Declarative Pipeline and Scripted Pipeline are two different things. Yes, it's possible to mix them, but scripted pipeline is meant to either abstract some logic into a shared library, or to provide you a way to be a "hard core master ninja" and write your own fully custom pipeline using the scripted pipeline and none of the declarative sugar.

I am not sure how your Docker <-> Jenkins connection is setup, but you would probably be better if you install a plugin and use agent templates to provide the agents you need.

If you have a Docker Swarm you can install the Docker Swarm Plugin and then in your pipeline you can just configure pipeline { agent { label 'my-agent-label' } }. This will automatically provision your Jenkins with an agent in a container which uses the image you specified.

If you have exposed /var/run/docker.sock to your Jenkins, then you could use Yet Another Docker Plugin, which has the same concept.

This way you can remove the agent configuration into the agent template and your pipeline will only use a label to have the agent it needs.

Upvotes: 3

Related Questions