简体   繁体   中英

Jenkins Pipeline with Dockerfile configuration

I am struggling, to get the right configuration for my Jenkins Pipeline.

It works but I could not figure out how to seperate test & build stages.

Requirements:

  • Jenkins Pipeline with seperated test & build stage
  • Test stage requires chromium (I currently use node alpine image + adding chromium)
  • Build stage is building a docker image, which is published later (publish stage)

Current Setup:

Jenkinsfile:

pipeline {

environment {
    ...
}
options {
    ...
}
stages {
    stage('Restore') {
       ...
    }
    stage('Lint') {
       ...
    }

    stage('Build & Test DEV') {
        steps {
            script {
                dockerImage = docker.build(...)
            }
        }
    }

    stage('Publish DEV') {
        steps {
            script {
                docker.withRegistry(...) {
                    dockerImage.push()
                }
            }

        }
    }

Dockerfile:

FROM node:12.16.1-alpine AS build

#add chromium for unit tests
RUN apk add chromium

...

ENV CHROME_BIN=/usr/bin/chromium-browser

...

# works but runs both tests & build in the same jenkins stage
RUN npm run test-ci

RUN npm run build

...

This works, but as you can see "Build & Test DEV" is a single stage,

I would like to have 2 seperate jenkins stages (Test, Build)

I already tried using Jenkins agent docker and defining the image for the test stage inside the jenkins file, but I dont know how to add the missing chromium package there.

Jenkinsfile:

pipeline {
agent {
    docker {
        image 'node:12.16.1-alpine'
        //add chromium package here?
        //set Chrome_bin env?
    }
}

I also thought about using a docker image that already includes chromium, but couldnt find any official images

Would really appreciate your help / insights how to make this work.

You can either build your customized image (which includes the installation of Chromium) and push it to a registry and then pull it from that registry:

node {
    docker.withRegistry('https://my-registry') {

        docker.image('my-custom-image').inside {
            sh 'make test'
        }
    }
}

Or build the image directly with Jenkins with your Dockerfile :

node {
    def testImage = docker.build("test-image", "./dockerfiles/test") 

    testImage.inside {
        sh 'make test'
    }
}

Builds test-image from the Dockerfile found at ./dockerfiles/test/Dockerfile.

Reference: Using Docker with Pipeline

So in general I would execute the npm run commands inside the groovy syntax and not inside the dockerfile. So your code would look something like that:

pipeline {
  agent {
    docker {
      image 'node:12.16.1-alpine'
      args  '-u root:root' // better would be to use sudo, but this should work
    }
  }
  stages {
    stage('Preparation') {
      steps {
        sh 'apk add chromium'
      }
    }
    stage('build') {
      steps {
        sh 'npm run build'
      }
    }
    stage('test') {
      steps {
        sh 'npm run test'
      }
    }
  }
}

I would also suggest that you collect the results within Jenkins with the warnings ng jenkins plugin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM