简体   繁体   English

Jenkins 管道与 Dockerfile 配置

[英]Jenkins Pipeline with Dockerfile configuration

I am struggling, to get the right configuration for my Jenkins Pipeline.我正在努力为我的 Jenkins 管道获得正确的配置。

It works but I could not figure out how to seperate test & build stages.它有效,但我无法弄清楚如何分开测试和构建阶段。

Requirements:要求:

  • Jenkins Pipeline with seperated test & build stage Jenkins 具有独立测试和构建阶段的管道
  • 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)构建阶段正在构建一个 docker 镜像,稍后发布(发布阶段)

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: 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,这可行,但正如您所见,“构建和测试 DEV”是一个阶段,

I would like to have 2 seperate jenkins stages (Test, Build)我想要 2 个单独的 jenkins 阶段(测试,构建)

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. 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我还考虑过使用已经包含铬的 docker 图像,但找不到任何官方图像

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:您可以构建您的自定义映像(包括 Chromium 的安装)并将其推送到注册表,然后从该注册表中提取它:

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 :或者使用 Dockerfile 直接使用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.从位于Dockerfile的 Dockerfile 构建test-image ./dockerfiles/test/Dockerfile.

Reference: Using Docker with Pipeline参考:使用 Docker 与流水线

So in general I would execute the npm run commands inside the groovy syntax and not inside the dockerfile.所以一般来说,我会在 groovy 语法内而不是在 dockerfile 内执行 npm 运行命令。 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我还建议您使用警告 ng jenkins 插件在 Jenkins 中收集结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 Jenkins 管道参数传递给 Dockerfile - Passing Jenkins Pipeline parameter to a Dockerfile Jenkins:创建管道以读取 dockerfile - Jenkins: Creating pipeline to read dockerfile 如何在基于 dockerfile 的容器中运行 Jenkins 管道? - How to run Jenkins pipeline in a container based on a dockerfile? 将 dockerfile 与 Jenkins 脚本化流水线语法一起使用 - Using a dockerfile with Jenkins Scripted Pipeline Syntax 将配置文件从 Dockerfile 插入到声明性 Jenkins 管道 Docker 容器中 - Insert a config file into a declarative Jenkins pipeline Docker container from Dockerfile 如何在声明式管道中将 Jenkins 构建参数作为参数传递给 dockerfile - How to pass Jenkins build parameter as arguments to dockerfile in declarative pipeline 如何创建 Dockerfile 与 maven jdk 和 docker 安装为 ZAB63A76362C3972AC83DC5CB8 - How to creating Dockerfile with maven jdk and docker installed for jenkins pipeline 声明性 Jenkins 管道:是否可以使用 SSH 凭据构建 Dockerfile? - Declarative Jenkins pipeline: is it possible to build Dockerfile with SSH credentials? 使用 Jenkins 声明式管道为 dockerfile 代理设置构建参数 - Setting build args for dockerfile agent using a Jenkins declarative pipeline 带有来自 SCM 的 Docker/Dockerfile 代理的 Jenkins 声明式管道 - Jenkins declarative pipeline with Docker/Dockerfile agent from SCM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM