Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Jenkins Job in a container

I have several Jenkins jobs and several Jenkins slave nodes (physical machines). These slaves are all running docker 1.12.1. I would like for any of these jobs to run on any of these slaves. Since these jobs have different environment requirements, I thought that I could create docker images for each of the different environments and then "within" the job, specify which container the job would run in.

Is this possible? i.e. to specify within these jobs a specific docker container that I would like these jobs to run in? At the end of the job, the containers would be destroyed and only the artifacts and reports would remain.

There are several docker plugins that exist in Jenkins but I have yet found one that does exactly what I want.

like image 306
Jon Avatar asked Nov 05 '16 03:11

Jon


2 Answers

Of course, the answer is Yes, this is possible.

Given the broad type of question, I can only give pointers.

jenkins-pipeline offers a nice way to describe your pipelines as code. The docker plugin also integrates with that, so that you can run your jobs within a container, e.g.

// select a slave based on the label 'docker'
node('docker') {
  stage "Run in container (e.g. the php-7)"
  docker.image('php:7').inside {
    // run your command
    sh "phpunit"
  }
}

There are couple of resources out there regarding jenkins-pipelinedocker that will get you started.

like image 59
StephenKing Avatar answered Sep 19 '22 13:09

StephenKing


I found the plugin I was looking for.

https://plugins.jenkins.io/docker-custom-build-environment/

This will do what I want. It will allow me to run my jobs in a container. So far, it is working well.

like image 37
Jon Avatar answered Sep 21 '22 13:09

Jon