繁体   English   中英

Jenkins 管道与 Dockerfile 配置

[英]Jenkins Pipeline with Dockerfile configuration

我正在努力为我的 Jenkins 管道获得正确的配置。

它有效,但我无法弄清楚如何分开测试和构建阶段。

要求:

  • Jenkins 具有独立测试和构建阶段的管道
  • 测试阶段需要铬(我目前使用节点高山图像+添加铬)
  • 构建阶段正在构建一个 docker 镜像,稍后发布(发布阶段)

当前设置:

詹金斯文件:

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

...

这可行,但正如您所见,“构建和测试 DEV”是一个阶段,

我想要 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.

詹金斯文件:

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

我还考虑过使用已经包含铬的 docker 图像,但找不到任何官方图像

非常感谢您的帮助/见解如何使这项工作。

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

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

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

或者使用 Dockerfile 直接使用Dockerfile构建映像:

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

    testImage.inside {
        sh 'make test'
    }
}

从位于Dockerfile的 Dockerfile 构建test-image ./dockerfiles/test/Dockerfile.

参考:使用 Docker 与流水线

所以一般来说,我会在 groovy 语法内而不是在 dockerfile 内执行 npm 运行命令。 所以你的代码看起来像这样:

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'
      }
    }
  }
}

我还建议您使用警告 ng jenkins 插件在 Jenkins 中收集结果

暂无
暂无

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

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