1
votes

I have a Node.js application with a Docker file. I want to build this in Jenkins. I also want to run test cases.For which I have defined a script in package.json file. Which runs test cases and generates coverage report.

From what I have understood. I need to

  1. Run Jenkins in Docker
  2. Checkout branch
  3. Create a Docker in Jenkins
  4. Build my application
  5. Run test cases and generate coverage reports
  6. Deploy

I want to checkout my repository from github.com . Here are the steps that I followed

  1. Click on new item on Jenkins main page
  2. Entered name and chose pipeline form list
  3. Under Pipeline I chose definition as Pipeline script from SCM
  4. Chose SCM as GIT
  5. Under Repository entered the Repository Url with the credentials
  6. Under Branches to build entered by branch as */my_branch_here
  7. Added the script path for Jenkins file

Here is what my Jenkins file looks like

pipeline {
agent {
    docker {
        image 'node:6-alpine' 
        args '-p 3000:3000' 
    }
}
stages {
    stage('initialization') {
        steps {
            script {
                TAG_NAME = '1.1.'+ "${env.BUILD_NUMBER}"
                def clientImage = docker.build("registrypath:5000/test:${TAG_NAME}", "-f ./path_to_dockerfile/Dockerfile .")
            }
        }
    }
}   

}

I get a Docker error: command not found. I have not been able to complete the first step to proceed further.

3

3 Answers

1
votes

You can use docker-in-docker in order to build your images in a docker container.

That is not quite recommendable due to data corruption.

Recommended: install in your own image, derived from jenkins/jenkins:lts, the binary file for docker and mount /var/lib/docker from your machine, where Jenkins is run.

Through this, you have also the advantage that your image are cached on your machine and you must not pull always the image from repository because Jenkins was restarted.

For more informations:

0
votes

Make sure that Docker is installed on your Jenkins machine or that Jenkins itself is running in a Docker container.

0
votes

You have to install docker client inside your jenkins container. You can do it by custom docker image.

Jenkins documentation say how to do that here.

More explanation from a guy here.

I did it (with fresh docker installation instructions for centos7) and that works well. You have to mount /var/run/docker.sock inside container and add jenkins user in docker group : usermod -a -G docker jenkins